linux下使用藍牙裝置

來源:互聯網
上載者:User

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.