Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
knowledge_base:programming:bluetooth [2022/12/02 14:45] – removed - external edit (Unknown date) 127.0.0.1 | knowledge_base:programming:bluetooth [2022/12/02 14:55] (current) – ↷ Links adapted because of a move operation George Wayne | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== AMA and Bluetooth Programming ====== | ||
+ | |||
+ | |||
+ | [[knowledge_base: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== AMA App initial query ===== | ||
+ | |||
+ | working advertising data (iOS): | ||
+ | < | ||
+ | mgmt# add-adv -g -d 030203fe171603fe7101010001000000000000000000000000000000 -s 0b09616d615f73616d706c65 1 | ||
+ | -g is general-discoverable flag | ||
+ | -u is uuid (big endien) if in -d, uuid and everything else should be expressed in small endien | ||
+ | -d is data starting with length, AD type (16 is service data type), uuid ... length, AD type, ... | ||
+ | -s is scan data starting with length, AD type (09 is full name) ... | ||
+ | </ | ||
+ | |||
+ | After Alexa App Setup: | ||
+ | < | ||
+ | Values written: | ||
+ | 10 00 05 08 14 a2 01 00 | ||
+ | alexa_tx_write_cb called | ||
+ | Values written: | ||
+ | 10 00 05 08 15 aa 01 00 | ||
+ | alexa_tx_write_cb called | ||
+ | Values written: | ||
+ | 10 00 05 08 5a d2 05 00 | ||
+ | alexa_tx_write_cb called | ||
+ | Values written: | ||
+ | 10 00 0b 08 32 92 03 06 10 a6 b0 f7 df 05 | ||
+ | </ | ||
+ | |||
+ | ===== Bluetooth Programming ===== | ||
+ | |||
+ | ==== BLE only on Rpi3 ==== | ||
+ | Run "sudo btmon" on a separate window to monitor HCI activities | ||
+ | |||
+ | < | ||
+ | $ sudo btmgmt | ||
+ | [mgmt]# info | ||
+ | Index list with 1 item | ||
+ | hci0: | ||
+ | addr B8: | ||
+ | supported settings: powered connectable fast-connectable discoverable bondable link-security ssp br/edr hs le advertising secure-conn debug-keys privacy static-addr | ||
+ | current settings: powered bondable ssp br/edr le secure-conn | ||
+ | name le_ama_sample_001 | ||
+ | short name | ||
+ | [mgmt]# power off | ||
+ | hci0 Set Powered complete, settings: bondable ssp br/edr le secure-conn | ||
+ | [mgmt]# bredr off | ||
+ | hci0 Set BR/EDR complete, settings: bondable le secure-conn | ||
+ | [mgmt]# connectable on | ||
+ | hci0 Set Connectable complete, settings: connectable bondable le secure-conn | ||
+ | [mgmt]# discov on | ||
+ | hci0 Set Discoverable complete, settings: connectable discoverable bondable le secure-conn | ||
+ | [mgmt]# io-cap 0x03 # | ||
+ | IO Capabilities successfully set | ||
+ | [mgmt]# power on | ||
+ | hci0 Set Powered complete, settings: powered connectable discoverable bondable le secure-conn | ||
+ | [mgmt]# advertising on | ||
+ | hci0 Set Advertising complete, settings: powered connectable discoverable bondable le advertising secure-conn # Current settings: 0x00000e1b | ||
+ | hci0 00: | ||
+ | hci0 00: | ||
+ | </ | ||
+ | |||
+ | sudo btmgmt advertising off\\ | ||
+ | |||
+ | Then use your own custom ad and scan payload like so:\\ | ||
+ | |||
+ | sudo btmgmt add-adv -d 02010606094142434400 -s 05061805051206000a00020a00 1\\ | ||
+ | |||
+ | This will turn on advertising with your custom payload once you've powered up the adapter. The example above sets up the complete name in the advertisement payload. The scan response payload is also set with the following: 16-bit UUID, connection interval range, and TX power level:\\ | ||
+ | |||
+ | Complete name: " | ||
+ | UUID: 1805\\ | ||
+ | Connection interval range: 7.5 ms to 12.5 ms\\ | ||
+ | TX power level: 0 dBm\\ | ||
+ | |||
+ | If you want to understand the meaning of the header bytes in the payloads I've posted, I suggest you read the Bluetooth Core Specification documents. Another place to look is in the BlueZ source code (eir.h in the src directory). | ||
+ | |||
+ | ==== A Good Source ==== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | You might want to check out the main.c file in the client folder of the most recent Bluez source code. It's the source code for the bluetoothctl tool. Run it too. The source code shows exactly how they use GDBus, including proxies, agents, calling methods like described in the API (/doc folder) and all that. It's in C and uses the high level API. | ||
+ | |||
+ | I suggest you step through the code because it took me 2 weeks endlessly trying to understand Bluez in C and the fact that there' | ||
+ | |||
+ | The gdbus tool: https:// | ||
+ | |||
+ | These contain all the calls to gdbus and objects in the main.c file and explain them very well. https:// | ||
+ | |||
+ | D-Feet, an invaluable tool to inspecting and learning about Dbus on your system. Try checking out the /bluez bus. https:// | ||
+ | |||
+ | or | ||
+ | |||
+ | sudo apt-get install d-feet | ||
+ | |||
+ | Not much of a tutorial, but worth a read to understand some concepts, as the bluetoothctl tool fits into what they' | ||
+ | |||
+ | The bluetoothctl creates an interactive shell though, so it might not be wise to waste time trying to fit in your code, but just pick what you need from it. | ||
+ | |||
+ | ==== Other Sources ==== | ||
+ | |||
+ | |||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | A bit late here, but I recently suffered the headache of following a variety of different guides, none of which worked for me. So here is another guide...that probably won't work for you ;) | ||
+ | |||
+ | I downloaded the most recent version from the official page: [[http:// | ||
+ | |||
+ | For example, at the time of writing it was 5.47, so I used (on my raspberry): | ||
+ | |||
+ | < | ||
+ | '' | ||
+ | </ | ||
+ | Then I extracted it and built it: | ||
+ | |||
+ | < | ||
+ | cd bluez-5.47 | ||
+ | '' | ||
+ | </ | ||
+ | **Read the '' | ||
+ | |||
+ | Install the dependencies first: (glib, dbus, libdbus, udev, etc.) Most of them are already installed and if not they should be easy to install, all provided by package manager. Once you've done that: | ||
+ | |||
+ | < | ||
+ | '' | ||
+ | </ | ||
+ | I also added '' | ||
+ | < | ||
+ | sudo make install | ||
+ | '' | ||
+ | </ | ||
+ | It takes maybe 10 minutes to compile. After installing, you should find '' | ||
+ | |||
+ | Go to each of these directories and type | ||
+ | |||
+ | < | ||
+ | '' | ||
+ | </ | ||
+ | You'll note that the one in //libexec// is new and the one in //lib// is old. | ||
+ | |||
+ | BlueZ creates these d-bus objects and interface to expose the bluetooth devices to you in nice ways. | ||
+ | |||
+ | In order to make sure that d-bus is talking to you **new** BlueZ 5.47 and not your **old** BlueZ 5.23, you need to tell '' | ||
+ | < | ||
+ | '' | ||
+ | </ | ||
+ | Make sure the '' | ||
+ | |||
+ | For me, that wasn't enough. No matter what, upon restart I always got bluetoothd 5.23... So I just created a symlink from the old one to the new. | ||
+ | |||
+ | First rename the old file: | ||
+ | |||
+ | < | ||
+ | '' | ||
+ | </ | ||
+ | Create the symlink: | ||
+ | |||
+ | < | ||
+ | sudo systemctl daemon-reload | ||
+ | '' | ||
+ | </ | ||
+ | That should do it. | ||
+ | |||
+ | Now, **I have to say this, even though it's not relevant to the question**: | ||
+ | |||
+ | If you want to develop an application for your pi, **read the blueZ docs folder**. It introduces you to something called [[https:// | ||
+ | |||
+ | There are a lot of red herrings out there: wrappers that attempt to give you " | ||
+ | |||
+ | Do yourself a favor. Take a few hours to learn how '' | ||
+ | |||
+ | Good luck! | ||
+ | |||