Bluetooth: Linux and mobile phones

Source: Internet
Author: User
Bluetooth: Linux and mobile phones-general Linux technology-Linux technology and application information. For more information, see the following. Basic
Bluetooth was first proposed by Ericsson, Sweden. It is named as a king in ancient Sweden, because it always eats some strange fruit (it seems to be a variety of strawberries and does not know much about botany, if it is wrong, do not take a photo of me). It makes the teeth blue, called bluetooth ). As in ancient Sweden, Sweden ruled the whole northern Europe at that time, the ambitious Ericsson used the BT name to name the highly anticipated short-distance Interconnection Technology.

Bluetooth is used to connect peripheral devices, such as wireless headsets, printers, scanners, mobile phones, and computers. files and data are exchanged between these devices to replace low-speed serial lines, common uses include

File Sharing and transmission
Voice Transmission
Remote Printing
Replacement of RS-232 serial port Line
Because the Bluetooth function is very practical, once proposed, there is a sense of instant response, with the decline in costs, the use of Bluetooth is getting wider and wider (of course, this process is not smooth sailing). A large number of low-end mobile phones have begun to be equipped with this technology, with its richer and more complete protocol stack and a slightly higher transmission rate, as well as getting rid of the barrier bound with no obstacles relative to infrared, it almost completely replaced the infrared interface that was commonly equipped on the original business mobile phone.

When Bluetooth came to China, it replaced the "tooth" with the "Bud" with a more Eastern beauty.

Bluetooth is actually not a simple protocol. On the contrary, it is very complicated.

In terms of network structure and networking mode, Bluetooth forms a network in the 10-meter area, which can have one master device and seven slave devices, a total of eight active devices, of course, there can be many devices to sleep, just don't snoring. For more devices, you can use the ad hoc method for interconnection. Students of the network can see this spirit, but I don't want to talk about it anymore.
From the perspective of applications, BT supports voice communication and serial line simulation, and uses Profile to support applications of various peripheral smart devices, such as headphones, printers ...... It is interesting to define a service discovery and calling mechanism.
The most interesting model of Bluetooth is the layer-7 interconnection and the Profile of the application layer. As for the underlying frequency hopping, It is not new. In addition, although the usage frequency of Bluetooth buds and the frequency of microwave oven cooking are no different, the emission power is still very small, basically not enough to cook such a large piece of meat.

Bluetooth protocol stack in Linux
There are several Bluetooth protocol stacks in Linux. Currently, bluez and affix are healthy. The latter is probably supported by Nokia. The former is the official Linux version of Bluetooth, it is integrated into the Linux kernel. That is to say, if you have a newer 2.6 kernel, you will probably already support Bluetooth, and if you do not yet support it, just re-compile it.

There is no need to think that the Linux protocol stack is inferior to the one in Windows. The fact is that the scenery here is good, and the bluez protocol stack supports far more hardware devices than windows systems. If you only use Linux like me, after buying the Bluetooth adapter, you will probably be able to drop the bloated drive disc into the trash, and then use the Bluetooth in a pleasant mood. Come with me,

The kernel protocol stack supports the following parts:

HCI. This is the bottom layer, known as Host Control Interface. HCI is derived from the application model of Bluetooth. Bluetooth is a wireless interface connecting smart peripherals. One side of the interface is a device, and the other side is a Host. USB, IEEE1394, are also used in similar recording methods, from the perspective of the original design intention, these things are aimed at almost the same market. Of course, they have their own strengths. Whether a Bluetooth adapter can be driven depends on HCI support. The most common Bluetooth adapter is the USB interface that I hold. For most standard Bluetooth devices, its driver module is hci-usb. For our 2.6 kernel, insert this adapter and the module will be automatically loaded.

The functions of L2CAP, Logical Link Control, and Adaptation Protocal are enabled on top of HCI. The upstream layer provides the Link Control function for asynchronous data transmission, it means that there is no primary clock to synchronize all transmission modes, which is usually used for data transmission. In contrast, synchronous transmission is completely synchronized with the clock, generally used in voice or multimedia calls. In Bluetooth, synchronous voice transmission is also supported, and SCO in the bluez protocol stack supports synchronous mode.

Two Protocols Above L2CAP are widely used: RFCOMM and BNEP. The former is used to replace the traditional serial port, including various applications on the serial port, for example, data transmission, such as fax and dial-up Internet access, printers, and file images. The latter provides an Ethernet interface, which is more suitable for computer networking. Naturally, RFCOMM is often used between mobile phones and computers.

As of now, almost all of the Bluetooth protocol stacks provided by the kernel are available. However, the only difference is that Bluetooth is not enough for my use. Only user-mode protocol stacks and tools work together, to have a happy Bluetooth life :)

