/
index.js
77 lines (65 loc) · 2.34 KB
/
index.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
const USER_SERVICE_UUID = '1d9113b9-b790-46cb-a55a-2e666525e0c3';
const WRITE_CHARACTERISTIC_UUID = 'E9062E71-9E62-4BC6-B0D3-35CDCD9B027B';
const NOTIFY_CHARACTERISTIC_UUID = '62FBD229-6EDD-4D1A-B554-5C4E1BB29169';
const PSDI_SERVICE_UUID ='E625601E-9E55-4597-A598-76018A0D293D';
const PSDI_CHARACTERISTIC_UUID = '26E2B12B-85F0-4F3F-9FDD-91D114270E6E';
const DEVICE_NAME = 'line-things-device';
const bleno = require('bleno');
const onoff = require('onoff');
const PrimaryService = bleno.PrimaryService;
const Gpio = onoff.Gpio;
let onOff = 0;
console.log(`bleno - ${DEVICE_NAME}`);
const Characteristic = bleno.Characteristic;
const writeCharacteristic = new Characteristic({
uuid: WRITE_CHARACTERISTIC_UUID,
properties: ['write'],
onWriteRequest: (data, offset, withoutResponse, callback) => {
console.log(`onOff = ${data[0]}`)
var led = new Gpio(12,"out");
led.writeSync(data[0]);
callback(Characteristic.RESULT_SUCCESS);
}
});
const notifyCharacteristic = new Characteristic({
uuid: NOTIFY_CHARACTERISTIC_UUID,
properties: ['notify'],
onSubscribe: (maxSize, callback) => {
console.log('subscribe');
setInterval(function() {
onOff ^= 1;
callback(new Buffer([onOff]));
}, 2000);
}
});
const psdiCharacteristic = new Characteristic({
uuid: PSDI_CHARACTERISTIC_UUID,
properties: ['read'],
onReadRequest: (offset, callback) => {
console.log('PSDI read');
const result = Characteristic.RESULT_SUCCESS;
const data = new Buffer.from('PSDI read');
callback(result, data);
}
});
bleno.on('stateChange', (state) => {
console.log(`on -> stateChange: ${state}`);
if (state === 'poweredOn') {
bleno.startAdvertising(DEVICE_NAME, [USER_SERVICE_UUID]);
} else {
bleno.stopAdvertising();
}
});
bleno.on('advertisingStart', (error) => {
console.log(`on -> advertisingStart: ${(error ? 'error ' + error : 'success')}`);
if(error) return;
const userService = new PrimaryService({
uuid: USER_SERVICE_UUID,
characteristics: [writeCharacteristic,notifyCharacteristic]
});
const psdiService = new PrimaryService({
uuid: PSDI_SERVICE_UUID,
characteristics: [psdiCharacteristic]
});
bleno.setServices([userService,psdiService]);
});