debian下藍牙適配器的配置和使用


本文打算將藍牙適配器和手機藍牙進行配對。

 

買了個支持藍牙4.0協議的藍牙適配器,將其插入到pc(debian 7.4)的usb口。

 

查看手機藍牙信息:

選擇手機中"設置"->"關於手機"->"狀態信息"可以看到藍牙地址是18:DC:56:D2:1C:1A

 

pc上安裝藍牙工具:

sudo apt-get install bluez bluez-hcidump

 

查看系統輸出信息:

$ dmesg | tail -n 4
[25259.408121] usb 1-1.2: new full-speed USB device number 32 using ehci_hcd
[25259.614615] usb 1-1.2: New USB device found, idVendor=0a12, idProduct=0001
[25259.614621] usb 1-1.2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[25259.614626] usb 1-1.2: Product: CSR8510 A10

 

查看藍牙適配器設備信息:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB
Bus 001 Device 031: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 002 Device 003: ID 046d:c05a Logitech, Inc. Optical Mouse M90
Bus 003 Device 002: ID 413c:2003 Dell Computer Corp. Keyboard
$ lsusb -s 001:031 -v

Bus 001 Device 031: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Couldn't open device, some information will be missing
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass          224 Wireless
  bDeviceSubClass         1 Radio Frequency
  bDeviceProtocol         1 Bluetooth
  bMaxPacketSize0        64
  idVendor           0x0a12 Cambridge Silicon Radio, Ltd
  idProduct          0x0001 Bluetooth Dongle (HCI mode)
  bcdDevice           88.91
  iManufacturer           0 
  iProduct                2 
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength          177
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xe0
      Self Powered
      Remote Wakeup
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0010  1x 16 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       1
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       2
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       3
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       4
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       5
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1

 

安裝bluez后,會提供兩個工具hciconfig和hcitool進行藍牙相關的配置。

查看當前藍牙適配器參數配置:

$ sudo hciconfig -a
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
    UP RUNNING PSCAN 
    RX bytes:3639 acl:0 sco:0 events:169 errors:0
    TX bytes:745 acl:0 sco:0 commands:47 errors:0
    Features: 0xff 0xff 0x8f 0xfe 0xdb 0xff 0x5b 0x87
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
    Link policy: RSWITCH HOLD SNIFF PARK 
    Link mode: SLAVE ACCEPT 
    Name: 'debian-0'
    Class: 0x420100
    Service Classes: Networking, Telephony
    Device Class: Computer, Uncategorized
    HCI Version: 4.0 (0x6)  Revision: 0x22bb
    LMP Version: 4.0 (0x6)  Subversion: 0x22bb
    Manufacturer: Cambridge Silicon Radio (10)

藍牙適配器的mac地址是: 00:1A:7D:DA:71:13,名稱是debian-0。

查看藍牙適配器提供的各種功能:

$ sdptool browse local
Browsing FF:FF:FF:00:00:00 ...
Service Name: SIM Access Server
Service RecHandle: 0x10000
Service Class ID List:
  "SIM Access" (0x112d)
  "Generic Telephony" (0x1204)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 8
Profile Descriptor List:
  "SIM Access" (0x112d)
    Version: 0x0101

Service Name: Headset Audio Gateway
Service RecHandle: 0x10001
Service Class ID List:
  "Headset Audio Gateway" (0x1112)
  "Generic Audio" (0x1203)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 12
Profile Descriptor List:
  "Headset" (0x1108)
    Version: 0x0102

Service Name: Hands-Free Audio Gateway
Service RecHandle: 0x10002
Service Class ID List:
  "Handsfree Audio Gateway" (0x111f)
  "Generic Audio" (0x1203)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 13
Profile Descriptor List:
  "Handsfree" (0x111e)
    Version: 0x0105

Service Name: AVRCP TG
Service RecHandle: 0x10003
Service Class ID List:
  "AV Remote Target" (0x110c)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 23
  "AVCTP" (0x0017)
    uint16: 0x103
Profile Descriptor List:
  "AV Remote" (0x110e)
    Version: 0x0103

Service Name: AVRCP CT
Service RecHandle: 0x10004
Service Class ID List:
  "AV Remote" (0x110e)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 23
  "AVCTP" (0x0017)
    uint16: 0x103
