linux下使用藍牙設備 bluetooth hciconfig hcitool
linux下使用藍牙設備 bluetooth hciconfig hcitool2012-02-14 13:26
1 載入藍牙模塊 需要載入的模塊有bluetooth、 hci_uart、 l2cap、 rfcomm、 sco、 bnep,位於/lib/modules/`uname-r`/kernel/net/bluetooth 和/lib/modules/`uname -r`/kernel/driver/bluetooth注:可以使用 bt_ins.sh 與 bt_rm.sh 兩個腳本,進行藍牙模塊的添加與刪除#!/bin/bash#name: bt_in.sh#author:young#date: 2007-01-16#decription: insmod for bt modulesinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/bluetooth.koinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/l2cap.koinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/sco.koinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/bnep/bnep.koinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/rfcomm/rfcomm.koinsmod /lib/modules/`uname -r`/kernel/drivers/bluetooth/hci_uart.kolsmod | grep bluetooth#!/bin/bash#name: bt_rm.sh#author:young#date: 2007-01-16#description: rmmod bt modules from kernelrmmod hci_uartrmmod bneprmmod scormmod rfcommrmmod l2caprmmod bluetoothlsmod 2 重啟Bluetooth系統服務 # /etc/init.d/bluetooth restartRestarting Bluetooth services: [ OK ] 3 邦定tty設備 # hciattach -n /dev/ttyUSB0 ericsson 57600 & 注: 成功後就不要再次邦定tty設備了 命令hciconfig提供有關本地設備的詳細信息。如果不帶任何參數執行 hciconfig,則輸出將顯示設備名 (hciX) 等設備信息、物理設備地址(12位數字,形式為00:12:34:56:78)和有關已傳送數據量的信息。hciconfig hci0 name顯示當您的計算機接收來自遠程設備的請求時它返回的名稱。除查詢本地設備的設置外,hciconfig還可用於修改這些設置。例如,hciconfig hci0 name TEST 將名稱設置為TEST。# hciconfig –ahci0: Type: UART BD Address: 00:08:AC:03:68:77 ACL MTU: 192:8 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:925 acl:6 sco:0 events:35 errors:0 TX bytes:488 acl:5 sco:0 commands:22 errors:0 Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH HOLD SNIFF PARK Link mode: SLAVE ACCEPT Name: "PC159-0" Class: 0x120104 Service Classes: Networking, Object Transfer Device Class: Computer, Desktop workstation HCI Ver: n/a (0x3) HCI Rev: 0xa5c LMP Ver: n/a (0x3) LMP Subver: 0xa5c Manufacturer: Cambridge Silicon Radio (10) 說明:如果沒有看到這樣的信息,那麼確認一下hcid是否正在運行,並在文件/var/log/messages中是否有錯誤信息。適配器的BD Address是藍牙識別符的唯一標識,其原理與乙太網的MAC地址類似。 4 激活藍牙模塊 # hciconfig hci0 up 5 查看藍牙模塊信息(是否正確被識別,是否工作正常) # hcitool devDevices: hci0 00:1B:35:00:12:34 6 查詢周圍藍牙設備 # hcitool scanScanning ... 00:E0:91:74:2E:64 KE85000:08:AC:03:58:A6 DTK001 00:19:2D:61:12:5E N73-MMI找的藍牙設備硬體地址與設備名 7 建立虛擬串口設備 # mknod /dev/rfcomm0 c 216 1# chmod 666 /dev/rfcomm0需要設置minicomSerial Device : /dev/rfcomm0 8 修改rfcomm.conf,添加如下內容 # vi /etc/bluetooth/rfcomm.conf rfcomm0{ bind no; device -00:1B:35:00:12:34; channel 1; comment "HCI003"}說明: bind no 是設置是否自動綁定設備; device -是設置綁定設備的地址; channel 是設置設備的通道; comment 是對設備的描述。注意:這個device的地址是本地藍牙設備的地址。 9 添加sdp協議通道 # sdptool add --channel=1 DID SP DUN LAN FAX OPUSH FTP HS HF SAP NAP GN PANU HID CIP CTP A2SRC A2SNK SYNCML NOKID PCSUITE SR1說明:後面的參數不一定全部支持,簡單起見默認將服務全部打開。 10 使用虛擬串口與外界藍牙設備綁定 格式:# rfcomm bind /dev/rfcomm0 藍牙設備地址通道# rfcomm bind /dev/rfcomm0 00:08:AC:03:58:A6 -1藍牙設備地址: 遠端藍牙設備地址(DTK001)如果需要解除綁定# rfcomm unbind /dev/rfcomm0 BD_ADDR channel 11 連接設備 # hcitool cc 00:08:AC:03:58:A6 |------------| | minicom -| |------------| | RFCOMM | <---> /dev/rfcomm0 |------------| | L2CAP | |------------| | --HCI_UART | |------------| | COM | <---> /dev/ttyS0 OR |------------| /dev/ttyUSB0 (USB to RS232) | BT Device –| |------------| # hcitool cmd 0x03 0x001A 0x00< HCI Command: ogf 0x03, ocf 0x001a, plen 1 00 > HCI Event: 0x0e plen 4 01 1A 0C 00 # hciconfig hci0hci0: Type: USB BD Address: 00:15:83:07:D5:78 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING RX bytes:1459 acl:0 sco:0 events:63 errors:0 TX bytes:764 acl:0 sco:0 commands:63 errors:0 # hcitool cmd 0x03 0x001A 0x01< HCI Command: ogf 0x03, ocf 0x001a, plen 1 01 > HCI Event: 0x0e plen 4 01 1A 0C 00 # hciconfig hci0hci0: Type: USB BD Address: 00:15:83:07:D5:78 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING ISCAN RX bytes:1472 acl:0 sco:0 events:65 errors:0 TX bytes:771 acl:0 sco:0 commands:65 errors:0 # hcitool cmd 0x03 0x001A 0x02< HCI Command: ogf 0x03, ocf 0x001a, plen 1 02 > HCI Event: 0x0e plen 4 01 1A 0C 00 # hciconfig hci0hci0: Type: USB BD Address: 00:15:83:07:D5:78 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING PSCAN RX bytes:1491 acl:0 sco:0 events:68 errors:0 TX bytes:785 acl:0 sco:0 commands:68 errors:0 # hcitool cmd 0x03 0x001A 0x03< HCI Command: ogf 0x03, ocf 0x001a, plen 1 03 > HCI Event: 0x0e plen 4 01 1A 0C 00 # hciconfig hci0hci0: Type: USB BD Address: 00:15:83:07:D5:78 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:1525 acl:0 sco:0 events:73 errors:0 TX bytes:811 acl:0 sco:0 commands:73 errors:0 # vi bluetooth.sh-----------------------------------#!/bin/bashinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/bluetooth.ko &>/dev/nullinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/l2cap.ko &>/dev/nullinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/sco.ko &>/dev/nullinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/bnep/bnep.ko &>/dev/nullinsmod /lib/modules/`uname -r`/kernel/net/bluetooth/rfcomm/rfcomm.ko &>/dev/nullinsmod /lib/modules/`uname -r`/kernel/drivers/bluetooth/hci_uart.ko &>/dev/nulllsmod | grep bluetooth/etc/init.d/bluetooth restartsleep 5hciconfig -ahciconfig hci0 uphcitool dev > locate_devhcitool scan > remote_devrm /dev/rfcomm0 &>/dev/nullmknod /dev/rfcomm0 c 216 1chmod 666 /dev/rfcomm0# configure rfcomm.conflocate_dev_mac=`awk "{ if(NR ==2) printf $2}" locate_dev`rm locate_devcat > /etc/bluetooth/rfcomm.conf << EOFrfcomm0 { bind no; device $locate_dev_mac; channel 1; comment "HCI003"}EOFsdptool add --channel=1 DID SP DUN LAN FAX OPUSH FTP HS HF SAP NAP GN PANU HID CIP CTP A2SRC A2SNK SYNCML NOKID PCSUITE SR1remote_dev_mac=`awk "/DTK/{printf $1}" remote_dev`rm remote_devrfcomm bind /dev/rfcomm0 $remote_dev_mac 1hcitool cc $remote_dev_machciattach - attach serial devices via UART HCI to BlueZ stack---------------------------------------------------hciattach [-n] [-p] [-t timeout] tty type|id speed flow bdaddr-n Don"t detach from controlling terminal.tty This specifies the serial device to attach. A leading /dev can be omitted. Examples: /dev/ttyS1 ttyS2type|id The type or id of the Bluetooth device that is to be attached,i.e. vendor or other device specific identifier. Currently supported types are type description any Unspecified HCI_UART interface, no vendor specific options ericsson Ericsson based modules digi Digianswer based cards xircom Xircom PCMCIA cards:Credit Card Adapter and Real Port Adapter csr CSR Casira serial adapter or BrainBoxes serial dongle(BL642) bboxes BrainBoxes PCMCIA card (BL620) swave Silicon Wave kits bcsp Serial adapters using CSR chips with BCSP serial protocol Supported IDs are (manufacturer id, product id) 0x0105, 0x080a Xircom PCMCIA cards:Credit Card Adapter and Real Port Adapter 0x0160, 0x0002 BrainBoxes PCMCIA card (BL620) |
推薦閱讀: