Bluetooth broadcast - host name vs friendly name vs alias - bluetooth

My Raspberry Pi is configured for Bluetooth. I change the friendly name using hciconfig:
$ sudo hciconfig hci0 name "MyTest"
$ sudo hciconfig -a hci0
hci0: Type: Primary Bus: UART
...
UP RUNNING PSCAN ISCAN
Name: 'MyTest'
...
On a different Ubuntu box, I scan for nearby devices:
$ sudo bluetoothctl
# scan on
...
[NEW] Device DC:A6:32:88:A6:12 raspberrypi
[CHG] Device DC:A6:32:88:A6:12 Name: MyTest
[CHG] Device DC:A6:32:88:A6:12 Alias: MyTest
Why does it show three different items? And where is it picking the name "raspberrypi" from? Is Pi also broadcasting its host name? Regards.

The first thing to mention is that hciconfig has been deprecated by the BlueZ developers in 2017 so will be removed from systems at some point in the future.
The current tool for this job is btmgmt (assuming you have an up-to-date system).
sudo btmgmt name MyRaspberryPi MyPi
In /etc/bluetooth/main.conf there is this information:
# Defaults to 'BlueZ X.YZ', if Name is not set here and plugin 'hostname' is not loaded.
# The plugin 'hostname' is loaded by default and overides the Name set here so
# consider modifying /etc/machine-info with variable PRETTY_HOSTNAME=<NewName> instead.
#Name = BlueZ

Related

hcitool lescan returns an I/O error on Manjaro

I'm trying to connect MX Master 3 to my computer.
OS: Manjaro 21.2.1 Qonos
Kernel: x86_64 Linux 5.10.89-1-MANJARO
Computer: Dell XPS 7390
I tried:
sudo hcitool lescan
which gives:
Set scan parameters failed: Input/output error
rfkill list all gives:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
and hcitool dev gives:
Devices:
hci0 84:1B:77:B4:B4:D1
Things I have already tried:
hciconfig hci0 down
hciconfig hci0 up
sudo systemctl restart bluetooth.service
sudo systemctl restart dbus.service
Inside bluetoothctl:
menu scan
transport le
back
scan on
This resolved the problem.

Minimal setup for bluetooth SDP in 2021

I have minimal linux system (no GUI, raspberry pi like board, based on buildroot) which should be able to do the following:
Allow all bluetooth clients to pair (no pin) after pressing a button for 60 seconds
Allow connection using SPP (serial port profile)
Endpoint of the SPP should be something like /dev/rfcomm
In an older version of buildroot (setup around 2016) I was able to do this using the following commands executed after the button press:
dbus-daemon &
bluetoothd --compat &
hciconfig hci0 up
hciconfig hci0 noauth
hciconfig hci0 sspmode 1
hciconfig hci0 class 0x080500
hciconfig hci0 name foobar
hciconfig hci0 piscan
sdptool add --channel=22 SP
rfcomm listen /dev/rfcomm0 22
I could then pair from my android phone without pin and connect to the SPP interface using this app. Since then hciconfig, sdptool and rfcomm where deprecated. If I try the above method I can't pair from my phone (got connection refused). Without pairing the SPP stuff can't work obviously.
What would be the preferred way in 2021 to setup a headless system which behaves as described above?

Debian: cannot create rfcomm0

On Debian Jessie 8.2:
I'm trying to create the following device: /dev/rfcomm0 in order to connect my arduino via bluetooth module HC-05, but no success.
Here are the steps I'm following:
1) I guess my HC-05 called FOO is recognised and properly configured, because
hcitool scan
reports
98:D3:31:xx:xx:xx FOO
xx are just a mask I use here for privacy.
2) I added the file /etc/bluetooth/rfcomm.conf
rfcomm0 {
# Automatically bind the device at startup
bind yes;
# Bluetooth address of the device
device 98:D3:31:xx:xx:xx;
# RFCOMM channel for the connection
channel 1;
# Description of the connection
comment "FOO";
}
3) I restarted bluetooth service
sudo /etc/init.d/bluetooth restart
response is:
[ ok ] Restarting bluetooth (via systemctl): bluetooth.service.
Nevertheless device rfcomm0 is not created.
I'm following the instructions here:
Bluetooth serial communication with HC-05
I did this operation months ago on another Linux system (it was ubuntu) and I can remember
evertything went well: the port was created. Probably I'm missing some important step!
Thanks a lot,
Valerio
UPDATE:
this command
sdptool records 98:D3:31:xx:xx:xx
reports
Service Name: Dev B
Service RecHandle: 0x10000
Service Class ID List:
"Serial Port" (0x1101)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 1
Language Base Attr List:
code_ISO639: 0x656e
encoding: 0x6a
base_offset: 0x100
I think this confirms that the channel in rfcomm.conf is 1
Ok , thanks to Kaylum this is solved!
The manual binding create the device rfcomm0
sudo rfcomm bind 0 98:D3:31:xx:xx:xx 1
Then, in order to make Processing write/read on the created port,
I needed to run Processing as sudoer, otherwise Processing says that the port exists but is busy. As sudoer, I can confirm that the port correctly sends data back and forth between Arduino and Processing!

