Detecting Bluetooth connectivity for Android

So, before me there was a task - to programmatically determine if any of the paired devices are currently connected to my phone via Bluetooth. I searched for a long time and unsuccessfully on the network for any ready-made solution on this subject, but the distance was only found to indicate that it was possible to track the connection event via Bluetooth. But the program can be launched after the event, therefore, this did not suit me.

Actually after that (and scrolling through the sections on Bluetooth in the official Android documentation), the thought came to try connecting 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 is connected. The idea was successful.
However, on the way to its implementation there was still a catch:
BluetoothSocket bs = device.createRfcommSocketToServiceRecord(MY_UUID);
bs.connect();

This client connection creation code did not want to be executed in any way, always returning a “Service discovery failed” error. Again, searching, reading and revealing the fact of a mass of complaints about the same problem. Tips for solving this problem boiled down to one thing: offering 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 trying to connect to a "sleeping" WM-communicator, the display lights up. That is, the connection is still initialized, but for some reason is not established. The compatriot found a solution :
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 connection test code looks something 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 lightning fast. Nevertheless, the code works and performs its function, especially since I could not find other solutions. Due to the fact that my experience in Android is not so big, maybe there is something else to fix in the code or there is some other solution. But experts already will prompt it.

Also popular now: