@@ -79,6 +79,10 @@ public static class BatteryLevelEvent {
7979 public int battery = 0 ;
8080 }
8181
82+ public final void readCharacteristics () {
83+ readCharacteristic (batteryCharacteristic ).with (((device , data ) -> setBatteryLevel (data ))).enqueue ();
84+ }
85+
8286 private abstract class AsteroidBleManagerGattCallback extends BleManagerGattCallback {
8387
8488 /* It is a constraint of the Bluetooth library that it is required to initialize
@@ -119,7 +123,7 @@ public final boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gat
119123 BluetoothGattCharacteristic characteristic1 = bluetoothGattService .getCharacteristic (characteristic );
120124 removeNotificationCallback (characteristic1 );
121125 setNotificationCallback (characteristic1 ).with ((device , data ) -> callback .call (data .getValue ()));
122- enableNotifications (characteristic1 ).with (( device , data ) -> callback . call ( data . getValue ())). enqueue ();
126+ enableNotifications (characteristic1 ).enqueue ();
123127 });
124128 }
125129
@@ -138,7 +142,14 @@ protected final void initialize() {
138142 .enqueue ();
139143
140144 setNotificationCallback (batteryCharacteristic ).with (((device , data ) -> setBatteryLevel (data )));
141- readCharacteristic (batteryCharacteristic ).with (((device , data ) -> setBatteryLevel (data ))).enqueue ();
145+ // Do not call readCharacteristic(batteryCharacteristic) here.
146+ // Otherwise, on Android 12 and later, the BLE bond to the watch
147+ // is lost, and communication no longer works. Arguably, reading
148+ // and writing should not be done in initialize(), since it is
149+ // part of the BLE manager's connect() request. Instead, do those
150+ // IO operations _after_ that request finishes (-> read the
151+ // characteristic in the SynchronizationService class, which is
152+ // where the BLE manager's connect() function is called).
142153 enableNotifications (batteryCharacteristic ).enqueue ();
143154
144155 // Let all services know that we are connected.
0 commit comments