Cannot connect to BLE device on Raspberry Pi

I'm trying to connect to a BLE device (Heart rate sensor, Polar H7), on a Raspberry Pi 2. I use the last version of bluez (5.35) found here: http://www.bluez.org/download/
But when I'm trying to connect using gatttool, I always have "connection refused" error.
Here is what I do :
pi#raspberrypi ~ $ sudo su
root#raspberrypi:/home/pi# hciconfig dev
hci0: Type: BR/EDR Bus: USB
BD Address: 5C:F3:70:69:54:3D ACL MTU: 1021:8 SCO MTU: 64:1
DOWN
RX bytes:616 acl:0 sco:0 events:34 errors:0
TX bytes:380 acl:0 sco:0 commands:34 errors:0
root#raspberrypi:/home/pi# hciconfig dev up
root#raspberrypi:/home/pi# hcitool lescan
LE Scan ...
00:22:D0:6D:E0:E6 (unknown)
00:22:D0:6D:E0:E6 Polar H7 6DE0E61C
^Croot#raspberrypi:/home/pi# hcitool lecc 00:22:D0:6D:E0:E6
Connection handle 64
root#raspberrypi:/home/pi# gatttool -b 00:22:D0:6D:E0:E6 -I
[00:22:D0:6D:E0:E6][LE]> connect
Attempting to connect to 00:22:D0:6D:E0:E6
Error connect: Connection refused (111)
[00:22:D0:6D:E0:E6][LE]>
I tried to follow this topic: BLE gatttool cannot connect even though device is discoverable with hcitool lescan
but it did not work for me.
I was able to connect via Bluetooth from Raspberry to my Android device using gatttool after setting the LE address type to random with the -t random argument, i.e. :
sudo gatttool -t random -b DD:9D:0B:43:A1:77 -I
connect
From gatttool man
--t, ---addr-type=[public | random]
# Set LE address type. Default: public
USAGE gatttool [OPTION...]
Help Options:
-h, --help Show help options
-h, --help Show help options
--help-all Show all help options
--help-gatt Show all GATT commands
--help-params Show all Primary Services/Characteristics
arguments
--help-char-read-write Show all Characteristics Value/Descriptor
Read/Write arguments
Application Options:
--i, ---adapter=hciX Specify local adapter interface
--b, ---device=MAC Specify remote Bluetooth address
--t, ---addr-type=[public | random] Set LE address type. Default: public
--m, ---mtu=MTU Specify the MTU size
--p, ---psm=PSM Specify the PSM for GATT/ATT over BR/EDR
--l, ---sec-level=[low | medium | high] Set security level. Default: low
--I, ---interactive Use interactive mode
By default GATT is not enable. Add the below lines to /etc/bluetooth/main.conf
EnableLE = true // Enable Low Energy support. Default is false.
AttributeServer = true // Enable the GATT attribute server. Default is false.
I got it running with:
btmgmt le on
Fixed the connection refused (111) issue in my raspberry pi 3B running raspbian buster by Updating/installing BlueZ via apt-get
> sudo apt-get install --no-install-recommends bluetooth
and re running
> sudo gatttool -t random -b E9:1C:89:B7:16:F9 -I
I had to disable the plugin pnat in /etc/bluetooth/main.conf -> DisablePlugins=pnat. I read that it's unstable, but I don't know much about this plugin.
If you are still wondering why the random worked. I have gone through the code and this what I found.
​-t ​(Addr:Type: Set LE Address Type)
Public | random
Default: Public
A random or static address is a 48-bit randomly generated address and shall meet the following
requirements:
• The two most significant bits of the static address shall be equal to ‘1’
• All bits of the random part of the static address shall not be equal to ‘1’
• All bits of the random part of the static address shall not be equal to ‘0’
(Source)
You could try to use gatttool -b 00:22:D0:6D:E0:E6 -I and then connect, just after discovering the MAC without connecting before with hcitool lecc 00:22:D0:6D:E0:E6

bluetooth pairing and connection from command line

I want to connect and pair to available nearby bluetooth devices from command line in linux.
I have searched through internet, and found that we can pair through simple-agent, but it is not available in my device.
following commands are available in device:
hcitool, hciconfig, hcidump, sdptool, l2ping, rfcomm.
for connecting to remote device i am running the following command.
hcitool cc BD_ADDRESS
but above command is connecting the device for a while, and again the device is getting disconnected. in hcidump output , i am seeing the disconnection event. Is there anyway in linux command line through which i can test my bluetooth? What can be the reason for device is getting disconnected after a while?
I saw the same issue once and was able to solve it with the rfcomm command. Try the following:
# rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &
Additionally, it is good practice to enable secure simple pairing and enable page and inquiry scan with the following commands:
# hciconfig hci0 sspmode 1
# hciconfig hci0 piscan
Let me know if you still have problems.

Resources