A small Bluetooth Application

Source: Internet
Author: User
Tags first string

USB bluetooth device and bluez have been transplanted to the HHARM2410-R3 in the last two days
The Bluetooth protocol stack, huh, is really a good thing, the protocol stack is rich in content, after hanging up can use a lot of functions. Now I can use it to receive GPS
Data and accessing the Internet through mobile phones. I am working on a car, so it is particularly useful. I have accumulated some knowledge about Bluetooth, but it is preliminary. I have sent my experience to anyone who has worked on Bluetooth. Please give me more instructions.

All rights reserved by bluez --- Dennis, High Performance Computing Center, Tongji University

Thank you for sending me an email to discuss dennis.he.2005@gmail.com with me.
First, I will introduce the hardware. I used a USB Bluetooth adapter that I bought at random in a computer store. The price is about 80 yuan. It should be made in Taiwan and there is no brand, like a smaller USB flash drive. Before buying the phone, ask the seller to test it in windows. Bring a mobile phone with Bluetooth and try again.
There are several Bluetooth protocol stacks on Linux, and the Official Bluetooth protocol stack is bluez. All the instructions and source code can be found at www.bluez.org. I first installed bluez's protocol stack on my PC, and then transplanted it to huaheng HHARM2410-R3.

(1) install bluez on Red Hat 9
 
I have referred to o'reilly's Linux unwired book, chapter for installation on the PC.
7 describes in detail the basic knowledge, installation, configuration, and usage of Bluetooth in Linux. Before installation, you must understand the basic structure of bluez.
The Protocol is HCI (Host Control
Interface), the high-level protocol used by my mobile phone and Bluetooth GPS is the Service Discovery Protocol SDP and the Protocol RFCOMM for simulating serial ports. The middle layer of the Protocol is L2CAP, which should be processing
For details about the Logical Link, see the official bluez documentation.
The kernel version used on the HHARM2410-R3 is 2.4.18, So I installed it on the PC
Bluez also uses the 2.4.18 kernel. To use bluez, you must type the kernel
In patch, after linux-2.4.18.tar.bz2 is downloaded to www.kernel.org, http:
// Download patch-2.4.18-mh15.gzfrom www.bluez.org/patches.html. Therefore, Solution
Pressure, patch:
# Tar xjvf linux-2.4.18.tar.bz2
# Cd Linux/
# Gzip-DC ../patch-2.4.18-mh15.gz | patch-p1
Note the patch output at this time. In terms of source code, the patch modifies the code in the driver/, include/, and net/directories and does not modify the content in the kernel, there should be no changes to the core of Linux, and there should be no major problems when porting to S3C2410.
The following describes how to compile the kernel. During the config process, my configuration for bluez is as follows:
#
# Bluetooth support
#

Config_bluez = m
Config_bluez_l2cap = m
Config_bluez_sco = m
Config_bluez_rfcomm = m
Config_bluez_rfcomm_tty = y
Config_bluez_bnep = m
Config_bluez_bnep_mc_filter = y
Config_bluez_bnep_proto_filter = y

#
# Bluetooth device drivers
#
Config_bluez_hciusb = m
# Config_bluez_hciusb_sco is not set
Config_bluez_hciuart = m
Config_bluez_hciuart_h4 = m
Config_bluez_hciuart_bcsp = m
# Config_bluez_hciuart_bcsp_txcrc is not set
# Config_bluez_hcibfusb is not set
Config_bluez_hcidtl1 = m
Config_bluez_hcibt3c = m
Config_bluez_hcibluecard = m
Config_bluez_hcibtuart = m
Config_bluez_hcivhci = m
 
