Emill / node-ble-host

A Bluetooth Low Energy host implementation for Node.js
ISC License
52 stars 15 forks source link

Device name longer than eight characters will throw an exception #1

Closed koleto closed 3 years ago

koleto commented 3 years ago

Thank you very much for sharing this!

I know that BLE advertising data is limited to 31 bytes, and I am not sure if it is a bug, but by extending the device name from the original example by one character fromMyDevice to MyDevice1 will throw an exception:

   // in server.js

...
const AdvertisingDataBuilder = NodeBleHost.AdvertisingDataBuilder;
const HciErrors = NodeBleHost.HciErrors;
const AttErrors = NodeBleHost.AttErrors;

const deviceName = 'MyDevice1';
...

 sudo node server.js 
/home/debian/bleriid2/node_modules/ble-host/lib/advertising-data-builder.js:30
            throw new Error('This item does not fit, needs ' + (2 + buf.length) + ' bytes but have only ' + (31 - data.length) + ' left');
                  ^

Error: This item does not fit, needs 18 bytes but have only 17 left
    at addData (/home/debian/bleriid2/node_modules/ble-host/lib/advertising-data-builder.js:30:10)
    at AdvertisingDataBuilder.add128BitServiceUUIDs (/home/debian/bleriid2/node_modules/ble-host/lib/advertising-data-builder.js:66:3)
    at /home/debian/bleriid2/server.js:68:30
    at /home/debian/bleriid2/node_modules/ble-host/lib/ble-manager.js:2356:17
    at cont (/home/debian/bleriid2/node_modules/ble-host/lib/ble-manager.js:491:5)
    at /home/debian/bleriid2/node_modules/ble-host/lib/ble-manager.js:478:6
    at Object.callback (/home/debian/bleriid2/node_modules/ble-host/lib/internal/adapter.js:616:5)
    at HciSocket.onData (/home/debian/bleriid2/node_modules/ble-host/lib/internal/adapter.js:1174:10)
    at HciSocket.emit (events.js:315:20)
    at /home/debian/bleriid2/node_modules/hci-socket/lib/hci-socket.js:105:9
Emill commented 3 years ago

Hi and thanks for finding my library!

The error is correct. If you have a 128-bit service uuid and flags, you can only fit a 8 byte long name. You could for example put the name in the scan response data if you want a longer name.

koleto commented 3 years ago

@Emill thanks!