Error BLE gattool with handle input - What is wrong? - bluetooth

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:

Related

Read raw USB data for port being used by VirtualBox

I have software for communicating with a serial device that only runs on Windows 7. My host machine is running Ubuntu 16.04, with Windows 7 in virtual box.
I've managed to set it up using USB device filters in VirtualBox settings so that Windows sees and can communicate with the device.
However I'd like to capture what's being sent to and from the device. I thought something as simple as cat /dev/ttyUSB0 would work but unfortunately when I start VirtualBox I get this in dmesg:
[31199.465270] vboxdrv: ffffffffc0df4020 VMMR0.r0
[31199.653494] vboxdrv: ffffffffc0ef7020 VBoxDDR0.r0
[31199.710573] VBoxNetFlt: attached to 'wlp1s0' / 98:54:1b:04:13:48
[31199.863579] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
[31199.863674] ftdi_sio 1-3:1.0: device disconnected
So I don't know which file to monitor in /dev anymore. I suppose I might be able to try listen for the communication from Windows, but if I can I'd like to know if it's possible to monitor it from my host machine.
Edit:
This answer helped me for a start. I now get some cool looking stream on the terminal, next step is deciphering it, or if anyone has a better way I'm interested - it looks like the person who answered that question still has deciphering it as a TODO :-)
The fact that it was communicating with software in VirtualBox didn't really matter. This answer got me most of the way just by (as root):
modprobe usbmon
cat /sys/kernel/debug/usb/devices|less and search for device
cat /sys/kernel/debug/usb/usbmon/7u where 7 is the number from "Bus=" in the device table from step 2
From there is was just filtering the output.
A basic way could be just
cat /sys/kernel/debug/usb/usbmon/1u |cut -d\ -f9- | grep ^\n
But the device I was working with was constantly sending lines with just 4 characters, to filter out those I got the address word from the output line, and grepped for that. In my case I only wanted to capture "C" or callback output. I was also filtering for lines that contained more than just the default "0160" that the device was constantly outputting. To try understand the hex output I put a xxd -r -p at the end which gave me:
cat /sys/kernel/debug/usb/usbmon/1u | grep -e 'C Bi:1:005:1\s0\s\w*\s=\s0160\w' | cut -d\ -f8- | xxd -r
Where the grep is
grep -e 'C <address> <someotherstuff> = <always-output-string><anycharacter>
This still gave me this indecipherable junk below, but I don't know the format so I'm stuck here. Maybe the steps to read and filter will be useful for someone
>
O�UDQN��RG_JAMS142E DEFAULT0XXXXXXX�lSTz:�RSDU�vy��������������������������z�fff=�����{6zC"z�u6zC�z�H
;�����C��Af[���RSC�b ISD�EGIN
�CG_IEW0321:0407JUN12S��Z-�$''$'''C'''

Can't get info from hcidump --raw output

i am trying to create a BLE scan module via nodeJs and Bluez. I want to learn how to get mac address, minor, major and rssi values from the output of hcidump --raw command. I took a look at Radius Networks ibeacon_scan file but i still don't have a clue how i can do it with nodeJs. Can anyone help me with that? Thanks a lot
Try this: sudo hcidump -R -x
Then in a second terminal do your Bluetooth stuff

Using gnu screen to access serial port

I am trying to access a device which is attached to a USB-serial port. The settings are 57600 baud, 8 bit, 1 stop bit, no parity. The device outputs a status line every second and accepts typed commands.
I would like to use GNU screen to initiate 2 way communications, so I am using this command:
screen /dev/ttyS2 57600,cs8
However I just get a blank screen, nothing received from the device.
The communication is fine using teraterm, and I can also do this
stty -F /dev/ttyS2 57600 cs8
cat /dev/ttyS2
to see the status output from the device.
I've tried various combinations of ixon, ixoff, crtscts, and clocal but nothing makes any difference.
How can I determine what the correct command should be?
I am using Cygwin on Windows 10.
I faced the same issue with gnu-screen, I started using plink.exe instead from the PuTTY suite. It's not optimal, but it does the job. In my case serial is just for recovery, not for everyday usage.
Start PuTTY, create a profile with your serial connection.
Name and save the connection.
From cygwin, run: '/cygdrive/c/Program\ Files\ (x86)/PuTTY/plink.exe -load SerialProfile'

bluetoothctl to hcitool equivalent commands

In Linux I used to use "hidd --connect mmac" to connect with BT devices but that is now gone since Bluez5.
I can use bluetoothctl to make the connection manually but I need to use these commands from my app and using bluetoothctl would be difficult.
What are the hcitool equivalent commands to do what bluetoothctl does?
For example, I would type in bluetoothctl:
select <cmac>
scan on
trust <mmac>
pairable on
pair <mmac>
connect <mmac>
I can use "hcitool scan" for the scanning but I haven't figured out connecting.
I've tried using "hcitool cc mmac" followed by "hcitool auth mmac" but nothing works.
Or can hcitool do what bluetoothctl does?
I am using bluetoothctl from scripts like this:
#!/bin/bash
bluetoothctl << EOF
power on
EOF
And it is possible to specify multiple commands as one command per line.
Strangely enough, it does not work like this for me:
echo "power on" | bluetoothctl
(I am using bluez-5.21-r1 - not sure whether this is version dependent)
You can pass commands to bluetoothctl like this:
echo -e 'power on\nquit' | bluetoothctl
You can even use tab to autocomplete:
echo -e 'power on\nconnect \t \nquit' | bluetoothctl
I am not adding this as a comment on Jiri's answer so it is more visible.
Another solution (the best in my opinion) would be to use expect TCL scripting with bluetoothctl.
I use it to automatically connect to bluetooth devices using bluetoothctl without having to interact with it.
For example to connect to a device identified by its MAC address
#!/usr/bin/expect -f
set address [lindex $argv 0]
set prompt "#"
log_user 0
spawn bluetoothctl
expect $prompt
send -- "remove $address\r"
expect $prompt
send -- "scan on\r"
expect "Discovery started"
sleep 10
send -- "scan off\r"
expect "Discovery stopped"
expect $prompt
send -- "trust $address\r"
expect "trust succeeded"
expect $prompt
send -- "pair $address\r"
expect "Pairing successful"
expect "Device $address Connected: no"
expect $prompt
send -- "connect $address\r"
expect "Connection successful"
expect $prompt
send "quit\r"
expect "eof"
You can launch this script as it ./myExpectScript <MAC_addr>
If you want to see the output just set the log_user value to 1
I solved this using tmux, i.e.:
Install tmux:
apt install tmux
Create Session:
tmux new-session -d -s ServerFault 'sudo bluetoothctl -a |& tee /run/shm/BLUETOOTH_OUTPUT'
Then you can issue commands like:
tmux send-keys -t ServerFault "pair AC:22:0B:9F:0C:D6" Enter
I wrote a python3 script to auto-connect my gamepads on my game cabinet. You have to run it for each device you want to connect, but no user interaction is needed. It uses the expect python module, similar to the above answers, to communicate with bluetoothctl. I found it a little easier to use than the expect/tcl scripts. If python can't find pexpect, you would need to install python3-pexpect.
sudo apt install python3-pexpect
You'll want to change the mylist list variable to search for the MACs that match the first 3 bytes (the vendor part) of your bluetooth devices. So, for example, if the first 3 bytes of the MACs on your devices start with AA:BB:CC:, then change the EF\:17\:D8\: part to AA\:BB\:CC\:
You can add as many devices you want to scan for in the mylist variable. My example searches for two different vendors, one starting with EF\:17\:D8\:, and one starting with 16\:04\:18\: The script will reject all other bluetooth devices that may be transmitting, and only connect the gamepad MACs you've configured in the mylist variable.
mylist = ['E4\:17\:D8\:[0-9A-F].[:][0-9A-F].[:][0-9A-F].', '16\:04\:18\:[0-9A-F].[:][0-9A-F].[:][0-9A-F].',pexpect.EOF]
Here is the python3 script:
#!/usr/bin/python3
import os,sys,time,pexpect
def findaddress():
address=''
p = pexpect.spawn('hcitool scan', encoding='utf-8')
p.logfile_read = sys.stdout
mylist = ['E4\:17\:D8\:[0-9A-F].[:][0-9A-F].[:][0-9A-F].', '16\:04\:18\:[0-9A-F].[:][0-9A-F].[:][0-9A-F].',pexpect.EOF]
p.expect(mylist)
address=p.after
if address==pexpect.EOF:
return ''
else:
return address
def setbt(address):
response=''
p = pexpect.spawn('bluetoothctl', encoding='utf-8')
p.logfile_read = sys.stdout
p.expect('#')
p.sendline("remove "+address)
p.expect("#")
p.sendline("scan on")
mylist = ["Discovery started","Failed to start discovery","Device "+address+" not available","Failed to connect","Connection successful"]
while response != "Connection successful":
p.expect(mylist)
response=p.after
p.sendline("connect "+address)
time.sleep(1)
p.sendline("quit")
p.close()
#time.sleep(1)
return
address=''
while address=='':
address=findaddress()
time.sleep(1)
print (address," found")
setbt(address)
I wrote another python3 script that wraps the entire process in a Vte and shows the process as it is happening, and lets you to exit it, if needed. If you want to see that, just let me know.
You can give commands as arguments directly to bluetoothctl from the shell, without needing expect scripts.
I use this in a Bash script in Ubuntu 20.04 :
mac="90:03:B7:17:00:08"
# turn on bluetooth in case it's off
rfkill unblock bluetooth
bluetoothctl power on
bluetoothctl connect $mac
To disconnect, use
bluetoothctl disconnect
This assumes the destination $mac is already paired of course. If it isn't, you can first do
bluetoothctl pair $mac
To list all available commands:
bluetoothctl help

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