linux下使用藍牙設備

UBUNTU下面有個gnome藍牙桌面管理器,使用起來很簡單,但是搞不清除這個工具的工作細節,看看下面的方法,用的是bluez提供的工具,寫成的腳本,我好好研究研究,以備做嵌入式之用。

 

 

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 modules
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/bluetooth.ko
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/l2cap.ko
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/sco.ko
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/bnep/bnep.ko
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/rfcomm/rfcomm.ko
insmod /lib/modules/`uname -r`/kernel/drivers/bluetooth/hci_uart.ko
lsmod | grep bluetooth


#!/bin/bash
#name: bt_rm.sh
#author:young
#date: 2007-01-16
#description: rmmod bt modules from kernel
rmmod hci_uart
rmmod bnep
rmmod sco
rmmod rfcomm
rmmod l2cap
rmmod bluetooth
lsmod


2 重啓Bluetooth
系統 服務
# /etc/init.d/bluetooth restart
Restarting 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 –a
hci0: 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 dev
Devices:
        hci0    00:1B:35:00:12:34


6 查詢周圍藍牙設備
# hcitool scan
Scanning ...
        00:E0:91:74:2E:64       KE850
        00: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
需要設置minicom
Serial 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
- |
          |------------|



# vi bluetooth.sh
-----------------------------------
#!/bin/bash
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/bluetooth.ko     &>/dev/null
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/l2cap.ko         &>/dev/null
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/sco.ko           &>/dev/null
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/bnep/bnep.ko     &>/dev/null
insmod /lib/modules/`uname -r`/kernel/net/bluetooth/rfcomm/rfcomm.ko &>/dev/null
insmod /lib/modules/`uname -r`/kernel/drivers/bluetooth/hci_uart.ko &>/dev/null
lsmod | grep bluetooth

/etc/init.d/bluetooth restart
sleep 5
hciconfig -a
hciconfig hci0 up

hcitool dev > locate_dev
hcitool scan      > remote_dev

rm /dev/rfcomm0   &>/dev/null
mknod /dev/rfcomm0 c 216 1
chmod 666 /dev/rfcomm0

# configure rfcomm.conf
locate_dev_mac=`awk ''{ if(NR ==2) printf $2}'' locate_dev`
rm locate_dev
cat > /etc/bluetooth/rfcomm.conf << EOF
rfcomm0 {
    bind   no;
    device     $locate_dev_mac;
    channel    1;
    comment    "HCI003"
}
EOF

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

remote_dev_mac=`awk ''/DTK/{printf $1}'' remote_dev`
rm remote_dev
rfcomm bind /dev/rfcomm0 $remote_dev_mac 1
hcitool cc $remote_dev_mac








hciattach - 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 ttyS2

type|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)

speed
     The speed specifies the UART speed to use. Baudrates higher than 115.200bps require vendor specific initializations that are not implemented for all types of devices. In general the following speeds are supported:
     9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600

     Supported vendor devices are automatically initialised to their respective best settings.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章