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,
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.