Peripheral is a GATT server that provides an indication to the android app(on a smoke detected characteristic) when it detects smoke. Usually the app logs the event to file, the user can click yes no to confirm if there was smoke, and that confirmation is written back to the Gatt server(by sendSmokeConf). However I find that if I leave the system running, the BluetoothGatt object dies. Using it throws the following exception(in this case triggered by the sendSmokeConf) :
2023-05-28 01:56:03.217 13873-13873 BluetoothGatt com.example.smoke_check E
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1129)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.writeCharacteristic(IBluetoothGatt.java:1520)
at android.bluetooth.BluetoothGatt.writeCharacteristic(BluetoothGatt.java:1205)
at com.example.smoke_check.ESP32ConnService.sendSmokeConf(ESP32ConnService.kt:341)
at com.example.smoke_check.YesNoActivity.onCreate$lambda$0(YesNoActivity.kt:38)
at com.example.smoke_check.YesNoActivity.$r8$lambda$MDfHIsiGzV4ibgC068x9iixK2rs(Unknown Source:0)
at com.example.smoke_check.YesNoActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:6599)
at android.view.View.performClickInternal(View.java:6576)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25908)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I have tried wrapping the calls to BluetoothGatt object with try catch(catch error, catch exception), however the exception cannot be even caught. If it could be, I would try to enable and then disable the bluetooth adapter. I am also looking for guidance why the Android OS kills the BluetoothGatt, and is there any native development method to fix this.