Profile Descriptor List:
  "AV Remote" (0x110e)
    Version: 0x0100

Service Name: Dial-Up Networking
Service RecHandle: 0x10005
Service Class ID List:
  "Dialup Networking" (0x1103)
  "Generic Networking" (0x1201)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 1
Profile Descriptor List:
  "Dialup Networking" (0x1103)
    Version: 0x0100

Service Name: OBEX Object Push
Service RecHandle: 0x10006
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 9
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Service Name: OBEX File Transfer
Service RecHandle: 0x10007
Service Class ID List:
  "OBEX File Transfer" (0x1106)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 10
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX File Transfer" (0x1106)
    Version: 0x0100

 

 

掃描藍牙設備:

$ hcitool scan
Scanning ...
    C4:17:FE:F6:65:FF    PC-201209031236
    CC:52:AF:4F:52:16    HR-6F1397D64651
    18:DC:56:D2:1C:1A    Coolpad 8297

使用hcitool獲取手機藍牙信息:

$ hcitool name 18:DC:56:D2:1C:1A
Coolpad 8297
$ sudo hcitool info 18:DC:56:D2:1C:1A
Requesting information ...
    BD Address:  18:DC:56:D2:1C:1A
    Device Name: Coolpad 8297
    LMP Version: 4.0 (0x6) LMP Subversion: 0x0
    Manufacturer: MediaTek, Inc. (70)
    Features page 0: 0xbf 0x3e 0x8d 0xfe 0xdb 0xff 0x5b 0x87
        <3-slot packets> <5-slot packets> <encryption> <slot offset> 
        <timing accuracy> <role switch> <sniff mode> <RSSI> 
        <channel quality> <SCO link> <HV2 packets> <HV3 packets> 
        <CVSD> <power control> <transparent SCO> <broadcast encrypt> 
        <EDR ACL 2 Mbps> <EDR ACL 3 Mbps> <enhanced iscan> 
        <interlaced iscan> <interlaced pscan> <inquiry with RSSI> 
        <extended SCO> <EV4 packets> <EV5 packets> <AFH cap. slave> 
        <AFH class. slave> <LE support> <3-slot EDR ACL> 
        <5-slot EDR ACL> <sniff subrating> <pause encryption> 
        <AFH cap. master> <AFH class. master> <EDR eSCO 2 Mbps> 
        <EDR eSCO 3 Mbps> <3-slot EDR eSCO> <extended inquiry> 
        <LE and BR/EDR> <simple pairing> <encapsulated PDU> 
        <non-flush flag> <LSTO> <inquiry TX power> <EPC> 
        <extended features> 
    Features page 1: 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00
    Features page 2: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

 

配置藍牙適配器使得能被其他設備搜索到:

$ sudo hciconfig hci0 iscan

這時在手機中搜索藍牙設備可以搜索到"debian-0",但是使用手機配對時提示“PIN或者配對密鑰不正確,無法和debian-0進行配對"。

 

在執行下面幾條命令:

$ sudo hciconfig hci0 pscan
$ sudo hciconfig hci0 noencrypt
$ sudo hciconfig hci0 noauth

然后在手機里可以對"debian-0"配對即可配對成功。

此時可以在pc上執行l2ping命令 :

host@debian:~$ sudo l2ping  -c 3 18:DC:56:D2:1C:1A
[sudo] password for host: 
Ping: 18:DC:56:D2:1C:1A from 00:1A:7D:DA:71:13 (data size 44) ...
44 bytes from 18:DC:56:D2:1C:1A id 0 time 15.00ms
44 bytes from 18:DC:56:D2:1C:1A id 1 time 26.16ms
44 bytes from 18:DC:56:D2:1C:1A id 2 time 26.14ms
3 sent, 3 received, 0% loss

 

如果不使用上面的hciconfig命令來進行配置,也可以使用blueman或者gnome-bluetooth來配置藍牙,安裝這些工具:

sudo apt-get install blueman gnome-bluetooth

這兩個工具都是圖形化界面,使用起來相當方便,只是需要從手機端發起配對請求,然后在pc端和手機端分別進行確認即完成配對。

然后可以在pc端用藍牙來向手機發送文件。

 

