-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathAsteroidBleManager.java
171 lines (146 loc) · 7.38 KB
/
AsteroidBleManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* AsteroidOSSync
* Copyright (c) 2023 AsteroidOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.asteroidos.sync.asteroid;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.asteroidos.sync.connectivity.IConnectivityService;
import org.asteroidos.sync.connectivity.IServiceCallback;
import org.asteroidos.sync.services.SynchronizationService;
import org.asteroidos.sync.utils.AsteroidUUIDS;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import no.nordicsemi.android.ble.BleManager;
import no.nordicsemi.android.ble.data.Data;
public class AsteroidBleManager extends BleManager {
public static final String TAG = AsteroidBleManager.class.toString();
@Nullable
public BluetoothGattCharacteristic batteryCharacteristic;
final SynchronizationService mSynchronizationService;
final ArrayList<BluetoothGattService> mGattServices;
public final HashMap<UUID, IServiceCallback> recvCallbacks;
public HashMap<UUID, BluetoothGattCharacteristic> sendingCharacteristics;
public AsteroidBleManager(@NonNull final Context context, SynchronizationService syncService) {
super(context);
mSynchronizationService = syncService;
mGattServices = new ArrayList<>();
recvCallbacks = new HashMap<>();
}
public final void send(UUID characteristic, byte[] data) {
writeCharacteristic(sendingCharacteristics.get(characteristic), data,
Objects.requireNonNull(sendingCharacteristics.get(characteristic)).getWriteType()).split().enqueue();
}
@NonNull
@Override
protected final BleManagerGattCallback getGattCallback() {
return new AsteroidBleManagerGattCallback() {
@Override
protected void onServicesInvalidated() {
mSynchronizationService.unsyncServices();
batteryCharacteristic = null;
mGattServices.clear();
}
};
}
public final void abort() {
cancelQueue();
}
public final void setBatteryLevel(Data data) {
BatteryLevelEvent batteryLevelEvent = new BatteryLevelEvent();
batteryLevelEvent.battery = Objects.requireNonNull(data.getByte(0)).intValue();
mSynchronizationService.handleUpdateBatteryPercentage(batteryLevelEvent);
}
public static class BatteryLevelEvent {
public int battery = 0;
}
public final void readCharacteristics() {
readCharacteristic(batteryCharacteristic).with(((device, data) -> setBatteryLevel(data))).enqueue();
}
private abstract class AsteroidBleManagerGattCallback extends BleManagerGattCallback {
/* It is a constraint of the Bluetooth library that it is required to initialize
the characteristics in the isRequiredServiceSupported() function. */
@Override
public final boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
final BluetoothGattService batteryService = gatt.getService(AsteroidUUIDS.BATTERY_SERVICE_UUID);
boolean notify = false;
if (batteryService != null) {
batteryCharacteristic = batteryService.getCharacteristic(AsteroidUUIDS.BATTERY_UUID);
if (batteryCharacteristic != null) {
final int properties = batteryCharacteristic.getProperties();
notify = (properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
}
}
if (sendingCharacteristics == null)
sendingCharacteristics = new HashMap<>();
for (IConnectivityService service : mSynchronizationService.getServices().values()) {
BluetoothGattService bluetoothGattService = gatt.getService(service.getServiceUUID());
List<UUID> sendUuids = new ArrayList<>();
service.getCharacteristicUUIDs().forEach((uuid, direction) -> {
if (direction == IConnectivityService.Direction.TO_WATCH)
sendUuids.add(uuid);
});
Log.d(TAG, "UUID " + sendUuids);
for (UUID uuid : sendUuids) {
BluetoothGattCharacteristic characteristic = bluetoothGattService.getCharacteristic(uuid);
sendingCharacteristics.put(uuid, characteristic);
bluetoothGattService.addCharacteristic(characteristic);
}
recvCallbacks.forEach((characteristic, callback) -> {
BluetoothGattCharacteristic characteristic1 = bluetoothGattService.getCharacteristic(characteristic);
removeNotificationCallback(characteristic1);
setNotificationCallback(characteristic1).with((device, data) -> callback.call(data.getValue()));
enableNotifications(characteristic1).enqueue();
});
}
return (batteryCharacteristic != null && notify);
}
@Override
protected final void initialize() {
beginAtomicRequestQueue()
.add(requestMtu(256) // Remember, GATT needs 3 bytes extra. This will allow packet size of 244 bytes.
.with((device, mtu) -> log(Log.INFO, "MTU set to " + mtu))
.fail((device, status) -> log(Log.WARN, "Requested MTU not supported: " + status)))
.done(device -> log(Log.INFO, "Target initialized"))
.fail((device, status) -> Log.e("Init", device.getAddress() + " not initialized with error: " + status))
.enqueue();
setNotificationCallback(batteryCharacteristic).with(((device, data) -> setBatteryLevel(data)));
// Do not call readCharacteristic(batteryCharacteristic) here.
// Otherwise, on Android 12 and later, the BLE bond to the watch
// is lost, and communication no longer works. Arguably, reading
// and writing should not be done in initialize(), since it is
// part of the BLE manager's connect() request. Instead, do those
// IO operations _after_ that request finishes (-> read the
// characteristic in the SynchronizationService class, which is
// where the BLE manager's connect() function is called).
enableNotifications(batteryCharacteristic).enqueue();
// Let all services know that we are connected.
try {
mSynchronizationService.syncServices();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}