目前,蓝牙技术已经比较成熟,特别是基于手机和PC得蓝牙文件传输。
本文主要讲述基于嵌入式arm linux的蓝牙文件传输。
现行2.6.x的linux内核都已经集成了bluez蓝牙驱动,对于2.4版本内核的需要到bluez官方网站下载并安装bluez蓝牙驱动。
本为基于2.6版本的内核讲述。对于2.4下载bluez后安装即可。有了bluez蓝牙驱动还需要安装bluez-libs库和bluez-utils工具
蓝牙测试流程:利用手机连接蓝牙模块,然后进行文件传输。
bluez-utils:蓝牙配置工具,包括hciconfig,hcitool,rfcomm
openobex:蓝牙传输工具
下载bluez-utils:http://bluez.sf.net/download/bluez-utils-3.36.tar.gz
下载openobex:https://sourceforge.net/projects/openobex/files/openobex/1.5/openobex-1.5.0-Source.zip/download
bluez-utils依赖库:
expat-2.0.1:http://downloads.sourceforge.net/expat/expat-2.0.1.tar.gz
dbus-1.4.1:https://dbus.freedesktop.org/releases/dbus/dbus-1.4.1.tar.gz
zlib-1.2.5:http://download.chinaunix.net/down.php?id=35616&ResourceID=12241&site=1
glib-2.28.8:https://download.gimp.org/pub/glib/2.28/glib-2.28.6.tar.gz
bluez-libs-3.36:http://bluez.sf.net/download/bluez-libs-3.36.tar.gz
openobex-1.5依赖库:
##2.1、编译expat-2.0.1
#!/bin/sh
./configure --host=arm-linux-gnueabihf --prefix=$PWD/install
make && make install
##2.2、编译dbus-1.4.1
#!/bin/sh
./configure --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc --prefix=$PWD/install \
CFLAGS=-I$PWD/../expat-2.0.1/install/include \
LDFLAGS=-L$PWD/../expat-2.0.1/install/lib \
--enable-abstract-sockets --with-x=no
make && make install
修改dbus中的pkgconfig文件dbus-1.pc,否则后面编译bluez-utils-3.36会报错
#vim install/lib/pkgconfig/dbus-1.pc
16 Cflags: -I${includedir} -I${libdir}/dbus-1.0/include
##2.3、编译zlib-1.2.5
#./configure --prefix=$PWD/install
替换zlib中所有的编译工具链为arm-linux-gnueabihf-
#vim Makefile
19 CC=arm-linux-gnueabihf-gcc
20
21 CFLAGS= -D_LARGEFILE64_SOURCE=1
22 #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
23 #CFLAGS=-g -DDEBUG
24 #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
25 # -Wstrict-prototypes -Wmissing-prototypes
26
27 SFLAGS= -fPIC -D_LARGEFILE64_SOURCE=1
28 LDFLAGS= -L. libz.a
29 TEST_LDFLAGS=-L. libz.a
30 LDSHARED=arm-linux-gnueabihf-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
31 CPP=arm-linux-gnueabihf-gcc -E
32
33 STATICLIB=libz.a
34 SHAREDLIB=libz.so
35 SHAREDLIBV=libz.so.1.2.5
36 SHAREDLIBM=libz.so.1
37 LIBS=$(STATICLIB) $(SHAREDLIBV)
38
39 AR=arm-linux-gnueabihf-ar rc
40 RANLIB=arm-linux-gnueabihf-ranlib
41 LDCONFIG=arm-linux-gnueabihf-ldconfig
42 LDSHAREDLIBC=-lc
43 TAR=tar
44 SHELL=/bin/sh
45 EXE=
#make && make install
##2.4、编译glib-2.28.8
vim compile.sh
#!/bin/sh
echo "glib_cv_stack_grows=no">config.cache
echo "glib_cv_uscore=no">>config.cache
echo "ac_cv_func_posix_getpwuid_r=yes">>config.cache
echo "ac_cv_func_posix_getgrgid_r=yes">>config.cache
echo "ac_cv_lib_rt_clock_gettime=no">>config.cache
echo "glib_cv_monotonic_clock=yes">>config.cache
export PKG_CONFIG_PATH=$PWD/../zlib-1.2.5/install/lib/pkgconfig
./configure --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc --prefix=$PWD/install/ -cache-file=config.cache
make && make install
#./compile.sh
##2.5、编译bluez-libs-3.36
#./configure --host=arm-linux-gnueabihf --prefix=$PWD/install
#make && make install
##2.6、 编译bluez-utils-3.36
#vim compile.sh
export TOPDIR=$PWD/..
export PKG_CONFIG_PATH=$TOPDIR/glib-2.28.8/install/lib/pkgconfig:\
$TOPDIR/dbus-1.4.1/install/lib/pkgconfig:\
$TOPDIR/zlib-1.2.5/install/lib/pkgconfig:\
$TOPDIR/bluez-libs-3.36/install/lib/pkgconfig
./configure --host=arm-linux-gnueabihf --prefix=$PWD/install\
CFLAGS="`pkg-config --cflags bluez dbus-1 glib-2.0 zlib`" \
LDFLAGS="`pkg-config --libs bluez dbus-1 glib-2.0 zlib`"
#make &&make install
重新编译一次,因为生成的部分命令有默认路径问题
#vim compile.sh
export TOPDIR=$PWD/..
export PKG_CONFIG_PATH=$TOPDIR/glib-2.28.8/install/lib/pkgconfig:\
$TOPDIR/dbus-1.4.1/install/lib/pkgconfig:\
$TOPDIR/zlib-1.2.5/install/lib/pkgconfig:\
$TOPDIR/bluez-libs-3.36/install/lib/pkgconfig
./configure --host=arm-linux-gnueabihf \
CFLAGS="`pkg-config --cflags bluez dbus-1 glib-2.0 zlib`" \
LDFLAGS="`pkg-config --libs bluez dbus-1 glib-2.0 zlib`"
#make
然后将install/bin和install/sbin目录下的文件替换掉
#vim compile.sh
#!/bin/sh
export TOPDIR=$PWD/../
export PKG_CONFIG_PATH=$TOPDIR/bluez-libs-3.36/install/lib/pkgconfig
./configure --host=arm-linux-gnueabihf --prefix=$PWD/install\
CFLAGS="`pkg-config --cflags bluez`" \
LDFLAGS="`pkg-config --libs bluez`"
#make && make install
先将上述编译生成的库文件及命令拷贝至板子上。
查看蓝牙设备
#hciconfig
激活蓝牙设备
#hciconfig hci0 up
配置蓝牙设备
#vim /etc/bluetooth/rfcomm.conf
#
# HCI daemon configuration file.
#
# HCId options
options {
# Automatically initialize new devices
autoinit yes;#改成yes
# Security Manager mode
# none - Security manager disabled
# auto - Use local PIN for incoming connections
# user - Always ask user for a PIN
#
security auto;
# Pairing mode
# none - Pairing disabled
# multi - Allow pairing with already paired devices
# once - Pair once and deny successive attempts
pairing multi;
# Default PIN code for incoming connections
passkey "BlueZ";
pin_helper /usr/bin/bluepin
}
# Default settings for HCI devices
device {
# Local device name
# %d - device id
# %h - host name
name "BlueZ (%d)";
# Local device class
class 0x000100;
# Default packet type
#pkt_type DH1,DM1,HV1;
# Inquiry and Page scan
iscan enable; pscan enable;
# Default link mode
# none - no specific policy
# accept - always accept incoming connections
# master - become master on incoming connections,
# deny role switch on outgoing connections
lm accept;
# Default link policy
# none - no specific policy
# rswitch - allow role switch
# hold - allow hold mode
# sniff - allow sniff mode
# park - allow park mode
lp rswitch,hold,sniff,park;
}
root@myzr:~# cat /etc/bluetooth/rfcomm.conf
#
# RFCOMM configuration file.
#
rfcomm0 {
# # Automatically bind the device at startup
bind no;
#
# # Bluetooth address of the device
device CC:08:8D:5B:26:94;#改成待连接设备MAC地址
#
# # RFCOMM channel for the connection
channel 1;#通道
#
# # Description of the connection
comment "Example Bluetooth device";
}
#vim /etc/bluetooth/hcid.conf
#
# HCI daemon configuration file.
#
# HCId options
options {
# Automatically initialize new devices
autoinit yes;
# Security Manager mode
# none - Security manager disabled
# auto - Use local PIN for incoming connections
# user - Always ask user for a PIN
#
security auto;
# Pairing mode
# none - Pairing disabled
# multi - Allow pairing with already paired devices
# once - Pair once and deny successive attempts
pairing multi;
# Default PIN code for incoming connections
passkey "BlueZ";
#pin_helper为线程名称,该线程调用/usr/bin/bluepin
#/usr/bin/bluepin为shell脚本,内容下文提及
pin_helper /usr/bin/bluepin
}
# Default settings for HCI devices
device {
# Local device name
# %d - device id
# %h - host name
name "BlueZ (%d)";
# Local device class
class 0x000100;
# Default packet type
#pkt_type DH1,DM1,HV1;
# Inquiry and Page scan
iscan enable; pscan enable;
# Default link mode
# none - no specific policy
# accept - always accept incoming connections
# master - become master on incoming connections,
# deny role switch on outgoing connections
lm accept;
# Default link policy
# none - no specific policy
# rswitch - allow role switch
# hold - allow hold mode
# sniff - allow sniff mode
# park - allow park mode
lp rswitch,hold,sniff,park;
}
#vim /usr/bin/bluepin
#!/bin/sh
#输出PIN码
echo "PIN:1234"
配置蓝牙PIN码文件
#vim /var/lib/bluetooth/A4:17:31:C7:9B:B6/pincodes
CC:08:8D:5B:26:94 1234
/var/lib/bluetooth/A4:17:31:C7:9B:B6/pincodes:A4:17:31:C7:9B:B6本地蓝牙设备MAC地址
#待连接蓝牙设备MAC地址CC:08:8D:5B:26:94
#1234:PIN码
#hcid /etc/bluetooth/hcid.conf
#rfcomm bind /dev/rfcomm0 CC:08:8D:5B:26:94 1
方式1:连接蓝牙设备,在我这不好用
#rfcomm conn /dev/rfcomm0 CC:08:8D:5B:26:94 1
方式2:连接蓝牙设备
#cat /dev/rfcomm0 &