【问题标题】:Connect BLE devices with Raspberry pi 3 B将 BLE 设备与 Raspberry pi 3 B 连接
【发布时间】:2017-06-02 02:35:44
【问题描述】:

我知道这不是第一次被问到这个问题,但在一周内尽我所能尝试之后,我仍然遇到问题。 我正在尝试将我的 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)

感谢您的帮助

编辑康斯坦丁·查比兰德的答案

$ 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

【问题讨论】:

  • 好吧,此时您的 BLE 设备不允许连接,或者 bluez 无法在您的 rpi 上运行。您是否尝试使用 bluetoothctl 连接到另一台设备?有效果吗? “info XX:XX:XX:XX:XX”是否返回有关您设备的正确信息?
  • 我用其他设备试过,每次都连接失败。而“info XX:XX:XX:XX:XX”给了我正确的信息(至少对我来说没有什么奇怪的)。我也尝试过使用其他 rpi,结果相同:s

标签: raspberry-pi bluetooth-lowenergy raspberry-pi3 bluez


【解决方案1】:

我遇到了同样的问题,即使用 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 之间创建蓝牙连接,请分享您的知识。 ;)

【讨论】:

    【解决方案2】:

    在脚本中使用 bluetoothctl 时,我需要安装 bluez-5.43 以解决默认 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

    【讨论】:

      【解决方案3】:

      感谢 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
      

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

      【讨论】:

        【解决方案4】:

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

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

        我还使用树莓派 3 连接到 BLE 设备,我不需要补丁。使用 bluez5.43 开始全新安装并发布我编写的两个命令的结果


        更新

        对于安装,我以最简单的方式进行了安装。我遵循了 INSTALL 文件中的“编译这个包的最简单方法是:”部分:

         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 它返回: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]
        

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

        【讨论】:

        • 感谢您的回答。我添加了您要求的结果。每个想法似乎都运作良好,但没有联系。我将尝试没有补丁的其他安装。你有一个链接解释你所做的安装吗?
        • 你在安装新版本之前卸载了bluez吗?
        • 是的,我有几个连接在一起的设备和应用程序。我正在尝试您的简单解决方案。
        猜你喜欢
        • 2016-01-02
        • 2016-04-30
        • 1970-01-01
        • 2014-02-10
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        相关资源
        最近更新 更多