美文网首页
Linux BLE使用说明

Linux BLE使用说明

作者: Mr_Michael | 来源:发表于2020-06-15 16:56 被阅读0次

    1.启动蓝牙适配器与搜索蓝牙

    # 查看蓝牙适配器
    $ hciconfig
    hci0:   Type: Primary  Bus: USB
        BD Address: 20:32:33:3C:2B:27  ACL MTU: 1021:6  SCO MTU: 255:12
        UP RUNNING 
        RX bytes:4404 acl:19 sco:0 events:310 errors:0
        TX bytes:29537 acl:19 sco:0 commands:246 errors:0
    # 开启蓝牙适配器
    $ sudo hciconfig hci0 up
    #关闭蓝牙适配器
    $sudo  hciconfig hci0 down
    
    # 搜索普通蓝牙
    $ hcitool scan
    # 搜索BLE蓝牙
    $ sudo hcitool lescan
    LE Scan ...
    55:D8:80:D4:47:8D (unknown)
    72:AB:2E:49:75:93 (unknown)
    DD:0D:30:00:18:9D (unknown)
    5C:90:AD:B6:40:9C (unknown)
    64:BB:A7:4B:B4:B2 (unknown)
    35:1A:4A:72:C8:9A (unknown)
    1C:99:C7:EE:32:FF (unknown)
    77:3E:2C:8E:F7:F8 (unknown)
    4E:BC:B7:7F:E9:CE (unknown)
    DD:0D:30:00:18:9D FSC-BT901-LE
    F4:52:53:51:F5:39 iVS101 F539 
    77:3E:2C:8E:F7:F8 (unknown)
    F4:52:53:51:F5:39 (unknown)
    

    2.使用gatttool与BLE设备通讯

    # 与BLE设备交互式通讯
    $ sudo gatttool -t random -b F4:52:53:51:F5:39 -I
    [F4:52:53:51:F5:39][LE]> help 
    help                                           Show this help
    exit                                           Exit interactive mode
    quit                                           Exit interactive mode
    connect         [address [address type]]       Connect to a remote device
    disconnect                                     Disconnect from a remote device
    primary         [UUID]                         Primary Service Discovery
    included        [start hnd [end hnd]]          Find Included Services
    characteristics [start hnd [end hnd [UUID]]]   Characteristics Discovery
    char-desc       [start hnd] [end hnd]          Characteristics Descriptor Discovery
    char-read-hnd   <handle>                       Characteristics Value/Descriptor Read by handle
    char-read-uuid  <UUID> [start hnd] [end hnd]   Characteristics Value/Descriptor Read by UUID
    char-write-req  <handle> <new value>           Characteristic Value Write (Write Request)
    char-write-cmd  <handle> <new value>           Characteristic Value Write (No response)
    sec-level       [low | medium | high]          Set security level. Default: low
    mtu             <value>                        Exchange MTU for GATT/ATT
    
    # 连接BLE
    [F4:52:53:51:F5:39][LE]> connect
    Connection successful
    
    # 寻找BLE设备中可用的服务
    [F4:52:53:51:F5:39][LE]> primary
    #attr handle是服务的句柄,uuid是服务的类型标识
    attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb # generic access 服务
    attr handle: 0x0008, end grp handle: 0x0008 uuid: 00001801-0000-1000-8000-00805f9b34fb # generic attribute 服务
    attr handle: 0x0009, end grp handle: 0xffff uuid: 6e400001-b5a3-f393-e0a9-e50e24dcca9e # 未知服务
    
    # 查看设备服务的特性
    # handle是特性的句柄,char properties是特性的权限,char value handle是特性的数值句柄,uuid是特性的类型标识
    [F4:52:53:51:F5:39][LE]> characteristics
    handle: 0x0002, char properties: 0x0a, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb # Device Name
    handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb # Appearance
    handle: 0x0006, char properties: 0x02, char value handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb # Peripheral Preferred Connection Parameters
    handle: 0x000a, char properties: 0x10, char value handle: 0x000b, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
    handle: 0x000d, char properties: 0x0c, char value handle: 0x000e, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
    handle: 0x000f, char properties: 0x0c, char value handle: 0x0010, uuid: 6e400004-b5a3-f393-e0a9-e50e24dcca9e
    
    # 通过特性的数值句柄(char value handle)发送数据
    [F4:52:53:51:F5:39][LE]> char-write-req 0x000e FFFFFFFFFF0B0108010102010301010103
    Characteristic value was written successfully
    
    # 通过特性的数值句柄(char value handle)读取数值
    [F4:52:53:51:F5:39][LE]> char-read-hnd 0x000b
    Characteristic value/descriptor: f9 21 92 19 21 00 65 01 49 49 21 09 20 09 21 09 22 09 23 09
    
    # 设置安全等级为高,可以让设备长时间 Ubuntu保持连接
    [F4:52:53:51:F5:39][LE]> sec-level high
    
    # 断开连接
    [F4:52:53:51:F5:39][LE]> disconnect
    

    3.与BLE设备非交互式通讯

    $ gatttool --help-all
    Usage:
      gatttool [OPTION?]
    
    Help Options:
      -h, --help                                Show help options
      --help-all                                Show all help options
      --help-gatt                               Show all GATT commands
      --help-params                             Show all Primary Services/Characteristics arguments
      --help-char-read-write                    Show all Characteristics Value/Descriptor Read/Write arguments
    
    GATT commands
      --primary                                 Primary Service Discovery
      --characteristics                         Characteristics Discovery
      --char-read                               Characteristics Value/Descriptor Read
      --char-write                              Characteristics Value Write Without Response (Write Command)
      --char-write-req                          Characteristics Value Write (Write Request)
      --char-desc                               Characteristics Descriptor Discovery
      --listen                                  Listen for notifications and indications
    
    Primary Services/Characteristics arguments
      -s, --start=0x0001                        Starting handle(optional)
      -e, --end=0xffff                          Ending handle(optional)
      -u, --uuid=0x1801                         UUID16 or UUID128(optional)
    
    Characteristics Value/Descriptor Read/Write arguments
      -a, --handle=0x0001                       Read/Write characteristic by handle(required)
      -n, --value=0x0001                        Write characteristic value (required for write operation)
    
    Application Options:
      -i, --adapter=hciX                        Specify local adapter interface
      -b, --device=MAC                          Specify remote Bluetooth address
      -t, --addr-type=[public | random]         Set LE address type. Default: public
      -m, --mtu=MTU                             Specify the MTU size
      -p, --psm=PSM                             Specify the PSM for GATT/ATT over BR/EDR
      -l, --sec-level=[low | medium | high]     Set security level. Default: low
      -I, --interactive                         Use interactive mode
    
    
    # 寻找BLE设备中可用的服务
    $  sudo gatttool -t random -b F4:52:53:51:F5:39 --primary
    attr handle = 0x0001, end grp handle = 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
    attr handle = 0x0008, end grp handle = 0x0008 uuid: 00001801-0000-1000-8000-00805f9b34fb
    attr handle = 0x0009, end grp handle = 0xffff uuid: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
    
    # 查看设备服务的特性
    $  sudo gatttool -t random -b F4:52:53:51:F5:39 --characteristics
    
    # 通过特性的数值句柄(char value handle)读取数值
    $ sudo gatttool -t random -b F4:52:53:51:F5:39 --char-read -a 0x000b
    
    #  通过特性的数值句柄(char value handle)发送数据
    $ sudo gatttool -t random -b F4:52:53:51:F5:39 --char-write-req -a 0x000e -n FFFFFFFFFF0B0108010102010301010103
    

    4.BLE的UUID

    低功耗蓝牙使用的UUID被分为5组:

    • 0x1800 ~ 0x26FF:服务类型;
    • 0x2700 ~ 0x27FF:计量单位;
    • 0x2800 ~ 0x28FF:属性类型;
    • 0x2900 ~ 0x29FF:特性描述;
    • 0x2A00 ~ 0x7FFF:特性类型;

    下面是SIG官网对各组UUID定义的链接地址:

    相关文章

      网友评论

          本文标题:Linux BLE使用说明

          本文链接:https://www.haomeiwen.com/subject/vgrhxktx.html