My gattServer advertise some data with paired bluetooth device and I run this gattServer with service.Everthing is well with Bluetooth state on but I turned off bluetooth and on again throw exception this line
sGattServer.notifyCharacteristicChanged(device, getCharacteristic(Constants.NOTIFICATION_SOURCE), false);
This is my connection Method
BluetoothAdapter bleAdapter = ((BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE)).getAdapter();
final Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();
for (BluetoothDevice d : pairedDevices) {
d.connectGatt(context, true, new BluetoothGattCallback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onConnectionStateChange(BluetoothGatt
gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
gatt.getServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
if (gatt !=null){
gatt.close();
gatt.disconnect();
gatt.connect();
}
break;
}
}
});
}
Stuck trace here:
10-23 10:04:53.978 27768-27768/E/BluetoothGattServer: android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.sendNotification(IBluetoothGatt.java:1482)
at android.bluetooth.BluetoothGattServer.notifyCharacteristicChanged(BluetoothGattServer.java:539)
When Bluetooth is turned off, Android restarts the Bluetooth stack to clean up its state. Kind of like cracking a walnut with a 40lb sledge hammer. See the Logcat
In your GattServer Service you need to recreate the BluetoothGattServer object when the Bluetooth is powered back on.
You did not show the code from your service, which is where the problem lies, but you will need to do something like the following. Create a method createServerGattService which defines the service UUID and characteristics of you GATT server service, then registers it with the BLE stack. You already have this because you say the GATT server works fine until you turn the Bluetooth adapter off snd on.
Add a Bluetooth adapter power state receiver to your service:
In the onCreate() method of your service, register the receiver and instantiate your gatt server if the Bluetooth adapter is powered on:
In the onDestroy() method of your service, remove the receiver and close the GATT server connection:
For completeness sake of the answer, the createServerGattService() should look something like this: