Connect to an already connected Bluetooth device

I recently tried to get the pairing process to work programmatically, and I succeeded. But recently, I found out that users of my application can be connected to several "interesting" devices. Therefore, I must prompt the user to select a device to connect to

Therefore, I must connect the user to an already connected Bluetooth device. But none of my efforts work. I tried to start the pairing process again using:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

as well as the following:

 Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1); 

which I implemented, and only a working way to connect my phone to the built-in Bluetooth device

So my question is:

  • Is it possible to disconnect a paired device and then connect to another embedded device? I tried .. just connect to a new device, but I can't get this to work.
+3
source share
1 answer

I'm afraid I'm not quite sure about your problem. Can't you create a socket for an already connected Bluetooth device?

First of all, if the device is already paired, you do not need to start the pairing process again. You just need to create a socket for communication, which will fail if the device is not available for communication. I have been doing some things lately, and I used the following code, which works great for me:

  try { Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); BluetoothSocket mySocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); } catch (<VARIOUS EXCEPTIONS>) { //Do stuff } 

To request the user to select which device, you can request a BluetoothAdapter for all currently connected devices as follows:

 Set<BluetoothDevice> bondedDevices = BluetoothAdapter .getDefaultAdapter().getBondedDevices(); 

Finally, you can create connections with multiple devices at the same time - see here: Android Bluetooth API connects to multiple devices

+2
source

All Articles