上面這兩種情況下都是在手機端發起配對請求,之后也只是能通過藍牙發文件。

但我希望能夠使用bluez系列工具中提供的命令行工具從pc端發起配對請求,以便於以后將藍牙適配器用到嵌入式開發板上。

參考《在linux下藍牙使用rfcomm連接》 、《BluetoothUser》以及《Bluetooth_Mouse》進行操作,但都沒成功,

這是因為我使用的bluez版本是4.99,沒有bluez5中的bluetoothctl工具,也沒有bluez4中的hidd工具,也沒有常用的bluez3中的/etc/bluetooth/hcid.conf配置文件,

進行配對時總是會因為pin或者key的問題而導致配對失敗,導致網絡上大部分教程都不適用於這個版本。

 

最終我選用了另一種方式,先從手機來對藍牙設備進行配對,然后將其模式修改成master,再從pc下執行命令就可以了。

首先配置rfcomm,修改/etc/bluetooth/rfcomm.conf(其實我不確定是否需要該配置,只是在參考網絡文章時修改了這個配置文件):

host@debian:~$ cat /etc/bluetooth/rfcomm.conf 
#
# RFCOMM configuration file.
#

rfcomm0 {
    # Automatically bind the device at startup
    bind no;

    # Bluetooth address of the device
    device 18:DC:56:D2:1C:1A;

    # RFCOMM channel for the connection
    channel    1;

    # Description of the connection
    comment "Example Bluetooth device";
}

重啟藍牙服務:

sudo /etc/init.d/bluetooth restart

可以使用hcidump(類似於tcpdump工具能夠打印bluez中每步都執行了什么功能,可以用於調試):

sudo hcidump -Xt

按照前面的步驟從手機來對藍牙適配器進行配對,可以配對成功。

查看hci0(類似於網卡的eth0接口)的連接模式(link mode,可以是master或者slave):

$ sudo hciconfig hci0 lm
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
    Link mode: SLAVE ACCEPT 

將其連接模式從SLAVE修改為master:

sudo hciconfig hci0 lm master

這時查看連接模式:

$ sudo hciconfig hci0 lm
[sudo] password for host: 
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
    Link mode: MASTER 

已經將link mode修改為master模式。

使用rfcomm來連接手機藍牙設備:

$ sudo rfcomm connect hci0 18:DC:56:D2:1C:1A
Connected /dev/rfcomm0 to 18:DC:56:D2:1C:1A on channel 1
Press CTRL-C for hangup

連接成功,查看連接信息:

$ sudo hcitool  con
Connections:
    < ACL 18:DC:56:D2:1C:1A handle 72 state 1 lm MASTER AUTH ENCRYPT 

可以執行hcitool中的cmd命令,例如下面的命令將當前設備的名稱改成"ABCD":

$ sudo hcitool cmd 0x03 0x0013 0x41 0x42 0x43 0x44
< HCI Command: ogf 0x03, ocf 0x0013, plen 4
  41 42 43 44 
> HCI Event: 0x0e plen 4
  01 13 0C 00 

此時在hcidump對應窗口顯示下面的信息:

2014-06-18 15:04:22.929405 < HCI Command: Write Local Name (0x03|0x0013) plen 4
    name 'ABCDBCD'
2014-06-18 15:04:22.930381 > HCI Event: Command Complete (0x0e) plen 4
    Write Local Name (0x03|0x0013) ncmd 1
    status 0x00
2014-06-18 15:04:22.930474 < HCI Command: Read Local Name (0x03|0x0014) plen 0
2014-06-18 15:04:22.947378 > HCI Event: Command Complete (0x0e) plen 252
    Read Local Name (0x03|0x0014) ncmd 1
    status 0x00 name 'ABCD'

使用hciconfig工具也可以查看到當前藍牙適配器名稱:

$ sudo hciconfig hci0 name
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
    Name: 'ABCD'

可以看到藍牙適配器的名稱已經被改成ABCD了。

還有其他很多命令都可以用hcitool hci0 cmd來進行配置,《利用 hcitool 通過hci 命令與芯片交互》中提供了其他的一些命令,感興趣的話可以自己玩玩看。

也可以下載《藍牙4.0規范》,從704-1103頁有關於藍牙命令的詳細說明。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM