Android Bluetooth accepts () / connect () with already paired devices

I had problems connecting two Android devices via Bluetooth, which only happens when they were paired before. I start one server and the other as a client.

Here is the sequence of things on the server side:

  • Check the various statuses of Bluetooth (adapter available, enabled, etc.).
  • call listenUsingRfcommWithServiceRecord () with the predefined UUID that I selected.
  • device discovery request
  • since detection happens asynchronously, I call accept () and wait for your incoming connection.

On the client side:

  • Check the various statuses of Bluetooth (adapter available, enabled, etc.).
  • for each device in getBondedDevices (), I compare getName () with the server name. If there is a match, go to step 6.
  • Start BT discovery
  • For each device found (note that paired devices from 2a do not appear here), compare the device name with the server name. If there is a match, go to step 6.
  • Cancel Detection
  • On the device that was found in step 2, call createRfcommSocketToServiceRecord () with the same predefined UUID that was used on the server side.
  • Call connect () and wait for it to return the connected socket.

The above process works fine for me when the client and server have never been paired before. However, after Android registers them in the list of devices, they will inevitably go to the connect () / accept () stage.

I searched for a solution for a couple of days and tried a lot of things, including this one: Connecting to an already connected Bluetooth device

The reflection method does not work for me either. It seems that connect () will return immediately, but when I tried getOutputStream (), I get an exception. On the accept () side, it does not even register that someone was trying to connect. I really need help or a pointer to ensure that the devices establish a connection as soon as they are paired before.

The following is device information:

  • I am testing the server and client on two LG G2X phones.
  • Both of them work on Android 2.3.3, which corresponds to API level 10.
  • Again, the above works after I manually disconnected the devices in the settings.

Thanks in advance. I am about two weeks old on Android and Bluetooth, so if you see any missing steps or best practices, indicate them as well.

+7
source share
3 answers

In the client, when I try to connect to a paired device, I just called it on the BluetoothDevice, which I found in BluetoothAdapter.getBondedDevices() . This does not work.

In order to establish a Bluetooth connection correctly, I had to do something similar to the pseudo code below:

 BluetoothDevice bonded = a device from BluetoothAdapter.getBondedDevices(); BluetoothDevice actual = BluetoothAdapter.getRemoteDevice(bonded.getAddress()); BluetoothSocket socket = actual.createRfcommSocketToServiceRecord(SOME_UUID); socket.connect(); 

I came up with this answer, following exactly the example of a Bluetooth chat: Bluetooth chat service . Why this does not work on the device from getBondedDevices() , is outside of me. Perhaps someone with a deeper knowledge of Android can answer this question.

+6
source

Check out this example: http://developer.android.com/resources/samples/BluetoothChat/index.html .

this may explain how the bluetooth device and transformation are connected.

+2
source
 private static BluetoothSocket mSocket; BluetoothDevice selectDevice = null; 

 void connectDevice(){ if(mSocket == null) { //Log.d(TAG, "Socket is null"); UUID SPP_UUID = UUID .fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); Set<BluetoothDevice> bondedDevices = BluetoothAdapter .getDefaultAdapter().getBondedDevices(); //Log.d(TAG, "Size: " + bondedDevices.size()); /** * Select your divice form paired devices */ for (BluetoothDevice bluetoothDevice : bondedDevices) { selectDevice = bluetoothDevice; //Log.d(TAG, bluetoothDevice.getName()+" "+bluetoothDevice.getAddress()); } if(selectDevice.getBondState() == selectDevice.BOND_BONDED) { //Log.d(TAG, selectDevice.getName()); try { mSocket = selectDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); } catch (IOException e1) { // TODO Auto-generated catch block //Log.d(TAG, "socket not created"); e1.printStackTrace(); } try { mSocket.connect(); } catch (IOException e) { try { mSocket.close(); //Log.d(TAG, "Cannot connect"); } catch (IOException e1) { //Log.d(TAG, "Socket not closed"); e1.printStackTrace(); } } } 
0
source

All Articles