-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstartAdv.js
58 lines (46 loc) · 1.75 KB
/
startAdv.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
var btle = require('./lib/btle');
var characteristic = require('./lib/characteristic');
var gatt = require('./lib/gatt');
var Peripheral = require('./lib/peripheral');
var service = require('./lib/service');
var util = require('util');
btle.setDebug(true);
// Create the services.
var services = [];
// Device Info service
services.push(gatt.createDeviceInfoService('My MFG', '0', '0000-0000', 'v0.01', 'v0.01', 'v0.01', 'My BLE Device'));
// Custom service
var characteristics = [];
// One read/write characteristic
var c = characteristic.create(null, characteristic.Properties.READ | characteristic.Properties.WRITE, 0xAA01, 0);
// Called whenever the characteristic value is written
c.on('write', function(c) {
console.log("Characteristic %s written, new value = %s", c.uuid.toString(), util.inspect(c.value));
});
characteristics.push(c);
// Create the custom service
services.push(service.create(null, 0xAA00, null, characteristics, peripheral));
var peripheral = Peripheral.create('My BLE Device', services);
// Connection handler
peripheral.on('connect', function(central) {
console.log("Got connection!!!");
peripheral.stopAdvertising();
});
peripheral.on('error', function(err) {
console.log(err);
advertiseAndListen();
});
function advertiseAndListen() {
// Start advertising
var advOptions = {flags: Peripheral.AdvertisementFlags.LIMITED_DISCOVERABLE | Peripheral.AdvertisementFlags.BR_EDR_NOT_SUPPORTED,
completeName: 'foo'};
peripheral.advertise(advOptions, advOptions);
// Listen for incoming connections
peripheral.listen();
}
// When the central closes the connection, start advertising again
peripheral.on('close', function() {
console.log('Got close event, readvertising');
advertiseAndListen();
});
advertiseAndListen();