|
| 1 | +const HciSocket = require('hci-socket'); |
| 2 | +const NodeBleHost = require('ble-host'); |
| 3 | +const BleManager = NodeBleHost.BleManager; |
| 4 | +const AdvertisingDataBuilder = NodeBleHost.AdvertisingDataBuilder; |
| 5 | +const HciErrors = NodeBleHost.HciErrors; |
| 6 | +const AttErrors = NodeBleHost.AttErrors; |
| 7 | + |
| 8 | +const deviceName = 'MyDevice'; |
| 9 | + |
| 10 | +const transport = new HciSocket(); // connects to the first hci device on the computer, for example hci0 |
| 11 | + |
| 12 | +const options = { |
| 13 | + // optional properties go here |
| 14 | +}; |
| 15 | + |
| 16 | +BleManager.create(transport, options, function (err, manager) { |
| 17 | + // err is either null or an Error object |
| 18 | + // if err is null, manager contains a fully initialized BleManager object |
| 19 | + if (err) { |
| 20 | + console.error(err); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + let notificationCharacteristic; |
| 25 | + |
| 26 | + manager.gattDb.setDeviceName(deviceName); |
| 27 | + manager.gattDb.addServices([ |
| 28 | + { |
| 29 | + uuid: '22222222-3333-4444-5555-666666666666', |
| 30 | + characteristics: [ |
| 31 | + { |
| 32 | + uuid: '22222222-3333-4444-5555-666666666667', |
| 33 | + properties: ['read', 'write'], |
| 34 | + value: 'some default value' // could be a Buffer for a binary value |
| 35 | + }, |
| 36 | + { |
| 37 | + uuid: '22222222-3333-4444-5555-666666666668', |
| 38 | + properties: ['read'], |
| 39 | + onRead(connection, callback) { |
| 40 | + callback(AttErrors.SUCCESS, new Date().toString()); |
| 41 | + } |
| 42 | + }, |
| 43 | + { |
| 44 | + uuid: '22222222-3333-4444-5555-666666666669', |
| 45 | + properties: ['write'], |
| 46 | + onWrite(connection, needsResponse, value, callback) { |
| 47 | + console.log('A new value was written:', value); |
| 48 | + callback(AttErrors.SUCCESS); // actually only needs to be called when needsResponse is true |
| 49 | + } |
| 50 | + }, |
| 51 | + (notificationCharacteristic = { |
| 52 | + uuid: '22222222-3333-4444-5555-66666666666A', |
| 53 | + properties: ['notify'], |
| 54 | + onSubscriptionChange(connection, notification, indication, isWrite) { |
| 55 | + if (notification) { |
| 56 | + // Notifications are now enabled, so let's send something |
| 57 | + notificationCharacteristic.notify(connection, 'Sample notification'); |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + ] |
| 62 | + } |
| 63 | + ]); |
| 64 | + |
| 65 | + const advDataBuffer = new AdvertisingDataBuilder() |
| 66 | + .addFlags(['leGeneralDiscoverableMode', 'brEdrNotSupported']) |
| 67 | + .addLocalName(/*isComplete*/ true, deviceName) |
| 68 | + .add128BitServiceUUIDs(/*isComplete*/ true, ['22222222-3333-4444-5555-666666666666']) |
| 69 | + .build(); |
| 70 | + manager.setAdvertisingData(advDataBuffer); |
| 71 | + // call manager.setScanResponseData(...) if scan response data is desired too |
| 72 | + startAdv(); |
| 73 | + |
| 74 | + function startAdv() { |
| 75 | + manager.startAdvertising( |
| 76 | + { |
| 77 | + /*options*/ |
| 78 | + }, |
| 79 | + connectCallback |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + function connectCallback(status, conn) { |
| 84 | + if (status !== HciErrors.SUCCESS) { |
| 85 | + // Advertising could not be started for some controller-specific reason, try again after 10 seconds |
| 86 | + setTimeout(startAdv, 10000); |
| 87 | + return; |
| 88 | + } |
| 89 | + conn.on('disconnect', startAdv); // restart advertising after disconnect |
| 90 | + console.log('Connection established!', conn); |
| 91 | + } |
| 92 | +}); |
0 commit comments