Emill / node-ble-host

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

Setting PHY=2M #16

Open travisjayday opened 1 year ago

travisjayday commented 1 year ago

How can I set the PHY configuration to 2M? My connections are only 1M phy and I'm not sure how to update it. Is 2M PHY supported?

I'm looking into the source code and the only references to PHY are in node_modules/ble-host/lib/internal/adapter.js. Without documentation, it's hard to figure out what's going on.

Any insight from someone more experienced is greatly appreciated!

Emill commented 1 year ago

Currently the library is using the "legacy" HCI API which is guaranteed to be supported on all Bluetooth >= 4.0 controllers. The extended/enhanced API was introduced in Bluetooth 5.0 which added support for creating connections or advertising using other PHYs than 1M.

The adapter.js file contains the low level HCI serialiser/deserialiser functions as well as some callback infrastructure to easily issue HCI commands. The more high level ble-manager.js makes use of this functionality internally.

You can find the HCI specification in the Bluetooth Core specification, Vol 4 Part E. As you can see in section 3.1.1, a host must either use only legacy commands/events or only extended/enhanced commands/events, which is a bit annoying.

However, the LE Set PHY Command, which can be called on an already established connection, is not part of the above restrictions. You can thus use the leSetPhy method in adapter.js to send this command. To receive the complete callback, you need to make sure the mask sent into leSetEventMask has bit 11 set. You should then wrap this functionality in ble-manager.js in some way.

If you want to use another PHY already at the point a connection is created, much more work would be needed since that requires all legacy commands/events to be replaced with the extended versions.

travisjayday commented 1 year ago

Okay, thank you for that information, that all makes sense. For now, I'm getting enough throughput with 1M phy but in the future, I'll look into making those changes you mentioned (don't know much about HCI/BLE so I don't really want to go down that rabbit hole right now but I might have to later). Thanks so much for your detailed and fast answer and thank you for this great library (l2cap COC works great :))