For your reference, the Linux unwired book provides a detailed description. Note that if you use make
Menuconfig. Open the. config file and check whether config_bluez_rfcomm_tty = Y. It cannot be n or M. Also note
Config_usb_bluetooth cannot be selected (under USB support), otherwise it will affect the operation of bluez.
After the kernel is compiled, it starts.
Prepare the libraries and tools for bluez. Download the latest bluezprocess from http://www.bluez.org/download.html.
I only downloaded bluez-libs-2.20.tar.gz, bluez-utils-2.20.tar.gz, bluez-pin-
The three 0.25.tar.gz packages are libraries, toolsets, and one provided by bluez.
Pin_helper. In PC, there is nothing to say about the installation process. Unzip,./configure, make, and make install.
Then
Note that the hcid. conf configuration file under the default configuration directory/etc/Bluetooth/is used to configure the background daemon process hcid. If
If this PC machine + Bluetooth adapter is regarded as a bluetooth device, the hcid of the Process manages the basic configuration information of the bluetooth device, such as name, class, address, and feature. Also
When a device is paired with another bluetooth device Bonding/pairing
The Code Program is also hcid, and one of the most important items in the hcid. conf script is pin_helper. The default configuration is pin_helper =
/Usr/bin/bluepin, which is written in Python to read the PIN entered by the user.
Code Program, installed in/usr/bin when installing the bluez-pin package.
It is necessary to know the pairing process here. I feel that the two devices connect to each other through the password.
Establish a connection with each other. After reading the hcid code, I understand the following: for example, two bluetooth settings
Slave A and B, a first sends a request requiring pairing to B, B is set by the user to pair the password is Pin
Code, and then return a message to a, saying you want to pair. Do you know my password? Then a sends the pin
Code is used in the past. B sends a replyok message to A, which establishes a trust relationship between the two.
In PC + Bluetooth adapter this Bluetooth settings
On slave a, if this device sends a pairing request to another remote device B, then when B sends a password inquiry request
The program reads the PIN code through a pin_helper program and sends it back to B. If the remote device B sends a pairing request to A, the pin on the hcid is used for verification
The Code is the content of/etc/Bluetooth/pin. If the read failure program is started, a password is set by default-
In the init_security_data function of utils/hcid/security. C, the default value is bluez. To match my cell phone, I changed it
123. Only numbers can be entered on the mobile phone.
For other hcid. conf settings, see man hcid. conf.
After setting the kernel, replace it with the new kernel and start
Linux. Insert a USB Bluetooth adapter, # dmesg | tail will find USB. C: USB device 2 (vend/Prod
0x1131/0x1001) is not claimed by any active driver. This problem, no matter it, as long as there is USB. C:
Registered new driver hci_usb message, then # lsmod will find that the hci_usb module is installed, and #
Modprobe RFCOMM, so that basically all modules are installed.
# Hciconfig hci0 up
# Hcid-F/etc/Bluetooth/hcid. conf
Start the HCI device and hcid genie Process
# Hciconfig-
Hci0: Type: USB
BD address: 00: 11: 67: 0f: BB: A1 acl mtu: 678: 8 sco mtu: 48: 10
Up running pscan iscan
RX Bytes: 115 ACL: 0 SCO: 0 events: 15 errors: 0
TX Bytes: 313 ACL: 0 SCO: 0 commands: 15 errors: 0
Features: 0xff 0xff 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: slave accept
Name: 'bluez (0 )'
Class: 0x000100
Service Classes: Unspecified
Device class: Computer, uncategorized
HCI Ver: 1.2 (0x2) HCI Rev: 0x1ae LMP Ver: 1.2 (0x2) LMP subver: 0x1ae
Manufacturer: integrated system solution Corp. (57)
You can see the current configuration of the device. Some of them are read from hcid. conf, and some are read from the device.
Next, turn on the Bluetooth on your phone and start the Bluetooth GPS
# Hcitool Scan
Scanning...
00: 0e: 07: 58: 1A: 33 t628
00: 08: 1b: C0: D1: 3C holux GR-231
Now we can see two devices. I use Sony Ericsson's t628 and holux's bluetooth GPS. The first string should be the unique address of the bluetooth device.
# Sdptool browse can view the services provided by each device
Inquiring...
Browsing 00: 0e: 07: 58: 1A: 33...
Service name: Dial-Up Networking
Service rechandle: 0x10000
Service class ID list:
"Dialup networking" (0x1103)
"Generic networking" (0x1201)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 1
Profile descriptor list:
"Dialup networking" (0x1103)
Version: 0x0100

.............

Service name: Serial Port 1
Service rechandle: 0x10003
Service class ID list:
"Serial Port" (0x1101)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 4

............

Service name: obex object push
Service rechandle: 0x10005
Service class ID list:
"Obex object push" (0x1105)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 10
"Obex" (0x0008)
Profile descriptor list:
"Obex object push" (0x1105)
Version: 0x0100

Browsing 00: 08: 1b: C0: D1: 3C...

