gatttool characteristic write in non-interactive mode - bluetooth

I want to use gatttool in a shell script.
What I want to achieve is similar to these:
Gatttool non-interactive mode --char-write
Gatttool in non interactive mode
How to use gatttool non-interactive mode
Gatttool Non-Interactive mode, multiple char-write-req
GATTTool fails to read/write characteristics
In interactive mode I can do this:
# gatttool -b xx:xx:xx:xx:xx:xx -I
[xx:xx:xx:xx:xx:xx][LE]> connect
Attempting to connect to xx:xx:xx:xx:xx:xx
Connection successful
[xx:xx:xx:xx:xx:xx][LE]> char-write-cmd 0x0025 aabbcc
Notification handle = 0x0025 value: aa bb cc
Now if I run non-interactively:
# gatttool -b xx:xx:xx:xx:xx:xx --char-write --handle=0x0025 --value=aabbcc
It just hangs I need to break it with CTRL+C and does not send anything to the device.
Now, all questions above are recommending to use --char-write-req instead of --char-write.
But that it is misleading, as request and command mean different things.
My device does not accept request, only commands (even in interactive mode!). So trying to send the same data with request will fail:
# gatttool -b xx:xx:xx:xx:xx:xx -I
[xx:xx:xx:xx:xx:xx][LE]> connect
Attempting to connect to xx:xx:xx:xx:xx:xx
Connection successful
[xx:xx:xx:xx:xx:xx][LE]> char-write-req 0x0025 aabbcc
Error: Characteristic Write Request failed: Attribute can't be written
Now of course using the recommendations from the above questions (--char-write-req) will fail:
# gatttool -b xx:xx:xx:xx:xx:xx --char-write-req --handle=0x0025 --value=aabbcc
Characteristic Write Request failed: Attribute can't be written
So what is the non-interactive alternative of writing a characteristic command,
If I don't want to use any Python or similar overkills for this?

Related

Bluetooth, btmgmt tool how to set Link Mode

I try to understand how btmgmt work. I search command equal to:
sudo hciconfig -a hci0 lm master
I have try to use sudo btmon for dumping hci command but I have only this result:
# RAW Open: hciconfig (privileged) version...
with no op-code output, this not realy help me. Writing output to btsnoop format reveal some unknown op codes (btmon -a /tmp/btsnoop). With wireshark I have no HCI events, I don't understand.
Using sudo strace hciconfig... reveal socket and ioctl call with ioctl(ctl, HCISETLINKMODE, dr)
I read HCISETLINKMODE value and hciconfig function
but nothing about op-code to put the interface in MASTER or SLAVE mode. For some others btmgmt commands I use btmon output and the command op-code and I use op-code to search in mgmt-api.txt.
After reading that hciconfig would be deprecated, I'm trying to migrate commands using btmgmt and i just miss this one.
Any help is welcome. Thank you

Error BLE gattool with handle input - What is wrong?

I want to write a value in a BLE device.
sudo gatttool -b 34:14:B5:4A:20:75 --char-write-req -a 36F5 -n ee83b36835181e5ef206f5ac3a708ec7
Problem is it doesn't recognize the UUID/handle "36F5", while this has to be the right handle as I sniffed it with nRF. Adding "0x" before the "36F5" also doesn't work.
Error ssh:
If I enter the value (ee83b36835181e5ef206f5ac3a708ec7) in nRF, everything works fine.
Screenshot nRF:

bluez: scan and connect in parallel

