Use a bluetooth device in Linux

Source: Internet
Author: User

1. Load the bluetooth module


The modules to be loaded include:Bluetooth,Hci_uart,L2CAP,RFCOMM,SCO,Bnep, In/lib/modules/'uname-R'/kernel/NET/Bluetooth and/lib/modules/'uname-R'/kernel/driver/Bluetooth
Note: You can use bt_ins.sh and bt_rm.sh scripts to add and delete Bluetooth modules.
#! /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. 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. Ko
Insmod/lib/modules/'uname-R'/kernel/NET/Bluetooth/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. Restart BluetoothSystemService


#/Etc/init. d/Bluetooth restart
Restarting Bluetooth services: [OK]

3 state tty device


# Hciattach-N/dev/ttyusb0 Ericsson 57600 &
Note: Do not attach the tty device again after the device succeeds.
The hciconfig command provides detailed information about the local device. If hciconfig is executed without any parameters, the output displays device information such as the device name (hcix) and physical device address (12-digit, in the format of 00: 12: 34: 56: 78) and information about the transferred data volume. Hciconfig hci0 name indicates the name that is returned when your computer receives a request from a remote device. In addition to querying local device settings, hciconfig can also be used to modify these settings. For example, hciconfig hci0 name Test Sets the name to test.
# Hciconfig-
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)
Note: if this information is not displayed, check whether the hcid is running and whether there is an error message in the file/var/log/messages. The BD address of the adapter is the unique identifier of the Bluetooth identifier, which is similar to the MAC address of the Ethernet.

4. Activate the bluetooth module


# Hciconfig hci0 up

5. View bluetooth module information(Whether it is correctly identified and normal)

# Hcitool Dev
Devices:
Hci000: 1b: 35: 00: 12: 34

6. query the peripheral Bluetooth devices.


# 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
Hardware address and device name of the bluetooth device

7. Create a virtual serial port device


# Mknod/dev/rfcomm0 C 216 1
# Chmod 666/dev/rfcomm0
SetMinicom
Serial device:/dev/rfcomm0

8. Modify RFCOMM. conf and add the following content:


# Vi/etc/Bluetooth/RFCOMM. conf
Rfcomm0 {
Bind no;
Device-00: 1b: 35: 00: 12: 34;
Channel 1;
Comment "hci003"
}
Note:
Bind no is to set whether to automatically bind the device;
Device-sets the address of the bound device;
A channel is the channel used to set the device;
Comment is the description of the device.
Note: The device address is the address of the local Bluetooth device.

9 Add an SDP protocol channel


# 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
Note: not all subsequent parameters are supported. to be simple, all services are enabled by default.

10 bind a virtual serial port to an external bluetooth device


Format:
# RFCOMM bind/dev/rfcomm0 bluetooth device address Channel
# RFCOMM bind/dev/rfcomm0 00: 08: AC: 03: 58: A6-1
Bluetooth device address: remote Bluetooth device address (dtk001)
Unbind
# RFCOMM unbind/dev/rfcomm0 bd_addr Channel

11 connecting devices

# 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. 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. Ko &>/dev/null
Insmod/lib/modules/'uname-R'/kernel/NET/Bluetooth/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-
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 Based Modules
DiGi digianswer based cards
Xircom PCMCIA cards: Credit card adapter and real Port Adapter
CSR casira serial adapter or brainboxes serial Dongle (blgle)
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,192 00, 38400,576 00, 115200,230 400, 460800,921 600
Supported vendor devices are automatically initialised to their respective best settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.