Bluetooth protocol stack and related tools in user space
Next, the protocol stack just now is implemented in the user space:

At the top layer, the blue bud defines many profiles. Each Profile corresponds to an application, such as printing, Headset, file transmission, and Fax/Modem dialing. Among them, file exchange corresponds to the Obex protocol, which is a file (object) Exchange Protocol Based on Bluetooth, infrared (IrDA), serial port and other media, it is not surprising that these types of media are listed together. After all, the first two are used to replace the serial port in a sense. Of course, some profiles, such as those of some human-computer interaction devices (such as the keyboard and mouse), are implemented in the kernel.

In practice, we have another important protocol that we have not introduced. This is the SDP-Service Discovery Protocol. This protocol can be considered to be at a unified level with RFCOMM because it does not carry RFCOMM, however, this protocol is very special and important. Through it, we can identify which services (profiles) a bluetooth device provides for me.

Well, the protocol stack already exists. How can we use Bluetooth? Recall how to put the elephant in the refrigerator:

Open the refrigerator door
Put the elephant in the refrigerator
Close the refrigerator door
It is so simple that a Bluetooth service is so easy:

Find the bluetooth device, which is the responsibility of the HCI Layer. Use the hcitool provided by the bluez-utils package to find the bluetooth device;

Find the service. RFCOMM provides different profiles through different channels. Therefore, we need to find the channel on which the service we want to use is on the device, this is done through sdptool in the same software package, that is, SDP, the Service Discovery Protocol.

To connect to an appropriate service, the use of Bluetooth is characterized by the above, and the task to be completed by a user-mode tool is:

Discovery Service
Use Services
Start the HCI Device
First, we need to start the hcid so that the communication at the HCI Layer can be performed. for Debian users, you need to install the bluez-utils package and start the hcid. If you have run the bluetooth service and set the HCID to be enabled in/etc/default/bluetooth

HIDD_ENABLED = 1

After the USB adapter is inserted, your hcid is already running. Check the relevant information:

Inspiration:/etc/bluetooth # hciconfig-
Hci0: Type: USB
BD Address: 11: 11: 11: 11: 11: 11 acl mtu: 678: 8 sco mtu: 48: 10
UP RUNNING PSCAN ISCAN
RX bytes: 413 acl: 0 sco: 0 events: 19 errors: 0
TX bytes: 323 acl: 0 sco: 0 commands: 19 errors: 0
Features: 0xbf 0xfe 0x8d 0x78 0x08 0x18 0x00 0x00
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF PARK
Link mode: MASTER
Name: 'espiration-0'
Class: 0x3e0100
Service Classes: Networking, Rendering, Capturing, Object Transfer, Audio
Device Class: Computer, Uncategorized
HCI Ver: 1.2 (0x2) HCI Rev: 0x1fe LMP Ver: 1.2 (0x2) LMP Subver: 0x1fe
Manufacturer: Integrated System Solution Corp. (57)
This process is automatic. Of course, you can also use hciconfig (8) for manual control.

Search for Bluetooth devices
HCI has been started, and now you can use it to find the bluetooth device. Of course, you must first enable the Bluetooth function of the bluetooth device. This is not nonsense. The Bluetooth function of the mobile phone is disabled by default, signals are sent only under manual control and can be scanned. However, the individual differences between devices are too large to be described here, I suppose you have opened the Bluetooth power of the device by yourself or by referring to the manual. Now, find it.

Inspiration:/home/gnawux # hcitool scan
Scanning...
00: 17: 00: 7B: 18: B8 Motorola SLVR

After the command is run, the bluetooth device will be found smoothly.

View the services provided by the device
With the SDP protocol, we can also view what services each device has and what services it provides. Each RFCOMM-based service uses a protocol that occupies a channel )", this is an important parameter when we use the service.

The following is an example:

Gnawux @ inspiration :~ $ Sdptool browse localBrowsing FF: 00: 00: 00... service Name: OBEX Object PushService RecHandle: 0x0000service Class ID List: "OBEX Object Push" (0x1105) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 9 "OBEX" (0x0008) Profile Descriptor List: "OBEX Object Push" (0x1105) Version: 0x0100
Look at other people (mobile phones)

