So, I faced a task - to programmatically determine whether any of the paired devices are currently connected to my phone via Bluetooth. Long and unsuccessfully I searched on the network for any ready-made solution on this issue, but all I could do was to find only an indication that it was possible to track the connection event via Bluetooth. But after all, the program can be launched after the event, therefore, it did not suit me.
Actually after that (and browsing the sections dedicated to Bluetooth in the official Android documentation), the thought came to try to connect with each paired device, and then look at the success of the operation: if successful, it means the device is in the coverage area and connected. The venture was successful.
However, on the way to its implementation, I expected another trick:
BluetoothSocket bs = device.createRfcommSocketToServiceRecord(MY_UUID); bs.connect();
This client connection creation code did not want to run, always returning the “Service discovery failed” error. Again, searching, reading and identifying the fact of a mass of complaints about the same problem. Tips for solving this problem boiled down to one thing: proposing different values ​​for MY_UUID. I tried the N-th number of different UUIDs from these tips, but I could not get a connection between Windows Mobile and Android. An interesting point: when you try to connect to a “sleeping” WM-communicator, the display lights up. That is, the connection is still initialized, but for some reason it is not installed. The decision was found by
compatriot :
Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class }); socket = (BluetoothSocket)m.invoke(device, Integer.valueOf(1));
And this method really works flawlessly.
The general Bluetooth verification code for connectivity looks like this:
boolean checkConnected() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); boolean connected = false; for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) { try { try { Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class }); try { BluetoothSocket bs = (BluetoothSocket) m.invoke(device,Integer.valueOf(1)); bs.connect(); connected = true; Log.d(TAG, device.getName() + " - connected"); break; } catch (IOException e) { Log.e(TAG, "IOException: "+e.getLocalizedMessage()); Log.d(TAG, device.getName() + " - not connected"); } } catch (IllegalArgumentException e) { Log.e(TAG, "IllegalArgumentException: "+e.getLocalizedMessage()); } catch (IllegalAccessException e) { Log.e(TAG, "IllegalAccessException: "+e.getLocalizedMessage()); } catch (InvocationTargetException e) { Log.e(TAG, "InvocationTargetException: "+e.getLocalizedMessage()); } } catch (SecurityException e) { Log.e(TAG, "SecurityException: "+e.getLocalizedMessage()); } catch (NoSuchMethodException e) { Log.e(TAG, "NoSuchMethodException: "+e.getLocalizedMessage()); } } return connected; }
Of course, the code does not work at lightning speed. But nevertheless, the code works and performs its function, especially since I could not find any other solutions. Due to the fact that I have not so much experience in Android, it is possible that there is something else to correct in the code or there is some other solution. But this will prompt experts.