laptou / bluez-rs

Control Bluetooth on Linux.
MIT License
39 stars 9 forks source link

Are GATT services and characteristics implemented? #22

Closed enochc closed 3 years ago

enochc commented 3 years ago

Once connected how do I interface with the GATT services and characteristics that are supported by the peripheral?

enochc commented 3 years ago

If not GATT, how can I just send some raw data that the Peripheral can receive and process properly?

laptou commented 3 years ago

To answer your question, no, this is not implemented.

Interacting with devices directly is not really part of BlueZ's management API, which is the only one that is currently implemented. In order to do what you're trying to do, you either need to open an L2CAP socket and communicate with the device through it, or use BlueZ's D-Bus API.

Someone has implemented this whole L2CAP business in C here, and it seems like these lines are a good place to start if you want to rewrite it in Rust. This website should also be helpful in figuring out how exactly to use these sockets, and this is the kernel header where the options structures are defined.

If you'd like to take a stab at implementing it, let me know. Otherwise, I'll have to have a look at this sometime in January.

enochc commented 3 years ago

Thank you so much for your detailed response, I'm still learning a lot about bluetooth. It would be great to have a working GATT implementation but for my short term goals all I really need is a socket connection where I can send and receive raw bytes. Does Bluez not provide a socket? I need a "This is how bluetooth works and how you can interface with it" guide for developers, but apparently such a thing either doesn't exist or is very hard to find (not to mention os and hardware specific).

laptou commented 3 years ago

Yeah, Bluetooth is tricky. You communicate with devices using an RFCOMM or L2CAP socket (more information about this in the Bluetooth Core specification, Vol. 3, Part A), which you can open like so in Linux.

For a Rust example that parallels that C code, look at ManagementSocket::open(). This code is for opening an HCI management socket, which is used to access BlueZ's management API, but you can easily modify this code to open an L2CAP socket instead.

If you can, I would recommend getting your hands on Bluetooth Essentials for Programmers by Huang and Rudolph. It's like the MIT CSAIL website I linked, but with a lot more detail.