I am developing an application that uses the new Bluetooth BLE interface from Android 4.3.
I used Samsung BLE Stack for Android 4.2 and it works fine even if stability could be better.
Now with 4.3 for each client connection we have an instance of the class BluetoothGatt.
That is, I connect to the BLE device by calling
BluetoothGatt gatt = device.connectGatt(this, true, callbacks);
These objects BluetoothGattare those that are used to actually interact with the device.
Since I want to connect to and interact with several devices, I cache the instances BluetoothGattin HashMap, defined as a private service property:
private HashMap<String, BluetoothGatt> devicesGatts
= new HashMap<String, BluetoothGatt>();
The keys are device addresses.
, , BluetoothGatt HashMap:
public BluetoothGatt connectGatt(final BluetoothDevice device){
if(device == null){
return null;
}
String adr = device.getAddress();
BluetoothGatt gatt = devicesGatts.get(adr);
if(gatt == null){
gatt = device.connectGatt(this, true, mGattCallbacks);
} else {
BluetoothDevice gattDevice = gatt.getDevice();
if(gattDevice != null && adr.equals(gattDevice.getAddress())){
gatt.connect();
} else {
gatt = device.connectGatt(this, true, mGattCallbacks);
}
}
devicesGatts.put(adr, gatt);
return gatt;
}
, , BluetoothGatt, gatt.connect(), ( Fatal) DeadObjectException.
, Bluetooth, .
, , , , , .
:
02-18 10:43:51.884: E/BluetoothGatt(23312): android.os.DeadObjectException
02-18 10:43:51.884: E/BluetoothGatt(23312): at android.os.BinderProxy.transact(Native Method)
02-18 10:43:51.884: E/BluetoothGatt(23312): at android.bluetooth.IBluetoothGatt$Stub$Proxy.clientConnect(IBluetoothGatt.java:841)
02-18 10:43:51.884: E/BluetoothGatt(23312): at android.bluetooth.BluetoothGatt.connect(BluetoothGatt.java:759)
02-18 10:43:51.884: E/BluetoothGatt(23312): at MYSERVICE.connectGatt(...)
, Bluetooth.
?