锁链战记动漫剧情:linux下使用蓝牙设备 bluetooth hciconfig hcitool

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 19:07:32
 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 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 –|
          |------------|

# hcitool cmd 0x03 0x001A 0x00
< HCI Command: ogf 0x03, ocf 0x001a, plen 1
  00
> HCI Event: 0x0e plen 4
  01 1A 0C 00
# hciconfig hci0
hci0:    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 hci0
hci0:    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 hci0
hci0:    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 hci0
hci0:    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/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)