Skip to content

Commit d6f6cc5

Browse files
dv1FlorentRevest
authored andcommitted
Fix BLE connection setup on Android 12+
1 parent c993100 commit d6f6cc5

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

app/src/main/java/org/asteroidos/sync/asteroid/AsteroidBleManager.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

app/src/main/java/org/asteroidos/sync/services/SynchronizationService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ final void handleConnect() {
102102
device.createBond();
103103
mBleMngr.connect(device)
104104
.useAutoConnect(true)
105-
.timeout(100000)
105+
.timeout(100 * 1000)
106106
.retry(3, 200)
107-
.done(device1 -> Log.d(TAG, "Connected to " + device1.getName()))
107+
.done(device1 -> {
108+
Log.d(TAG, "Connected to " + device1.getName());
109+
// Now we read the current values of the GATT characteristics,
110+
// _after_ the connection has been fully established, to avoid
111+
// connection failures on Android 12 and later.
112+
mBleMngr.readCharacteristics();
113+
})
108114
.fail((device2, error) -> Log.e(TAG, "Failed to connect to " + device.getName() +
109115
" with error code: " + error))
110116
.enqueue();

0 commit comments

Comments
 (0)