Mobile phones are bound to many services, including dial-up services, audio services, serial port services, and file exchange services (obex)... under the SDP protocol, each service is bound to a channel. No service is bound to GPS, which is used only as a virtual serial port device.
The RFCOMM utility can be used to bind RFCOMM-compliant services to a similar serial device. Before binding, you must ensure that the/dev device has RFCOMM *. If not, create the device:
# Mknod-M 666/dev/rfcommx C 216 x (x is a number)
In this way, you can bind it. For example, if you need to use the dial-up function of the mobile phone to access the Internet, you can see that channel 1 is the dialup networking function of RFCOMM. Then:
# RFCOMM bind 0 00: 0e: 07: 58: 1A: 33 1
 
About 00: 0e: 07: 58: 1A: 33 Channel
1. The service is bound to/dev/rfcomm0. In this way, a virtual serial connection is established to connect to a dial-up wireless modem. Enable minicom and set the serial port
/Dev/rfcomm0, baud rate 19200, 8n1, type atz, and Return OK. You can use PPP to dial up the Internet. When you connect to the internet for the first time, an input box will pop up asking the pin
Code.
The GPS device is only used as a serial port. It is also very simple:
# RFCOMM bind 1 00: 08: 1b: C0: D1: 3C
Enable minicom and set the baud rate to 4800 to read GPS data!
In addition, the sdpd program is a service provided by the device to external users. You can use sdptool add -- channel = x SP to add external services. I have not studied it. In addition, you still need to learn more about file transfer obex and personal LAN bneq.

2. Transplantation on HHARM2410-R3
After completing the installation on Red Hat, I am familiar with some basic things of the Bluetooth protocol stack. The following task is to move the protocol stack to the Development Board.
The first step is of course the kernel. Since hua Heng's kernel code has been changed, I don't know whether the patch downloaded from bluez can be used. I just tried it.
# Cd/hharm2410/kernel
# Gzip-DC ~ /Patch-2.4.18-mh15.gz | patch-p1
# Find./-name '* rej'
./Fs/cramfs/inode. C. rej
 
No, there are files that cannot be patched. Check whether the patch is for the cramfs file system. I don't know why the bluez patch will be changed to cramfs, but I want
The problem should not be big. The kernel source code of the Bluetooth part of Hua Heng should have not been changed, so the main part of the patch should be no problem. If a problem occurs after the kernel is compiled
We can only look at the kernel code.
First, try it. It is similar to that on a common PC, except that some of m are all y now. Part of my configuration about bluez is set:
#
# Bluetooth support
#
Config_bluez = y
Config_bluez_l2cap = y
# Config_bluez_sco is not set
Config_bluez_rfcomm = y
Config_bluez_rfcomm_tty = y
Config_bluez_bnep = y
Config_bluez_bnep_mc_filter = y
Config_bluez_bnep_proto_filter = y
# Config_bluez_hidp is not set
#
# Bluetooth device drivers
#
Config_bluez_hciusb = y
# Config_bluez_hciusb_sco is not set
# Config_bluez_hciuart is not set
# Config_bluez_hcibfusb is not set
# Config_bluez_hcidtl1 is not set
# Config_bluez_hcibt3c is not set
# Config_bluez_hcibluecard is not set
# Config_bluez_hcibtuart is not set
# Config_bluez_hcivhci is not set

The device part is configured with a USB, And the other parts are not configured.
Next we will continue the general process of make Dep; Make zimage. OK, there is no compilation problem, so we can be a little more comfortable.

The following is the basic library of bluez to be transplanted and executed under RedHat 9.
# LDD/usr/lib/libbluetooth. so.1.0.17
Libc. so.6 =>/lib/libc. so.6 (0x4002c000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x80000000)
If you find that there is no dependency on any inexplicable library, you should change the compiler and configure it. My configuration command is simple:
#
./Configure -- Host = armv4l
-- Prefix =/hharm2410/applications/bluezport/libs
Cc =/opt/host/armv4l/bin/armv4l-unknown-linux-gcc
CPP =/opt/host/armv4l/bin/armv4l-unknown-linux-cpp
AR =/opt/host/armv4l/bin/armv4l-unknown-linux-ar
Strip =/opt/host/armv4l/bin/armv4l-unknown-linux-strip
Ranlib =/opt/host/armv4l/bin/armv4l-unknown-linux-ranlib
LD =/opt/host/armv4l/bin/armv4l-unknown-linux-ld
# Make
# Make install
Okay. Now let's take a look at/hharm2410/applications/bluezport/libs. I don't know why. A and. La static libraries exist, and there is no dynamic library. Had to do it yourself, to the bluz-libs-2.17/src/, their own dynamic library Compilation:
#/Opt/host/armv4l/bin/armv4l-unknown-linux-gcc-shared-O libblustmth. so.1.0.17 Bluetooth. o hci. o sdp. o
Connect two symbols
# Ln-s libbluetooth. so.1.0.17 libbluetooth. So
# Ln-s libbluetooth. so.1.0.17 libbluetooth. so.1
All right, after the library is cross-compiled, the MV can be saved to the/hharm2410/application/bluezport/libs/lib folder.

