LightPeripheral.java
7.47 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
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.bluetooth.light;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import com.telink.bluetooth.Peripheral;
import com.telink.util.Strings;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class LightPeripheral extends Peripheral {
public static final String ADV_MESH_NAME = "com.telink.bluetooth.light.ADV_MESH_NAME";
public static final String ADV_MESH_ADDRESS = "com.telink.bluetooth.light.ADV_MESH_ADDRESS";
public static final String ADV_MESH_UUID = "com.telink.bluetooth.light.ADV_MESH_UUID";
public static final String ADV_PRODUCT_UUID = "com.telink.bluetooth.light.ADV_PRODUCT_UUID";
public static final String ADV_STATUS = "com.telink.bluetooth.light.ADV_STATUS";
protected final Map<String, Object> advProperties = new HashMap<>();
protected final Map<UUID, byte[]> characteristicsValue = new HashMap<>();
public boolean meshChanged;
private byte[] meshName;
private byte[] password;
private byte[] longTermKey;
private int meshAddress;
private Callback mCallback;
private String meshNameStr;
private int newMeshAddress = -1;
public LightPeripheral(BluetoothDevice device, byte[] scanRecord, int rssi,
byte[] meshName, int meshAddress) {
super(device, scanRecord, rssi);
this.setMeshName(meshName);
this.setMeshAddress(meshAddress);
}
public byte[] getMeshName() {
return this.meshName;
}
public void setMeshName(String value) {
this.meshNameStr = value;
}
public void setMeshName(byte[] value) {
this.meshNameStr = Strings.bytesToString(value);
this.meshName = value;
}
public String getMeshNameStr() {
return this.meshNameStr;
}
public byte[] getPassword() {
return password;
}
public void setPassword(byte[] password) {
this.password = password;
}
public byte[] getLongTermKey() {
return this.longTermKey;
}
public void setLongTermKey(byte[] value) {
this.longTermKey = value;
}
public int getMeshAddress() {
return meshAddress;
}
public void setMeshAddress(int value) {
this.meshAddress = value;
this.newMeshAddress = value;
}
public int getNewMeshAddress() {
return newMeshAddress;
}
public void setNewMeshAddress(int newMeshAddress) {
this.newMeshAddress = newMeshAddress;
}
public int getMeshUUID() {
return this.getAdvPropertyAsInt(ADV_MESH_UUID);
}
public int getProductUUID() {
return this.getAdvPropertyAsInt(ADV_PRODUCT_UUID);
}
public int getStatus() {
return this.getAdvPropertyAsInt(ADV_STATUS);
}
public void putAdvProperty(String key, Object value) {
this.advProperties.put(key, value);
}
public Object getAdvProperty(String key) {
return this.advProperties.get(key);
}
public String getAdvPropertyAsString(String key) {
return (String) this.advProperties.get(key);
}
public int getAdvPropertyAsInt(String key) {
return (int) this.advProperties.get(key);
}
public long getAdvPropertyAsLong(String key) {
return (long) this.advProperties.get(key);
}
public byte[] getAdvPropertyAsBytes(String key) {
return (byte[]) this.advProperties.get(key);
}
public void putCharacteristicValue(UUID characteristicUUID, byte[] value) {
this.characteristicsValue.put(characteristicUUID, value);
}
public byte[] getCharacteristicValue(UUID characteristicUUID) {
if (this.characteristicsValue.containsKey(characteristicUUID))
return this.characteristicsValue.get(characteristicUUID);
return null;
}
public String getCharacteristicValueAsString(UUID characteristicUUID) {
byte[] value = this.getCharacteristicValue(characteristicUUID);
return value != null ? new String(value) : null;
}
public String getFirmwareRevision() {
UUID characteristicUUID = UuidInformation.CHARACTERISTIC_FIRMWARE.getValue();
return this.getCharacteristicValueAsString(characteristicUUID);
}
public void connect(Context context, Callback callback) {
this.mCallback = callback;
super.connect(context);
}
@Override
public void disconnect() {
super.disconnect();
}
public BluetoothGattService findService(UUID serviceUUID) {
if (this.mServices == null || this.mServices.size() == 0)
return null;
BluetoothGattService mService = null;
for (BluetoothGattService service : mServices) {
if (service.getUuid().equals(serviceUUID)) {
mService = service;
break;
}
}
return mService;
}
public BluetoothGattCharacteristic findCharacteristic(UUID serviceUUID, UUID characteristicUUID) {
BluetoothGattService mService = this.findService(serviceUUID);
if (mService == null)
return null;
List<BluetoothGattCharacteristic> characteristics = mService.getCharacteristics();
BluetoothGattCharacteristic mCharacteristic = null;
for (BluetoothGattCharacteristic characteristic : characteristics) {
if (characteristic.getUuid().equals(characteristicUUID)) {
mCharacteristic = characteristic;
break;
}
}
return mCharacteristic;
}
@Override
protected void onConnect() {
super.onConnect();
if (this.mCallback != null)
this.mCallback.onConnect(this);
}
@Override
protected void onDisconnect() {
super.onDisconnect();
if (this.mCallback != null) {
this.mCallback.onDisconnect(this);
this.mCallback = null;
}
}
@Override
protected void onServicesDiscovered(List<BluetoothGattService> services) {
super.onServicesDiscovered(services);
if (this.mCallback != null)
this.mCallback.onServicesDiscovered(this, services);
}
@Override
protected void onNotify(byte[] data, UUID serviceUUID,
UUID characteristicUUID, Object tag) {
super.onNotify(data, serviceUUID, characteristicUUID, tag);
if (this.mCallback != null)
this.mCallback.onNotify(this, data, serviceUUID,
characteristicUUID, tag);
}
@Override
protected void onRssiChanged() {
super.onRssiChanged();
if (this.mCallback != null)
this.mCallback.onRssiChanged(this);
}
public interface Callback {
void onConnect(LightPeripheral light);
void onDisconnect(LightPeripheral light);
void onServicesDiscovered(LightPeripheral light,
List<BluetoothGattService> services);
void onNotify(LightPeripheral light, byte[] data,
UUID serviceUUID, UUID characteristicUUID, Object tag);
void onRssiChanged(LightPeripheral light);
}
}