I have a BLE light which I'd like to control programatically. By sniffing on the Bluetooth packets between my phone (which has an app to control the lights) and the light I figured out the following information:

  • Device mac address: XX:XX:XX:XX:XX:XX
  • Handle that controls light status: 0x0009
  • Value for turning on light: c7e3f68520e8d5ae5acd17760a01459d

While looking into this, I stumbled upon the (deprecated) utility gatttool. According to their documentation, I can write to the BLE device like so:

sudo gatttool --char-write-req -b XX:XX:XX:XX:XX:XX -a 0x0009 -n c7e3f68520e8d5ae5acd17760a01459d

This works to some extent. When I am connected to the light using my phone and execute said command, I receive this response:

connect error: Connection refused (111)

This is expected, as I am already connected to the device with my phone. When I disconnect my phone I receive the following response:

connect error: Device or resource busy (16)

Unfortunately this is not what I hoped to see. Online I read that this problem might occur due to gatttool being deprecated.

To summarize, what I'm looking for is:

  • Some information/thoughts on how to make gatttool possibly work in this situation.
  • A different approach (i.e. without gatttool). I looked into bluetoothctl, but I cannot seem to connect with the device either.

Any input will be appreciated.

1 Answer 1

connect error: Device or resource busy (16)

Means that your interface is busy, is your laptop connected to a device or is another tool using it? For example you can't scan with bluetoothctl and use gatttool at the same time. Unless you have two different interfaces.

Use the following to make sure your Bluetooth is working, where hci0 is the hci interface you're using.

$ hciconfig hci0 down && hciconfig hci0 up
$ service bluetooth restart

To check your interfaces, -a is optional but gives extra information.

$ hciconfig -a

To automatically convert hex to ascii when reading a value:

$ gatttool -b [MAC] --char-read -a [characteristic] | awk -F':' '{print $2}' | tr -d ' ' | xxd -r -p; printf '\n'

In my experience gatttool can be very useful, other tools to try out: bettercap, ubertooth one

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .