Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth: Detect Event Using Bluez

  • We are trying to detect an event in C/C++.
  • We are using Bluez on Linux as our Bluetooth Library.
  • Our objective is to run an external script when that event is triggered.
  • Specifically, we have a Motorola H730 headset, with a button on it, and we are looking to detect the press of that button.
  • We have paired the bluetooth device(hcitool), and we can connect to it (bluez-test-audio), but would be interested in migrating that approach to C/C++.
  • The output of hcidump when the device is connected and the button is pressed is:

    HCI sniffer - Bluetooth packet analyzer ver 2.2
    device: hci0 snap_len: 1028 filter: 0xffffffff

    HCI Event: Mode Change (0x14) plen 6
    status 0x00 handle 21 mode 0x00 interval 0
    Mode: Active
    ACL data: handle 21 flags 0x02 dlen 17
    L2CAP(d): cid 0x0041 len 13 [psm 0]
    < ACL data: handle 21 flags 0x00 dlen 14
    L2CAP(d): cid 0x0047 len 10 [psm 0]
    < ACL data: handle 21 flags 0x00 dlen 22
    L2CAP(d): cid 0x0047 len 18 [psm 0]
    < ACL data: handle 21 flags 0x00 dlen 22
    L2CAP(d): cid 0x0047 len 18 [psm 0]
    HCI Event: Number of Completed Packets (0x13) plen 5
    handle 21 packets 1
    ACL data: handle 21 flags 0x02 dlen 18
    L2CAP(d): cid 0x0041 len 14 [psm 0]
    < ACL data: handle 21 flags 0x00 dlen 14
    L2CAP(d): cid 0x0047 len 10 [psm 0]
    HCI Event: Number of Completed Packets (0x13) plen 5
    handle 21 packets 1
    HCI Event: Number of Completed Packets (0x13) plen 5
    handle 21 packets 1
    HCI Event: Number of Completed Packets (0x13) plen 5
    handle 21 packets 1
    HCI Event: Mode Change (0x14) plen 6
    status 0x00 handle 21 mode 0x02 interval 2048
    Mode: Sniff

  • We've looked for the bluetooth documentation and haven't had much success.
    How can we detect a bluetooth event in C/C++ using Bluez?
like image 406
user1505132 Avatar asked Jul 05 '12 21:07

user1505132


1 Answers

I did something similar last year. The approach I settled on was using libdbus-c++ to generate c++ proxy objects/classes to communicate with bluez via dbus.

  • Determine the bluetooth interfaces implemented by your headset. I used d-feet and explored the device when it was connected. You can manually control and read status from the headset here, this came in handy for debugging.
  • Generate,find or create the dbus xml descriptions of the required interfaces. I did this manually by translating the api specification from the bluez api documentation to xml however I believe you could also create them by dbus introspection.
  • Use dbusxx-xml2cpp to generate proxy objects
  • Determine the callback that is called when your button is pressed
  • Integrate proxy objects into your code as required

You will need to link your code to libdbus-c++ and have it install on the target too.

You can use the generated proxy objects to access the entire functionality of the headset, it may come in handy for a lot more than catching a button press!

like image 149
Michael Shaw Avatar answered Oct 06 '22 03:10

Michael Shaw