Gnawux @ inspiration :~ $ Sdptool browse 00: 17: 00: 7B: 18: B8
Browsing 00: 17: 00: 7B: 18: B8...
Service RecHandle: 0x0
Service Class ID List:
"SDP Server" (0x1000)
Protocol Descriptor List:
"L2CAP" (0x0100)
"SDP" (0x0001)
Profile Descriptor List:
"SDP Server" (0x1000)
Version: 0x0100
Service Name: Dialup Networking Gateway
Service Description: Dialup Networking Gateway
Service Provider: Motorola
Service RecHandle: 0x10001
Service Class ID List:
"Dialup Networking" (0x1103)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 1
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"Dialup Networking" (0x1103)
Version: 0x0100
Service Name: Voice Gateway
Service Description: Headset Audio Gateway
Service Provider: Motorola
Service RecHandle: 0x10003
Service Class ID List:
"Headset Audio Gateway" (0x1112)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 3
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"Headset" (0x1108)
Version: 0x0100
Service Name: Handsfree Voice Gateway
Service Description: Handsfree Voice Gateway
Service Provider: Motorola
Service RecHandle: 0x10007
Service Class ID List:
"Handfree Audio Gateway" (0x111f)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 7
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"Handsfree" (0x111e)
Version: 0x0101
Service Name: OBEX Object Push
Service Description: OBEX Object Push
Service Provider: Motorola
Service RecHandle: 0x10008
Service Class ID List:
"OBEX Object Push" (0x1105)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 8
"OBEX" (0x0008)
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"OBEX Object Push" (0x1105)
Version: 0x0100
Service Name: OBEX File Transfer
Service Description: OBEX File Transfer
Service Provider: Motorola
Service RecHandle: 0x10009
Service Class ID List:
"OBEX File Transfer" (0x1106)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 9
"OBEX" (0x0008)
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"OBEX File Transfer" (0x1106)
Version: 0x0100
Service Name: Image Push
Service Description: Image Push
Service Provider: Motorola
Service RecHandle: 0x1000a
Service Class ID List:
"Imaging Responder" (0x111b)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 10
"OBEX" (0x0008)
Language Base Attr List:
Code_ISO639: 0x656e
Encoding: 0x6a
Base_offset: 0x100
Code_ISO639: 0x7a68
Encoding: 0x6a
Base_offset: 0xc800
Profile Descriptor List:
"Imaging" (0x111a)
Version: 0x0100
There are many mobile phones, and I have not truncated them. It is convenient for you to see the rich Bluetooth functions provided by mobile phones. In the next section, we will introduce several things that can be done using Bluetooth:

Bluetooth Application
In the previous section, through sdptool, we can see that my mobile phone provides dialing, headphones, file transmission, and other functions. Now let's try it out.

Transfer files using Bluetooth

Files transmitted using Bluetooth are transmitted using the previously mentioned Obex protocol. Here we will introduce several usage instructions.

Push files with mobile phones
This is the "OBEX Object Push" (0x1105) service provided by the mobile phone or computer. The other party pushes files to it. The tool used is the obex_test tool in the openobex-apps package.

First, push to the mobile phone. For my mobile phone, the push service is on Channel 8:

Gnawux @ inspiration :~ $ Obex_test-B 00: 17: 00: 7B: 18: B8 8
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> C
Connect OK!
Version: 0x10. Flags: 0x00
> P wangxu.jpg me.jpg
PUT file (local, remote)> name‑wangxu.jpg, size = 34177
Going to send 34177 bytes
Made some progress...
Made some progress...
Made some progress...
Made some progress...
Made some progress...
PUT successful!
> Q
As you can see, after executing obex_test, you can enter an interactive state, first establish a connection, and then transfer the file (the local file name is wangxu.jpg, which is called me.jpg on the mobile phone, ). Finally, exit. In this process, you need to watch the mobile phone screen. You may need to check whether the connection is established and where the file is stored. Of course, this is related to the mobile phone.

Now you can also check whether the mobile phone is pushed to the computer. First, let the mobile phone enter the receiving status. If the Object PUSH Service is not started locally, add the following to it:

Gnawux @ inspiration :~ $ Sdptool add OPUSH
OBEX Object Push service registered
Now you are in the receiving status. Note that you must use the address and channel number of your computer (local:

Gnawux @ inspiration :~ $ Obex_test-B FF: 00: 00: 00 9
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> S
Now, the computer enters the receiving mode, and you can send a file to the computer on the mobile phone side.

Upload and download using obexftp
Using obexftp of the "OBEX File Transfer" (0x1106) service is a convenient tool. We can view the content on the mobile phone and upload and download it. For my mobile phone, the corresponding channel is 9 (refer to the above sdptool browse results ).


Check which directories are available:

Gnawux @ inspiration :~ $ Obexftp-B 00: 17: 00: 7B: 18: B8-B 9-l
Browsing 00: 17: 00: 7B: 18: B8...
Channel: 9
Connecting... done
Grouping ing "(null)"... |








Done
Disconnecting... done
We can see that there are four directories with read/write permissions. Now, let's look at what's in audio.

Gnawux @ inspiration :~ $ Obexftp-B 00: 17: 00: 7B: 18: B8-B 9-l audio
Browsing 00: 17: 00: 7B: 18: B8...
Channel: 9
Connecting... done
Indexing ing "audio"... |





Done
Disconnecting... done
We can see that there is an mp3 file. We can take it down.

Gnawux @ inspiration :~ $ Obexftp-B 00: 17: 00: 7B: 18: B8-B 9-c audio-g a.mp3
Browsing 00: 17: 00: 7B: 18: B8...
Channel: 9
Connecting... done
Sending "audio"... done
Indexing ing "a.mp3"...-done
Disconnecting... done


After a long wait, the transfer was finally completed, and a bit regretted choosing such a large file for transmission. Now, put wangxu.jpg in the picture directory.


Gnawux @ inspiration :~ $ Obexftp-B 00: 17: 00: 7B: 18: B8-B 9-c picture-p wangxu.jpg
Browsing 00: 17: 00: 7B: 18: B8...
Channel: 9
Connecting... done
Sending "picture"... done
Sending "wangxu.jpg"... \ done
Disconnecting... done
For other usage of obexftp (1), you can learn the document by yourself :)

