【问题标题】:Bluetooth pairing using fixed PIN on bluez 5在 bluez 5 上使用固定 PIN 进行蓝牙配对
【发布时间】:2018-03-22 07:26:58
【问题描述】:

我正在尝试通过蓝牙连接 2 个或更多 Raspberry Pi 3 板。我正在寻找在配对时设置安全性的选项。我正在使用 Raspian-stretch(可用的最新版本)。 RPI-3 上可用的 Bluez 版本为 5.23(如 bluetoothd -v 命令所示)。

我正在使用无头版本。我希望配对得到保护(意味着应该有某种身份验证我可以设置,如 PIN(4 位)或密码(6 位)),而无需用户登录。因此,如果我必须将手机连接到 RPI,则无需登录 RPI 即可输入 PIN/密码。 然后我想设置蓝牙 PAN 网络,以便我可以在连接到 PAN 网络的设备之间进行通信。

我想使用系统文件中可用的 PIN 或我可以指向的某个位置来配对设备。例如,/temp/ 目录中的 pin.txt 文件或通过运行代理来设置 PIN。我从其他帖子中读到 bluez5.x 摆脱了 bluez 早期版本中使用的蓝牙代理来完成我可以完成的事情。

bluetoothctl 中的代理,例如 DisplayOnly、KeyboardDisplay、NoInputNoOutput、DisplayYesNo、KeyboardOnly,要么设置必须手动输入的动态密钥或确认密钥,要么在 NoInputNoOutput 的情况下让任何设备无需任何身份验证即可配对和连接.

这是我在该论坛找到的链接,说明该代理不再可用: https://www.raspberrypi.org/forums/viewtopic.php?t=133961 我还提到了一些显示设备配对的示例,但没有解决我正在寻找的内容。

联机帮助页上也没有可用的信息。 https://manpages.debian.org/stretch/bluez/bluetoothctl.1.en.html

这是我发现的有关命令的内容,但仍然不是我想要的。 https://wiki.archlinux.org/index.php/Bluetooth

我还发布了这个 Raspberry Pi 论坛。链接在这里: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=195090

感谢任何帮助或建议以解决此问题或我可以参考的文档链接。

提前致谢。

