I am currently working on Android 4.3 Bluetooth Low Energy, I am able to connect to a device, get services, read/write service. Now when I try to connect to second device, services of second device is received and first device services are lost. Now when I try to connect to write/read characteristic of first device, nothing works.
Has any one tried connecting to multiple devices. How do you initialize Gatt for two devices?
I think SAMSUNG BLE API and Google's API have Broadcom Ble stack and Broadcom stack not supported multiple ble device currently.
I had exactly the same behavior on the Samsung BLE stack (galaxy S4). I hacked the samsung code and fixed it:
The class BluetoothGatt keeps a list of "all" discovered services. A call to BluetoothGatt->discoverServices will clear this list. I drived my own class and modified the behaviour of the function to only delete the services of the concerning BluetoothDevice. This is my code:
public class MyBluetoothGatt extends BluetoothGatt {
MyBluetoothGatt(Context paramContext, BluetoothProfile.ServiceListener paramServiceListener)
{
super(paramContext, paramServiceListener);
}
public final boolean discoverServices(BluetoothDevice paramBluetoothDevice)
{
if (paramBluetoothDevice == null)
{
Log.e("BtGatt.BluetoothGatt", "discoverServices() - device is null ");
return false;
}
Log.d("BtGatt.BluetoothGatt", "discoverServices() - device: " + paramBluetoothDevice.getAddress());
if ((d == null) || (this.f == 0))
return false;
// old code this.h.clear();
//--new code
@SuppressWarnings("rawtypes")
Iterator localIterator = this.h.iterator();
while (localIterator.hasNext())
{
BluetoothGattService localBluetoothGattService;
localBluetoothGattService = (BluetoothGattService)localIterator.next();
if (localBluetoothGattService.a().equals(paramBluetoothDevice))
{
this.h.remove(paramBluetoothDevice);
}
}
//end new code
this.d.discoverServices(this.f, paramBluetoothDevice.getAddress());
return true;
}
}
i also had to use a class editor to remove some of the "final" modifiers in order to be able to dervive my own class. It was a big hack but now it's working beautifully. I will upgrade my S4 to android 4.3 once the official release comes out and port my code, so fingers crossed this will work for android 4.3 as well.
It should work for all if you implement different BluetoothGatt
instance and different GattCallback
for each device you want to connect...