Other obex tools
There are also obexserver, qobex (qt based) and other tools, which are simpler to use and will not be introduced.



Dial with Bluetooth phone

We have said that one of the functions of Bluetooth is to imitate the serial port. One of the important functions of serial ports (probably the most important) is dial-up, and the traditional DTE is Modem. In fact, through rfcomm, the Bluetooth connection can be reflected on character devices such as/dev/rfcomm0 and operated like a serial port. Of course, we 'd better first define/etc/bluetooth/rfcomm. conf, which is written according to the device number and channel number of the mobile phone.

Rfcomm0 {
# Automatically bind the device at startup
Bind yes;

# Bluetooth address of the device
Device 00: 17: 00: 7B: 18: B8;

# RFCOMM channel for the connection
Channel 1;

# Description of the connection
Comment "Motorola SLVR L7 ";
}
In this way, the bluetooth service is automatically connected when it is started, and you do not need to use the rfcomm (1) command. Now, you can use any of your favorite serial programs (such as minicom and gtkterm) to dial/dev/rfcomm0.

Enter the First Command of the AT command:

AT

OK

Try to play the number

ATD139012345;

This is a random number. You can find the number you know to play it on (deliberately write less than two, saving you the trouble of trying to get to someone else's head). Of course, you can also connect to GPRS. Here, we use the basic dialup program pppd to implement it, but we also use AT Command to implement it.

Inspiration:/home/gnawux # cat/etc/ppp/peers/gprs
Rfcomm1
115200
Debug
Noauth
Defaultroute
Usepeerdns
Connect-delay 10000
Crtscts
Lock
Lcp-echo-failure 4
Lcp-echo-interval 65535
Connect '/usr/sbin/chat-v-t3-f/etc/ppp/peers/upls_chat'


Based on the content of this file, pppd will use the content of the file "maid" to interact on the serial port rfcomm1 (that is, the Bluetooth connection to my L7 mobile phone) and establish a ppp connection, the chat file contains some AT Command interactions:

Inspiration:/home/gnawux # cat/etc/ppp/peers/upls_chat
ABORT 'no carrier'
ABORT 'error'
ABORT 'no dialtone'
ABORT 'busy'
ABORT 'no ancer'
'''Atz'
'OK' 'ate0v1'
'OK' AT + CGDCONT = 1, "IP", "cmwap.cmcc.com", "0.0.0.0'
'OK' 'atd * 99 *** 1 #'

The above is a number of AT commands required for GPRS establishment. After the file is created, the operation is very simple, as long as you run

Pppd call gprs

The connection can be established. This is the information obtained by ifconfig.

Ppp0 Link encap: Point-to-Point Protocol
Inet addr: 10.14.7.136 P-t-P: 192.168.100.101 Mask: 255.255.255.255
Up pointopoint running noarp multicast mtu: 1500 Metric: 1
RX packets: 4 errors: 1 dropped: 0 overruns: 0 frame: 0
TX packets: 13 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 3
RX bytes: 64 (64.0 B) TX bytes: 543 (543.0 B)
Here are the route table and DNS settings:

Inspiration:/etc/ppp/peers # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.101*255.255.255.255 UH 0 0 0 ppp0
Default * 0.0.0.0 U 0 0 ppp0

Inspiration:/etc/ppp/peers # cat/etc/resolv. conf
Nameserver 221.130.33.52
Nameserver 221.130.33.60

It's very easy.

Summary
So far, all the content of Linux and Bluetooth mobile phones has been completed theoretically. The operating system is based on Debian GNU Linux with the 2.6 kernel, the tested mobile phones include Motorola SLVR L7 and Nokia N-Gage QD. The Bluetooth adapter chip is Integrated System Solution Corp. KY-BT100 Bluetooth Adapter, all tests are very smooth, but also wish readers have a good time.
Related Article

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.