node-ble-host icon indicating copy to clipboard operation
node-ble-host copied to clipboard

Create BLE Beacon

Open sialalex opened this issue 3 years ago • 6 comments

Is there a way to advertise a BLE Beacon with this lib? @Emill

Thanks in advance!

sialalex avatar Feb 11 '22 09:02 sialalex

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.

Emill avatar Feb 15 '22 10:02 Emill

OK - thank you!

Do you have an example for an beacon? It is not important which type (iBeacon, AltBeacon,...)

sialalex avatar Feb 15 '22 10:02 sialalex

I try to create something like that, but I am not able to create it!

image

sialalex avatar Feb 18 '22 09:02 sialalex

Could you post a screenshot of what pops up when you press RAW?

Emill avatar Feb 18 '22 09:02 Emill

So here is the beacon i actually need - with the raw data!

MicrosoftTeams-image (3) MicrosoftTeams-image (4) MicrosoftTeams-image (2)

sialalex avatar Feb 18 '22 10:02 sialalex

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.

Emill avatar Feb 18 '22 11:02 Emill