【问题讨论】:

    标签: bluetooth raspbian raspberry-pi3 bluez pairing


    【解决方案1】:

    在摆弄 BlueZ 5 几天后,这就是我所拥有的。 使用 BlueZ 5.50 和 Raspbian Stretch(Pi 零 W):

    使用 --compat 启动蓝牙:

    附加到 /etc/systemd/system/dbus-org.bluez.service 中的 ExecStart

    在 rc.local 中:sudo bluetoothd --compat &


    接下来的步骤由下面发布的代码处理,但为了澄清,需要将 hciconfig 设置为:

    sudo hciconfig hci0 sspmode 0

    注意 #1: 使用 'sspmode 1' 从 Android 配对时,您会收到输入 PIN 的提示,但之后 Pi 会自动生成 6 位密码并配对失败。

    注意 #2: hciconfig hci0 不能使用 authencrypt 设置,它们将实际注册agent DisplayOnly(我们将在下一步创建代理)作为 KeyboardDisplay(sudo btmon 进行验证)并且配对不会使用预定义的 PIN。不确定 DisplayOnly 是否有原因无法使用身份验证、加密(可能与他们设置安全模式 3 有关)。

    之后我们将使用 bluetoothctl

    pi@raspberrypi:~ $ bluetoothctl
    Agent registered
    [bluetooth]# agent off
    Agent unregistered
    [bluetooth]# agent DisplayOnly
    Agent registered
    [bluetooth]# default-agent
    Default agent request successful
    [bluetooth]# discoverable on
    Changing discoverable on succeeded
    [CHG] Controller 11:22:33:44:55:66 Discoverable: yes
    [bluetooth]# pairable on
    Changing pairable on succeeded
    [CHG] Controller 11:22:33:44:55:66 Pairable: yes
    
    // Initiate pairing on remote device //
    
    [NEW] Device AA:BB:CC:DD:EE:FF Android_phone
    
    // Enter any PIN on Device AA:BB:CC:DD:EE:FF
    
    Request PIN code
    
    // retype your PIN below (on Pi)
    [agent] Enter PIN code: <your PIN> 
    [CHG] Device AA:BB:CC:DD:EE:FF Class: 0x005a020c
    ...
    [CHG] Device AA:BB:CC:DD:EE:FF Paired: yes
    [bluetooth]# quit
    

    注意事项 #3: 使用 pexpect 注册代理(如果您尝试运行下面发布的代码,请注意)和 BlueZ 5.43(Stretch 中的默认版本)是命中注定的


    以下是设置 sspmode 并处理与预生成 PIN 配对的 Python 2.7 代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from __future__ import print_function   # import print from python3: end=""
    import time   
    import re
    import pexpect    # sudo apt-get install python-pexpect
    import subprocess
    import random
    # !!! make sure bluetoothd runs in --compat mode before executing this script !!!
    def pair_with_pin(start_time, pin, time_limit=60):  # int(time.time()), pin - \d{4}, time_limit - approximate pairing window time in seconds, it might take up to 2x (nested timeout conditions)
        "exectutes pairing with entered PIN on bluetooth adapter side"
        pairing_status = False
        try:
            subprocess.call(['sudo','hciconfig','hci0','sspmode', '0'])
            
            # bluetoothctl 
            child = pexpect.spawn('bluetoothctl')
            child.expect("#")
            child.sendline('agent off') # might be unnecessary
            child.expect("unregistered")
            
            child.sendline('agent DisplayOnly')
            child.expect("Agent registered")
            child.sendline('pairable on')
            child.expect("pairable on succeeded")
            child.sendline('discoverable on')
            child.expect("discoverable on succeeded")
            child.sendline('default-agent')
            print ('Please input PIN: ' + pin)              
            
            # waiting for Phone to send a pairing request... 
            child.expect('Enter PIN code:', timeout = time_limit )   # timeout <= PAIRING_TIME_LIMIT to keep some kind of logic
            while int(time.time()) < start_time + time_limit:   # allow multiple pairing attempts during pairing window            
                child.sendline(pin)
                i = child.expect(['Paired: yes', 'Enter PIN code:'], timeout = time_limit)
                if i == 0: # found 'Paired: yes' == successful pairing
                    trust_mac = 'trust ' + re.search(r'(?:[0-9a-fA-F]:?){12}.+$', child.before).group(0)    # extract MAC from last line, one with 'Paired: Yes'
                    child.sendline(trust_mac)   # optionally add device to trusted
                    child.expect('trust succeeded', timeout = 10)                
                    pairing_status = True
                    break
                #else: # i == 1
                    # print('wrong PIN, retrying if time will allow') 
        except pexpect.EOF:
            print ('!!!!!!!! EOF')
        except pexpect.TIMEOUT:
            print ('!!!!!!!! TIMEOUT')
            
        # hide Pi's bluetooth for security reasons
        child.sendline('pairable off')
        child.expect("pairable off succeeded")
        child.sendline('discoverable off')
        child.expect("discoverable off succeeded")    
        child.close()
        
        return pairing_status
    
    #main program body
    PAIRING_TIME_LIMIT = 60
    BT_PIN = random.randint(1000,10000)    # generate random 4-digit PIN 1000..9999
    
    status = pair_with_pin(int(time.time()), str(BT_PIN), PAIRING_TIME_LIMIT)
    if status == True:
        print('Pairing successful')

    最后说明:成功配对后可能可以开启加密,尝试:

    hciconfig hci0 加密

    hcitool enc $BDADD

    【讨论】:

      【解决方案2】:

      我能够使用测试脚本来完成这项工作。

      有兴趣了解详情的朋友可以参考我在树莓派论坛上的帖子。下面是链接。

      https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=195090&p=1221455#p1221455

      【讨论】:

        【解决方案3】:

        首先您必须配置 sspmode 0,对于 pin 请求:hciconfig hci0 sspmode 0

        并使用 bt-agent 应用程序(您也可以作为守护程序运行):

        bt-agent -c NoInputNoOutput -p /root/bluethooth.cfg

        编辑文件配置,可以输入mac地址和pin:例如:XX:XX:XX:XX:XX:XX 1234

        或者,如果您希望所有设备的 pin 码具有相同的 pin 码,例如 1234,请像这样编辑文件:* 1234

        这对我有用!

        【讨论】:

          猜你喜欢
          • 2014-03-10
          • 2013-01-27
          • 1970-01-01
          • 2014-09-14
          • 1970-01-01
          • 1970-01-01
          • 2013-07-17
          • 2015-02-02
          相关资源
          最近更新 更多