what I want to achieve in a test setup is:
running:
hcitool -i hci0 lescan --duplicates --passive
on one shell
and in parallel connect on another via
hcitool -i hci0 lecc <BTADDR>
The moment I issue the connect command the scan process exits.
Why is that? I thought choosing to scan --passive will allow a scan output while managing a connection?
Best,
If you are on kernel 3.2 and above use gatttool or bluetoothctl for LE connections. hcitool is deprecated and you should not be using them anymore. Compile and install latest bluez and use gatttool for LE connections. If your LE device is in the vicinity, you should be able to connect, read and write using gatttool directly:
say, your local BT adapter is hci0 and the remote device BD_ADDR is 12:22:33:44:55:66 then:
gatttool -i hci0 -b 12:22:33:44:55:66 -I
[12:22:33:44:55:66][LE]>connect
Attempting to connect to 12:22:33:44:55:66
Connection successful
[12:22:33:44:55:66][LE]>characteristics
Read manual and help to know more about gattool.

Raspberry Pi & Blend Micro : can't connect with Bluetooth

Recently got a Blend Micro for a project and I need a raspberry pi to initiate a bluetooth connection with it.
For that I installed BlueZ 5.2 on raspberry and i used this command to detect the Blend Micro :
sudo hcitool -i hci0 lescan
I correctly detect the Blend micro then i use the gatttool command :
sudo gatttool -i hcitool hci0 -b XX:XX:XX:XX:XX:XX -I
(where the XX corresponds to the Blend MAC address)
But when I initiate the connection by typing connect it says "Host is down" :
$ sudo gatttool -i hci0 -b XX:XX:XX:XX:XX:XX -I
[ ][XX:XX:XX:XX:XX:XX][LE]> connect
Connecting... connect error: Host is down (112)
[ ][XX:XX:XX:XX:XX:XX][LE]>
Do you have any idea on how to fix that ? Tried many things without any results though...
Regards, RaZZeR
When you are connecting, you need to add "-t random" to your command line.

Programmatically removing all bluetooth devices on the Linux command line

I am able to scan for all available bluetooth devices with hcitool or with my C program.
I can pair the device using it's address with a simple-agent python script.
I would like to know if I can also remove the paired device using either hcitool, hciconfig or some kind of bluetooth command.
I know the information of detected devices for the hci0 controller is stored in /var/lib/bluetooth/XX:XX:XX:XX:XX:XX, where XX:XX:XX:XX:XX is the address of the hci controller.
This would be useful for testing pairing, connecting and disconnecting devices.
For those using Ubuntu 20.04, here is the same command using the bluetoothctl command
#!/bin/bash
for device in $(bluetoothctl devices | grep -o "[[:xdigit:]:]\{8,17\}"); do
echo "removing bluetooth device: $device | $(bluetoothctl remove $device)"
done
bluez-test-device remove XX:XX:XX:XX:XX:XX
If you install the bluez-tools package, run this to unpair a bluetooth device :
bt-device -r xx:xx:xx:xx:xx:xx
where xx:xx:xx:xx:xx:xx is the address of the paired device.
As it is mentioned above on ashish's answer, you can us bluez-test-device to remove the device which that you already know its mac address. So the problem is to parse the mac address of the added devices.
With python or c or whatever you use,
1) list the devices with;
bluez-test-device list
and parse the output and get all the MAC addresses of the devices, add them to a list.
2) disconnect and remove the devices;
bluez-test-device disconnect <MAC ADDRESS>
bluez-test-device remove <MAC ADDRESS>
Command using bluetoothctl binary: for device in $(bluetoothctl devices | grep -vEi '(o que mais vc quer deixar aqui|samsung|jbl|wireless)' | awk '{print $2}'); do bluetoothctl remove $device; done
All these answers don't answer the headline "removing all Bluetooth devices"
I wrote this little bash script to remove all the Bluetooth devices that are listed in the bt-device -l
#!/bin/bash
for device in $(bt-device -l | grep -o "[[:xdigit:]:]\{11,17\}"); do
echo "removing bluetooth device: $device | $(bt-device -r $device)"
done
How to run?
Make a new file like <fileName>.sh and paste the code above.
Run chmod +x <fileName> to make the script executable
Run ./<fileName>.sh
Celebrate! All Bluetooth devices are removed now :)

Resources