-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline-things-server.js
79 lines (74 loc) · 1.83 KB
/
line-things-server.js
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
/* global trace */
import BLEServer from 'bleserver'
import { uuid } from 'btutils'
import { IOCapability } from 'sm'
import Hex from 'hex'
import getMacAddress from 'mac-address'
// import UUID from "uuid";
const DEVICE_NAME = 'M5Stack'
const SERVICE_UUID_LIST = ['91E4E176-D0B9-464D-9FE4-52EE3E9F1552']
const uuidList = [uuid(SERVICE_UUID_LIST)]
class LineThingsServer extends BLEServer {
onReady () {
this.deviceName = DEVICE_NAME
this.securityParameters = {
encryption: true,
mitm: false,
bonding: true,
ioCapability: IOCapability.NoInputNoOutput
}
this.onDisconnected()
}
onConnected (connection) {
this.onStatusChange('CONNECTED')
this.stopAdvertising()
}
onDisconnected (connection) {
this.onStatusChange('READY')
this.startAdvertising({
fast: true,
connectable: true,
discoverable: true,
scanResponseData: {
flags: 6,
completeName: DEVICE_NAME
},
advertisingData: {
flags: 6,
completeName: DEVICE_NAME,
completeUUID128List: uuidList
}
})
}
onCharacteristicNotifyEnabled (characteristic) {
if (characteristic.name === 'notify') {
this.notifyCharacteristic = characteristic
}
}
onCharacteristicNotifyDisabled (characteristic) {
if (characteristic.name === 'notify') {
this.notifyCharacteristic = null
}
}
onCharacteristicRead (params) {
trace(params.name)
if (params.name === 'value') {
const value = getMacAddress()
trace(Hex.toString(value))
return value
}
return 0
}
onCharacteristicWritten (params, value) {
if (params.name === 'write') {
this.onWritten(value)
}
}
onWritten (value) {
// noop, to be overridden
}
onStatusChange (status) {
// noop, to be overridden
}
}
export default LineThingsServer