node-ble-host
node-ble-host copied to clipboard
Create BLE Beacon
Is there a way to advertise a BLE Beacon with this lib? @Emill
Thanks in advance!
Sure.
If you follow the peripheral example at the readme page, modify the part that uses the AdvertisingDataBuilder to create advertising data that matches your desired beacon format. See https://github.com/Emill/node-ble-host/blob/master/docs/api/advertising-data-builder.md for documentation.
If you do not want your device to be connectable (i.e. it should only advertise), you can add advertisingType: 'ADV_NONCONN_IND'
or advertisingType: 'ADV_SCAN_IND'
to the advertising options, depending on if you need scan response data or not.
OK - thank you!
Do you have an example for an beacon? It is not important which type (iBeacon, AltBeacon,...)
I try to create something like that, but I am not able to create it!
Could you post a screenshot of what pops up when you press RAW?
So here is the beacon i actually need - with the raw data!
I think this code should work to replicate that beacon, by modifying "Full GATT Server Example (Peripheral)" accordingly:
const advDataBuffer = new AdvertisingDataBuilder()
.addFlags(['leGeneralDiscoverableMode', 'brEdrNotSupported'])
.addManufacturerData(0x004C, Buffer.from([ // Apple's manufacturer ID is 0x004C
0x02, // Type: Beacon
0x15, // Length of following data: 21
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // UUID
0x11, 0x22, // Major
0x33, 0x44, // Minor
0x100-77])); // RSSI at 1m
.build();
manager.setAdvertisingData(advDataBuffer);
const scanRspDataBuffer = new AdvertisingDataBuilder()
.addLocalName(/*isComplete*/ true, deviceName)
.build();
manager.setScanResponseData(scanRspDataBuffer);
startAdv();
function startAdv() {
manager.startAdvertising({advertisingType: 'ADV_SCAN_IND'}, connectCallback); // Make it non-connectable
}
Note that that RSSI value should be updated to match the TX power of your Bluetooth controller.