This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentral.js
73 lines (59 loc) · 2.25 KB
/
central.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
const noble = require('@abandonware/noble');
const UB_UUID = '00756c74-7261-6c69-6768-74206265616d';
const FORWARD_UUID = '00000000-0000-0000-0000-000000000001';
function start(state) {
console.log('UB central')
noble.on('stateChange', state => {
if (state === 'poweredOn') {
console.log('Scanning...');
noble.startScanning([UB_UUID]);
} else {
noble.stopScanning();
}
});
noble.on('discover', peripheral => {
// connect to the first peripheral that is scanned
//noble.stopScanning();
const name = peripheral.advertisement.localName;
console.log(`Connecting to ${name} ${peripheral.id}...`);
connectAndSetUp(peripheral);
});
function connectAndSetUp(peripheral) {
peripheral.connect(error => {
if (error) console.error({error});
state.peripherals[id] = peripheral;
console.log('Connected to :: ', peripheral.id);
// specify the services and characteristics to discover
const serviceUUIDs = [UB_UUID];
const characteristicUUIDs = [FORWARD_UUID];
peripheral.discoverSomeServicesAndCharacteristics(
serviceUUIDs,
characteristicUUIDs,
function (error, services, characteristics) { // onDiscover
console.log('Discovered services and characteristics');
const forwardCharacteristic = characteristics.find(c => c.uuid === FORWARD_UUID);
if (!forwardCharacteristic) return
peripheral.forwardCharacteristic = forwardCharacteristic;
// data callback receives notifications
forwardCharacteristic.on('data', (data, isNotification) => {
console.log(`Received :: ${data}`);
forwardToCentral(state, data);
});
// subscribe to be notified whenever the peripheral update the characteristic
forwardCharacteristic.subscribe(error => {
if (error) {
console.error('Error subscribing to forwardCharacteristic!');
} else {
console.log('Subscribed for forwardCharacteristic notifications!');
}
});
}
);
});
peripheral.on('disconnect', () => {
console.log('Disconnected!')
delete state.peripherals[peripheral.id]
});
}
}
module.exports = start;