[BlueZ] 1、Download install and use the BlueZ and hcitool on PI 3B+
星期日, 02. 九月 2018 11:58下午 - beautifulzzzz
1. introduction
bluez is the default bluetooth protocol stack on linux. it should be present and installed on your linux distribution. if not, building and installing from source is not too difficult:
- download the latest stable source release of bluez from here. unzip the compressed file you downloaded.
- install the headers and libraries required for bluez compilation:
2. download and install
i follow the blog (installing bluez 5.44 onto raspbian?) to install bluez-5.50.
download the most recent version from the official page:.
for example, at the time of writing it was 5.50, so i used(on my pi):
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.50.tar.xz
then i extracted it and built it:
tar -xf bluez-5.50.tar.xz cd bluez-5.50
read the readme! it lists the dependencies and the configure switches:
install the dependencies first: (glib, dbus, libdbus, udev, etc.)
sudo apt install libdbus-1-dev libudev-dev libical-dev libreadline-dev
note: if you do not install the libdbus-1-dev, you will later get this strange error:
configure: error: d-bus >= 1.6 is required
once you've installed dependencies, you can configure switches:
./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --enable-experimental
then do:
make sudo make install
it takes maybe 10 minutes to compile. after installing, you should find bluetoothd in /usr/libexec/bluetooth
. you should also see bluetoothd in /usr/lib/bluetooth
.
go to each of these directories and type
./bluetoothd --version
you'll note that the one in libexec is new and the one in lib is old.
in order to make sure that d-bus is talking to you new bluez 5.50 and not your old bluez 5.43, you need to tell systemd to use the new bluetooth daemon:
sudo vim /lib/systemd/system/bluetooth.service
make sure the exec.start
line points to your new daemon in /usr/libexec/bluetooth
.
for me, that wasn't enough. no matter what, upon restart i always got bluetoothd 5.43... so i just created a symlink from the old one to the new.
first rename the old file:
sudo mv /usr/lib/bluetooth/bluetoothd /usr/lib/bluetooth/bluetoothd-543.orig
create the symlink:
sudo ln -s /usr/libexec/bluetooth/bluetoothd /usr/lib/bluetooth/bluetoothd sudo systemctl daemon-reload
that should do it.
3. how to use the bluez and hcitool
3.1 setup bluetooth service
start the bluetooth service and enable automatic startup, assuming you're using systemd as the init daemon:
sudo systemctl start bluetooth.service sudo systemctl enable bluetooth.service
3.2 hcitool scan for bluetooth devices
before start scanning make sure that your bluetooth device is turned on and not blocked, you can check that with the rfkill command:
sudo rfkill list
if the bluetooth device is blocked (soft or hard blocked), unblock it with the rfkill command again:
sudo rfkill unblock bluetooth
bring up the bluetooth device with hciconfig command and start scanning, make sure the target device's bluetooth is on and it's discoverable:
sudo hciconfig hci0 up hcitool scan
wait few moment to complete the hcitool scan or hcitool lescan, the results will be something like bellow:
here 00:1a:7d:da:71:0a is the bluetooth mac address and shen-pc is the name of the bluetooth device, i.e. an pc.
note: use hcitool lescan
will forever scan ble devices, if use ctrl+c stop it, it will show error(ref to links 4 to solve):
hcitool lescan
set scan parameters failed: input/output error
3.3 bluetooth service discovery
now we have the bluetooth mac address of the target device, use the sdptool
command to know which services (like dun, handsfree audio) are available on that target device.
sdptool browse 28:ed:6a:a0:26:b7
you can also use the interactive bluetoothctl
tool for this purpose.
if the target device is present, you can ping it with l2ping command, requires root privilege:
➜ bluez-5.50 sudo l2ping 94:87:e0:b3:ac:6f ping: 94:87:e0:b3:ac:6f from b8:27:eb:8e:cc:51 (data size 44) ... 44 bytes from 94:87:e0:b3:ac:6f id 0 time 53.94ms 44 bytes from 94:87:e0:b3:ac:6f id 1 time 77.12ms 44 bytes from 94:87:e0:b3:ac:6f id 2 time 38.63ms 44 bytes from 94:87:e0:b3:ac:6f id 3 time 46.13ms 44 bytes from 94:87:e0:b3:ac:6f id 4 time 59.96ms 5 sent, 5 received, 0% loss
so, bluetooth service discovery is useful to determine the type of the device, like if it's a bluetooth mp3 player or it's a keyboard.
links
- 1.bluetooth on modern linux by szymon janc
- 3.linux bluetooth setup with bluez and hcitool
- 4.hcitool lescan shows i/o error
@beautifulzzzz 智能硬件、物联网,热爱技术,关注产品 博客:http://blog.beautifulzzzz.com 园友交流群:414948975
上一篇: 我军训完回家,我妈以为我去非洲挖煤了