Manufacture.java
9.54 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.bluetooth.light;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 厂商信息设置接口
*/
public final class Manufacture {
private static final Manufacture DEFAULT_MANUFACTURE = new Builder().build();
private static Manufacture definitionManufacture;
private final Map<String, UUID> uuidMap = new HashMap<>();
private String name;
private String version;
private String info;
private String factoryName;
private String factoryPassword;
private byte[] factoryLtk;
private int vendorId;
private int otaDelay;
private int otaSize;
private Manufacture(String name, String version, String info, String defaultMeshName, String defaultPassword, byte[] defaultLongTermKey, int vendorId, int otaDelay, int otaSize, UUID serviceUUID, UUID pairUUID, UUID commandUUID, UUID notifyUUID, UUID otaUUID) {
this.name = name;
this.version = version;
this.info = info;
this.factoryName = defaultMeshName;
this.factoryPassword = defaultPassword;
this.factoryLtk = Arrays.copyOf(defaultLongTermKey, 16);
this.vendorId = vendorId;
this.otaDelay = otaDelay;
this.otaSize = otaSize;
this.putUUID(UUIDType.SERVICE.getKey(), serviceUUID);
this.putUUID(UUIDType.PAIR.getKey(), pairUUID);
this.putUUID(UUIDType.COMMAND.getKey(), commandUUID);
this.putUUID(UUIDType.OTA.getKey(), otaUUID);
this.putUUID(UUIDType.NOTIFY.getKey(), notifyUUID);
}
/**
* 获取默认厂商,即Telink相关描述
*
* @return
*/
public static Manufacture getDefaultManufacture() {
return DEFAULT_MANUFACTURE;
}
/**
* 获取自定义的厂商
*
* @return
*/
public static Manufacture getDefinitionManufacture() {
synchronized (Manufacture.class) {
return definitionManufacture;
}
}
/**
* 设置自定义厂商
*
* @param manufacture
*/
public static void setManufacture(Manufacture manufacture) {
synchronized (Manufacture.class) {
definitionManufacture = manufacture;
}
}
/**
* 获取当前的厂商,如果不设置自定义厂商则为默认厂商
*
* @return
*/
public static Manufacture getDefault() {
synchronized (Manufacture.class) {
if (definitionManufacture == null)
return DEFAULT_MANUFACTURE;
}
return definitionManufacture;
}
/**
* 厂商名称
*
* @return
*/
public String getName() {
return name;
}
/**
* 版本信息
*
* @return
*/
public String getVersion() {
return version;
}
/**
* 描述信息
*
* @return
*/
public String getInfo() {
return info;
}
/**
* 设备的出厂名
*
* @return
*/
public String getFactoryName() {
return factoryName;
}
/**
* 设备的出厂密码
*
* @return
*/
public String getFactoryPassword() {
return factoryPassword;
}
/**
* 设备的出厂LTK
*
* @return
*/
public byte[] getFactoryLtk() {
return factoryLtk;
}
/**
* 厂商Id
*
* @return
*/
public int getVendorId() {
return vendorId;
}
/**
* OTA数据包写入间隔时间
* <p>可以根据不同的手机设置此参数.
*
* @return
*/
public int getOtaDelay() {
return otaDelay;
}
public int getOtaSize() {
return otaSize;
}
public UUID getUUID(UUIDType uuidType) {
return this.getUUID(uuidType.getKey());
}
public UUID getUUID(String key) {
UUID result = null;
synchronized (this.uuidMap) {
if (this.uuidMap.containsKey(key))
result = this.uuidMap.get(key);
}
return result;
}
public void putUUID(String key, UUID value) {
synchronized (this.uuidMap) {
if (!this.uuidMap.containsKey(key))
this.uuidMap.put(key, value);
}
}
public enum UUIDType {
SERVICE("SERVICE_UUID"), PAIR("PAIR_UUID"), COMMAND("COMMAND_UUID"), OTA("OTA_UUID"), NOTIFY("NOTIFY_UUID");
private final String key;
UUIDType(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
public static final class Builder {
private String name = "telink";
private String version = "1.0";
private String info = "TELINK SEMICONDUCTOR (Shanghai) CO, LTD is a fabless IC design company";
private String factoryName = "telink_mesh1";
private String factoryPassword = "123";
private byte[] factoryLtk = new byte[]{
(byte) 0xC0, (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4,
(byte) 0xC5, (byte) 0xC6, (byte) 0xC7, (byte) 0xD8, (byte) 0xD9,
(byte) 0xDA, (byte) 0xDB, (byte) 0xDC, (byte) 0xDD, (byte) 0xDE,
(byte) 0xDF};
private int vendorId = 0x1102;
private UUID serviceUUID = UuidInformation.TELINK_SERVICE.getValue();
private UUID pairUUID = UuidInformation.TELINK_CHARACTERISTIC_PAIR.getValue();
private UUID commandUUID = UuidInformation.TELINK_CHARACTERISTIC_COMMAND.getValue();
private UUID notifyUUID = UuidInformation.TELINK_CHARACTERISTIC_NOTIFY.getValue();
private UUID otaUUID = UuidInformation.TELINK_CHARACTERISTIC_OTA.getValue();
private int otaDelay = 0;
private int otaSize = 128;
public Builder() {
}
/**
* 设置厂商名称
*
* @param name
* @return
*/
public Builder setName(String name) {
this.name = name;
return this;
}
/**
* 设置版本信息
*
* @param version
* @return
*/
public Builder setVersion(String version) {
this.version = version;
return this;
}
/**
* 设置厂商描述信息
*
* @param info
* @return
*/
public Builder setInfo(String info) {
this.info = info;
return this;
}
/**
* 设置出厂名
*
* @param factoryName
* @return
*/
public Builder setFactoryName(String factoryName) {
this.factoryName = factoryName;
return this;
}
/**
* 设置出厂密码
*
* @param factoryPassword
* @return
*/
public Builder setFactoryPassword(String factoryPassword) {
this.factoryPassword = factoryPassword;
return this;
}
/**
* 设置出厂LTK
*
* @param factoryLtk
* @return
*/
public Builder setFactoryLtk(byte[] factoryLtk) {
this.factoryLtk = factoryLtk;
return this;
}
/**
* 设置厂商标识
*
* @param vendorId
* @return
*/
public Builder setVendorId(int vendorId) {
this.vendorId = vendorId;
return this;
}
public Builder setOtaDelay(int otaDelay) {
this.otaDelay = otaDelay;
return this;
}
public Builder setOtaSize(int otaSize) {
this.otaSize = otaSize;
return this;
}
/**
* 设置设备的ServiceUUID
*
* @param serviceUUID
* @return
*/
public Builder setServiceUUID(UUID serviceUUID) {
this.serviceUUID = serviceUUID;
return this;
}
/**
* 设置配对用的UUID
*
* @param pairUUID
* @return
*/
public Builder setPairUUID(UUID pairUUID) {
this.pairUUID = pairUUID;
return this;
}
/**
* 设置发送命令用的UUID
*
* @param commandUUID
* @return
*/
public Builder setCommandUUID(UUID commandUUID) {
this.commandUUID = commandUUID;
return this;
}
/**
* 设置Notification用的UUID
*
* @param notifyUUID
* @return
*/
public Builder setNotifyUUID(UUID notifyUUID) {
this.notifyUUID = notifyUUID;
return this;
}
/**
* 设置OTA用的UUID
*
* @param otaUUID
* @return
*/
public Builder setOtaUUID(UUID otaUUID) {
this.otaUUID = otaUUID;
return this;
}
public Manufacture build() {
return new Manufacture(this.name, this.version, this.info, this.factoryName, this.factoryPassword, this.factoryLtk, this.vendorId, this.otaDelay, this.otaSize, this.serviceUUID, this.pairUUID, this.commandUUID, this.notifyUUID, this.otaUUID);
}
}
}