-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBleApplication.java
More file actions
304 lines (259 loc) · 9.77 KB
/
BleApplication.java
File metadata and controls
304 lines (259 loc) · 9.77 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package it.tangodev.ble;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import it.tangodev.utils.BleAdapter;
import org.bluez.GattApplication1;
import org.bluez.GattManager1;
import org.bluez.LEAdvertisingManager1;
import org.dbus.ObjectManager;
import org.dbus.InterfacesAddedSignal.InterfacesAdded;
import org.dbus.InterfacesRomovedSignal.InterfacesRemoved;
import org.freedesktop.DBus;
import org.freedesktop.DBus.Properties;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.Path;
import org.freedesktop.dbus.Variant;
import org.freedesktop.dbus.exceptions.DBusException;
/**
* BleApplication class is the starting point of the entire Peripheral service's structure.
* It is responsible of the service's publishment and and the advertisement.
* @author Tongo
*
*/
public class BleApplication implements GattApplication1 {
public static final String DBUS_BUSNAME = "org.freedesktop.DBus";
public static final String BLUEZ_DBUS_BUSNAME = "org.bluez";
public static final String BLUEZ_DEVICE_INTERFACE = "org.bluez.Device1";
public static final String BLUEZ_ADAPTER_INTERFACE = "org.bluez.Adapter1";
public static final String BLUEZ_GATT_INTERFACE = "org.bluez.GattManager1";
public static final String BLUEZ_LE_ADV_INTERFACE = "org.bluez.LEAdvertisingManager1";
public static final String ADDRESS = "Address";
private List<BleService> servicesList = new ArrayList<BleService>();
private String path = null;
private BleAdapter bleAdapter;
private BleService advService;
private BleAdvertisement adv;
private String adapterAlias;
private boolean hasDeviceConnected = false;
private DBusSigHandler<InterfacesAdded> interfacesAddedSignalHandler;
private DBusSigHandler<InterfacesRemoved> interfacesRemovedSignalHandler;
private BleApplicationListener listener;
private DBusConnection dbusConnection;
/**
* In order to create a BleApplication you need to pass a path.
* The bluezero standard structure is:
* APPLICAZION
* SERVICE
* CHARACTERISTIC-1
* CHARACTERISTIC-2
*
* Since bluez 5.43, the advertisement is able to run only ONE service.
* @param path
*/
public BleApplication(String path, BleApplicationListener listener) {
this.path = path;
this.listener = listener;
String advPath = path + "/advertisement";
this.adv = new BleAdvertisement(BleAdvertisement.ADVERTISEMENT_TYPE_PERIPHERAL, advPath);
}
/**
* First of all the method power-on the adapter.
* Then publish the service with their characteristic and start the advertisement (only primary service can advertise).
* @throws DBusException
* @throws InterruptedException
*/
public void start() throws DBusException, InterruptedException {
this.dbusConnection = DBusConnection.getConnection(DBusConnection.SYSTEM);
bleAdapter = findAdapterPath();
if (bleAdapter == null) {
throw new RuntimeException("No BLE adapter found");
}
Properties adapterProperties = (Properties) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, bleAdapter.getPath(), Properties.class);
adapterProperties.Set(BLUEZ_ADAPTER_INTERFACE, "Powered", new Variant<Boolean>(true));
if(adapterAlias != null) {
adapterProperties.Set(BLUEZ_ADAPTER_INTERFACE, "Alias", new Variant<String>(adapterAlias));
}
GattManager1 gattManager = (GattManager1) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, bleAdapter.getPath(), GattManager1.class);
LEAdvertisingManager1 advManager = (LEAdvertisingManager1) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, bleAdapter.getPath(), LEAdvertisingManager1.class);
if (!adv.hasServices()) {
updateAdvertisement();
}
export();
Map<String, Variant> advOptions = new HashMap<String, Variant>();
advManager.RegisterAdvertisement(adv, advOptions);
Map<String, Variant> appOptions = new HashMap<String, Variant>();
gattManager.RegisterApplication(this, appOptions);
initInterfacesHandler();
}
/**
* Stop the advertisement and unpublish the service.
* @throws DBusException
* @throws InterruptedException
*/
public void stop() throws DBusException, InterruptedException {
if (bleAdapter == null) {
return;
}
GattManager1 gattManager = (GattManager1) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, bleAdapter.getPath(), GattManager1.class);
LEAdvertisingManager1 advManager = (LEAdvertisingManager1) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, bleAdapter.getPath(), LEAdvertisingManager1.class);
if (adv != null) {
advManager.UnregisterAdvertisement(adv);
}
gattManager.UnregisterApplication(this);
unexport();
dbusConnection.removeSigHandler(InterfacesAdded.class, interfacesAddedSignalHandler);
dbusConnection.removeSigHandler(InterfacesRemoved.class, interfacesRemovedSignalHandler);
dbusConnection.disconnect();
dbusConnection = null;
}
protected void initInterfacesHandler() throws DBusException {
DBus dbus = dbusConnection.getRemoteObject(DBUS_BUSNAME, "/or/freedesktop/DBus", DBus.class);
String bluezDbusBusName = dbus.GetNameOwner(BLUEZ_DBUS_BUSNAME);
ObjectManager bluezObjectManager = (ObjectManager) dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, "/", ObjectManager.class);
interfacesAddedSignalHandler = new DBusSigHandler<InterfacesAdded>() {
@Override
public void handle(InterfacesAdded signal) {
Map<String, Variant> iamap = signal.getInterfacesAdded().get(BLUEZ_DEVICE_INTERFACE);
if (iamap != null) {
Variant<String> address = iamap.get(ADDRESS);
String path = signal.getObjectPath().toString();
hasDeviceConnected = true;
if (listener != null) {
listener.deviceConnected(path, address.getValue());
}
}
}
};
interfacesRemovedSignalHandler = new DBusSigHandler<InterfacesRemoved>() {
@Override
public void handle(InterfacesRemoved signal) {
List<String> irlist = signal.getInterfacesRemoved();
for (String ir : irlist) {
if (BLUEZ_DEVICE_INTERFACE.equals(ir)) {
String path = signal.getObjectPath().toString();
hasDeviceConnected = false;
if (listener != null) {
listener.deviceDisconnected(path);
}
}
}
}
};
dbusConnection.addSigHandler(InterfacesAdded.class, bluezDbusBusName, bluezObjectManager, interfacesAddedSignalHandler);
dbusConnection.addSigHandler(InterfacesRemoved.class, bluezDbusBusName, bluezObjectManager, interfacesRemovedSignalHandler);
}
/**
* Set the alias name of the peripheral. This name is visible by the central that discover s peripheral.
* This must set before start to take effect.
* @param alias
*/
public void setAdapterAlias(String alias) {
adapterAlias = alias;
}
public void addService(BleService service) {
this.servicesList.add(service);
}
public void removeService(BleService service) {
this.servicesList.remove(service);
}
public List<BleService> getServicesList() {
return servicesList;
}
public boolean hasDeviceConnected() {
return hasDeviceConnected;
}
public BleAdvertisement getAdvertisement() {
return adv;
}
/**
* Search for a Adapter that has GattManager1 and LEAdvertisement1 interfaces, otherwise return null.
* @return BleAdapter based on the map stored in the D-Bus Managed object org.bluez.Adapter1
* @throws DBusException if there is an error communicating with BlueZ over D-Bus
*/
public BleAdapter findAdapterPath() throws DBusException {
ObjectManager bluezObjectManager = dbusConnection.getRemoteObject(BLUEZ_DBUS_BUSNAME, "/", ObjectManager.class);
if (bluezObjectManager == null) {
return null;
}
Map<Path, Map<String, Map<String, Variant>>> bluezManagedObject = bluezObjectManager.GetManagedObjects();
if (bluezManagedObject == null) {
return null;
}
for (Path path : bluezManagedObject.keySet()) {
Map<String, Map<String, Variant>> value = bluezManagedObject.get(path);
boolean hasGattManager = false;
boolean hasAdvManager = false;
for (Map.Entry<String, Map<String, Variant>> entry : value.entrySet()) {
if (entry.getKey().equals(BLUEZ_GATT_INTERFACE)) {
hasGattManager = true;
}
if (entry.getKey().equals(BLUEZ_LE_ADV_INTERFACE)) {
hasAdvManager = true;
}
if (hasGattManager && hasAdvManager) {
return new BleAdapter(path, value.get("org.bluez.Adapter1"));
}
}
}
return null;
}
/**
* Export the application in Dbus system.
* @throws DBusException
*/
private void export() throws DBusException {
if (adv != null) {
adv.export(dbusConnection);
}
for (BleService service : servicesList) {
service.export(dbusConnection);
}
dbusConnection.exportObject(path, this);
}
/**
* Unexport the application in Dbus system.
* @throws DBusException
*/
private void unexport() throws DBusException {
if (adv != null) {
adv.unexport(dbusConnection);
}
for (BleService service : servicesList) {
service.unexport(dbusConnection);
}
dbusConnection.unExportObject(path);
}
@Override
public boolean isRemote() {
return false;
}
@Override
public Map<Path, Map<String, Map<String, Variant>>> GetManagedObjects() {
System.out.println("Application -> GetManagedObjects");
Map<Path, Map<String, Map<String, Variant>>> response = new HashMap<Path, Map<String, Map<String, Variant>>>();
for (BleService service : servicesList) {
response.put(service.getPath(), service.getProperties());
for (BleCharacteristic characteristic : service.getCharacteristics()) {
response.put(characteristic.getPath(), characteristic.getProperties());
// TODO foreach Description in Characteristic
}
}
System.out.println(response);
return response;
}
// add primary service uuids to advertisement
private void updateAdvertisement() {
for (BleService service : servicesList) {
if(service.isPrimary()) {
adv.addService(service);
break;
}
}
}
public BleAdapter getBleAdapter() {
return bleAdapter;
}
}