首页 文章

在bluez 5上使用固定PIN进行蓝牙配对

提问于
浏览
0

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

我正在使用无头版本 . 我希望配对是安全的(在我没有用户登录的情况下,我应该设置某种身份验证,如PIN(4位)或密码(6位)) . 因此,如果我必须将手机连接到RPI,我无需登录RPI以输入PIN / Passkey . 然后我想 Build 蓝牙PAN网络,以便我可以连接到连接到PAN网络的设备之间 .

我希望使用PIN在设备中配对,该PIN可以在系统中的文件中找到,也可以在某个地方指向 . 例如,/ temp /目录中的pin.txt文件或运行代理来设置PIN . 我从其他帖子中读到,bluez5.x摆脱了早期版本bluez中使用的bluetooth-agent来完成我可以完成的事情 .

蓝牙技术中的代理,例如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

任何有关此问题的帮助或建议或我可以参考的documnets链接表示赞赏 .

提前致谢 .

3 回答

  • -1

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

    --compat 开始bluetoothd:

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

    要么

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


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

    sudo hciconfig hci0 sspmode 0

    Note #1: 使用'sspmode 1 '从Android配对时,您会收到PIN提示,但之后Pi会自动生成6位密码和配对错误 .

    Note #2: hciconfig hci0 无法设置 authencrypt 那些实际上会注册 agent DisplayOnly (我们将在下一步创建代理)作为 KeyboardDisplay (sudo btmon验证)和配对赢得't use predefined PIN. Not sure if there is a reason why DisplayOnly can' t使用auth,加密(可能与某些事情有关)他们设置安全模式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
    

    Note #3: 使用pexpect注册代理(如果你尝试运行下面发布的代码只是一个注释),使用BlueZ 5.43(Stretch中的默认版本)就会命中并错过


    下面是一个Python 2.7代码,它设置sspmode并处理与预生成的PIN的配对:

    #!/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')
    

    Final note: 成功配对后, might 可能会启用加密功能,请尝试:

    hciconfig hci0加密

    要么

    hcitool enc $ BDADD

  • 1

    我能够使用测试脚本 .

    如果您有兴趣了解详细信息,请参阅我在Raspberry Pi论坛上的帖子 . 以下是链接 .

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

  • 0

    首先你需要配置sspmode 0,用于引脚请求:hciconfig hci0 sspmode 0

    并使用bt-agent aplicattion(你也可以作为deamon运行):

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

    编辑文件配置,你可以把tha mac地址和pin:例如:XX:XX:XX:XX:XX:XX 1234

    或者,如果您希望所有设备的引脚都使用相同的引脚代码(例如1234),请按以下方式编辑文件:* 1234

    这项工作对我来说!

相关问题