Then transplant the bluez toolset. Similar to the library, let's take a look at the libraries required by each program, for example:
# LDD/usr/sbin/hcid
Libbluetooth. so.1 =>/usr/lib/libbluetooth. so.1 (0x40035000)
Libc. so.6 =>/lib/libc. so.6 (0x40042000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
 
Try the required tools one by one and find that the required libraries Hua Heng and bluez are provided. You should change the compiler to compile the tool. Before compilation, I made it easier to connect to my mobile phone on the board.
The bluez-utils-2.17/hcid/security. c file, in the init_security_data function to set the default password, very simple
Changed the following two rows:
Strcpy (hcid. pin_code, "123 ");
Hcid. pin_len = 3;
Then I want to configure Configure. My configure is set:
#
./Configure -- prefix =/HHARM2410-R3/applications/bluezport/utils
-- Export dedir =/HHARM2410-R3/applications/bluezport/libs/include
-- Libdir =/HHARM2410-R3/applications/bluezport/libs/lib
-- With-bluez =/HHARM2410-R3/applications/bluezport/Libs -- disable-test
-- Disable-cups -- disable-pcmia -- Host = armv4l
Cc =/opt/host/armv4l/bin/armv4l-unknown-linux-gcc
CPP =/opt/host/armv4l/bin/armv4l-unknown-linux-cpp
AR =/opt/host/armv4l/bin/armv4l-unknown-linux-ar
Strip =/opt/host/armv4l/bin/armv4l-unknown-linux-strip
Ranlib =/opt/host/armv4l/bin/armv4l-unknown-linux-ranlib
LD =/opt/host/armv4l/bin/armv4l-unknown-linux-ld
# Make
In this step of make, I encountered a small error. It seems that path_max is not defined. This is the maximum length of the buffer for storing path names set in Linux. It should be caused by a small header file. I did not elaborate on it, so I added three lines to config. h In the current file:
# Ifndef path_max
# Define path_max 4095/* page_size-1 */
# Endif
Use a simple and rude way to add a definition. Make again, pass.
# Make install
All right, the toolkit is under/HHARM2410-R3/applications/bluezport/util
Most
In Linux, the default pin_helper
/Usr/bin/bluepin is the code written in Python. It cannot be used in MySQL 2410. Therefore, you must write a pin_helper program by yourself. Flip
/Usr/bin/bluepin code and hcid/security. C code. In fact, the hcid process verifies
In code, a pipe pointing to the standard output of the pin_helper process is enabled. The pin_helper process asks the user for the pin
Code. If the user enters the password XXXX, It will be written to the standard output in the form of "pin: XXXX". If the user reject, it will write "Err" back.
I implement the pin_helper program in the simplest way, that is, write a code, such as 123, at the cost of security.
Script implementation is:
#! /Bin/sh

Echo "pin: 123"
One implementation in C is:
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
Printf ("pin: 123 ");
}
Compile it into my_pin_helper and put it under/usr/bin. Then you can change hcid. conf of the board:
Pin_helper/usr/pin/my_pin_helper
Later, I wrote a pin_helper on the board using MiniGUI, which is quite simple.

Okay, everything is ready. Do ramdisk well. Remember to put the mknod RFCOMM in/Dev and put it on the board to run it!
Plug in the USB adapter and do not use probe for Modules
# Hciconfig hci0 up
# Hcid-F/etc/Bluetooth/hcid. conf
# Hciconfig-
The device is displayed, and other operations are OK. In combination with PPP, you can use a mobile phone to access the Internet ~~

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.