连接 BLE 设备 Raspberry pi 3 B

Connect BLE devices with Raspberry pi 3 B

我知道这不是第一次有人问这个问题,但在一周内尝试了所有可能的方法后,我仍然遇到问题。 我正在尝试将我的 Raspberry pi 连接到 BLE 设备。我可以扫描它们但无法连接到它们。

我遵循的最后一个解释是这个 github 问题:https://github.com/ukBaz/python-bluezero/issues/30

这是我为尝试更新 bluez 并使此连接正常运行而执行的所有命令。

###Install RASPBIAN JESSIE LITE 
2017-01-11-raspbian-jessie-lite.img

###Updates
$ sudo apt-get update

$ sudo apt-get upgrade

###Add libs
$ sudo apt-get install bluetooth  bluez-tools build-essential autoconf glib2.0 libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline-dev git

###Get bluez-5.43
$ wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.43.tar.xz

$ tar xf bluez-5.43.tar.xz

$ cd bluez-5.43

###Patch bluez
$ wget https://gist.github.com/pelwell/c8230c48ea24698527cd/archive/3b07a1eb296862da889609a84f8e10b299b7442d.zip

$ unzip 3b07a1eb296862da889609a84f8e10b299b7442d.zip

$ git apply  -v c8230c48ea24698527cd-3b07a1eb296862da889609a84f8e10b299b7442d/*

###Install bluez
$ ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --enable-experimental --enable-maintainer-mode

$ make -j 4 && sudo make install

### Add to /etc/dbus-1/system.d/bluetooth.conf 
<!-- allow users of bluetooth group to communicate -->
<policy group="bluetooth">
  <allow send_destination="org.bluez"/>
</policy>

$ sudo usermod -G bluetooth -a $USER

$ sudo sed -i '/^ExecStart.*bluetoothd\s*$/ s/$/ --experimental/' /lib/systemd/system/bluetooth.service

$ sudo systemctl daemon-reload

$ sudo service bluetooth restart

$ service bluetooth status

$ sudo reboot

$ sudo hcitool lescan
       LE Scan ...
       XX:XX:XX:XX:XX:XX (name)

$ 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
       Error: connect error: Transport endpoint is not connected (107)


$ gatttool -b XX:XX:XX:XX:XX:XX -t random -I
       [XX:XX:XX:XX:XX:XX][LE]> connect
       Attempting to connect to XX:XX:XX:XX:XX:XX
       Error: connect error: Connection refused (111)

感谢您的帮助

编辑 Constantin Chabirand 的回答

$ systemctl status bluetooth
    ● bluetooth.service - Bluetooth service
       Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
       Active: active (running) since Tue 2017-01-17 21:17:07 UTC; 1min 51s ago
         Docs: man:bluetoothd(8)
     Main PID: 587 (bluetoothd)
      Status: "Running"
       CGroup: /system.slice/bluetooth.service
               └─587 /usr/libexec/bluetooth/bluetoothd --experimental


$ bluetoothctl
    [NEW] Controller B8:27:EB:FD:93:2B raspberrypi [default]
    [bluetooth]# scan on
    Discovery started
    ....

    [bluetooth]# scan off
    [bluetooth]# connect XX:XX:XX:XX:XX:XX
    Attempting to connect to XX:XX:XX:XX:XX:XX
    Failed to connect: org.bluez.Error.Failed

编辑 2

使用简单解决方案重新安装后:

$ bluetoothctl -v
    5.43

$ systemctl status bluetooth 
    ● bluetooth.service - Bluetooth service
       Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
       Active: active (running) since Wed 2017-01-18 15:46:53 UTC; 3min 37s ago
         Docs: man:bluetoothd(8)
     Main PID: 586 (bluetoothd)
       Status: "Running"
       CGroup: /system.slice/bluetooth.service
               └─586 /usr/local/libexec/bluetooth/bluetoothd --experimental

$ sudo bluetoothctl
    [bluetooth]# connect XX:XX:XX:XX:XX:XX
    Attempting to connect to XX:XX:XX:XX:XX:XX
    Failed to connect: org.bluez.Error.Failed

bash 命令的结果是什么 systemctl status bluetooth ?

你可以启动 bluetoothctl(只需在你的 shell 中输入)吗?

我还使用 raspberry pi 3 连接到 BLE 设备,我不需要补丁。使用 bluez5.43 和 post 我编写的两个命令的结果开始全新安装


更新

对于安装,我使用了最简单的方法。我按照安装文件中的 "The simplest way to compile this package is:" 部分进行操作,即:

 wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.43.tar.xz

 tar xf bluez-5.43.tar.xz

 cd bluez-5.43.tar.xz

./configure

 make 

 make install

您是否使用其他方式成功连接到您的 BLE 设备?像智能手机?有些应用程序可以读取 gatt 属性。

更新 2

我没有删除任何东西。仅在其余部分之上安装了 bluez5.43。当我 运行 bluetoothctl -v 它 returns : 5.43.

当我 运行 apt list --installed 时可以看到自动安装了以下软件包:

bluez/stable,now 5.23-2+rpi2 armhf [installed,automatic]
bluez-firmware/stable,now 1.2-3+rpi1 all [installed,automatic]
bluez-obexd/stable,now 5.23-2+rpi2 armhf [installed,automatic]
bluez-tools/stable,now 0.2.0~20140808-3 armhf [installed]

如您所见,我仍然安装了旧版本。

感谢 Constantin Chabirand,我成功了。我需要更多的命令行来让它工作,我需要更改我的 BLE 设备的广播频率(我仍然需要对此进行一些测试)。

这是我用来在我的 rpi3 上安装 bluez 并连接到 BLE 设备的确切命令:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install -y libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev

wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.43.tar.xz
tar xf bluez-5.43.tar.xz
cd bluez-5.43/

./configure
make
sudo make install
sudo reboot

# check version
bluetoothctl -v

sudo nano /lib/systemd/system/bluetooth.service
        # Add --experimental to this lane 
        ExecStart=/usr/local/libexec/bluetooth/bluetoothd --experimental

sudo systemctl daemon-reload
sudo systemctl restart bluetooth
sudo hciconfig hci0 up
sudo reboot

sudo usermod -G bluetooth -a pi
sudo reboot

就是这样。再次感谢您的帮助。

我需要安装 bluez-5.43 以解决在脚本中使用 bluetoothctl 时默认 bluez-5.23 中的错误。

您可能可以从 stretch 安装二进制版本,但我的经验是您经常 运行 进入其他依赖项。

我在 Ubuntu-16.04 下遇到了同样的问题,并使用了 Ubuntu-Zesty 的 deb-src 包来解决这个问题。由于这只是一个 debian 软件包,我也在 Raspberry pi 上使用了它,在使用 USB 蓝牙加密狗时工作正常。要在通过串行线连接蓝牙控制器的 RPI-3 上运行此功能,您还需要安装 ../issue/30 补丁。

这样做的好处是,一旦您的 Raspbian 包含更新的 bluez 版本,它将取代我们在这里所做的,如果您有更多的 raspbian 系统,您当然可以只需要安装生成的.deb包

这是我所做的(免提):

#!/bin/bash
sudo apt-get install devscripts debhelper dh-autoreconf flex bison libdbus-glib-1-dev libglib2.0-dev  libcap-ng-dev libudev-dev l
ibreadline-dev libical-dev check dh-systemd libebook1.2-dev

wget https://launchpad.net/ubuntu/+archive/primary/+files/bluez_5.43.orig.tar.xz
wget https://launchpad.net/ubuntu/+archive/primary/+files/bluez_5.43-0ubuntu1.debian.tar.xz
wget https://launchpad.net/ubuntu/+archive/primary/+files/bluez_5.43-0ubuntu1.dsc

tar xf bluez_5.43.orig.tar.xz
cd bluez-5.43
tar xf ../bluez_5.43-0ubuntu1.debian.tar.xz
# install patches relevant for rpi-3 bluetooth
. /etc/os-release
if [ $ID = raspbian ]; then
    wget https://gist.github.com/pelwell/c8230c48ea24698527cd/archive/3b07a1eb296862da889609a84f8e10b299b7442d.zip
    cd debian/patches
    unzip ../../3b07a1eb296862da889609a84f8e10b299b7442d.zip
    for i in c8230c48ea24698527cd-3b07a1eb296862da889609a84f8e10b299b7442d/*;do
        mv $i .
        basename $i >> series
    done
    rmdir c8230c48ea24698527cd-3b07a1eb296862da889609a84f8e10b299b7442d
    cd ../..
fi
# end of Raspian related patches
debchange --local=~lorenzen 'Backport to Xenial'
debuild -b -j4
cd ..
sudo dpkg -i *.deb

这应该也适用于其他 Debian 派生系统, https://askubuntu.com/a/884062/655086

我在使用 Raspberry Pi 3 内置蓝牙连接和播放 BLE 设备时遇到了同样的问题。首先,我通过下面的网页学习了如何播放和控制bluetoothctl的一个工具。

[https://mcuoneclipse.com/2016/12/19/tutorial-ble-pairing-the-raspberry-pi-3-model-b-with-hexiwear/][1]

在本教程中,与您的 BLE 设备成功连接后,您可以使用您想要玩的属性。

比如你要使用蓝牙UART TX或RX的属性,你只需要在下面输入命令,然后读取或写入该特性的属性值即可。

list-attributes 00:34:40:0A:00:4E 
select-attribute /org/bluez/hci0/dev_00_32_40_08_00_12/service0026/char0027
read 
write

如果有人完全了解在 Raspberry Pi 3 内置 BLE 与 Arduino Ble 之间创建蓝牙连接,请分享您的知识。 ;)