linux 蓝牙脚本,linux下蓝牙开发(bluez应用)
编译blueZ-5.25须要先编译安装如下包:htmlbluez-libs-3.36.tar.gzexpat-2.1.0.tar.gzdbus-1.10.0.tar.gzglib-2.26.1.tar.gzbluez-utils-3.36.tar.gzlibusb-1.0.9.tar.bz2D-bus1.8.10linuxglib2.28.6gitbluez-libs-3.36githubblue
编译blueZ-5.25
须要先编译安装如下包:html
bluez-libs-3.36.tar.gz
expat-2.1.0.tar.gz
dbus-1.10.0.tar.gz
glib-2.26.1.tar.gz
bluez-utils-3.36.tar.gz
libusb-1.0.9.tar.bz2
D-bus1.8.10linux
glib2.28.6git
bluez-libs-3.36github
bluez-utils-3.36编程
bluez-libs-3.36(不须要其余库)bash
bluez-utils-3.36:socket
glib-2.16.5工具
dbus-1.0.2: libxml2-2.7.4测试
libsndfile-1.0.17网站
libusb-0.1.12
注:/opt/libs 和/opt/utils是我本身编译时用的路径,你能够随便的选取。
下载地址: https://download.csdn.net/download/suizhuantoukuai1/10018231
1,编译安装bluez-lib-3.36.tar.gz
这个库不须要什么依赖,直接解压,配置,编译而后安装便可。
#tar zxvf bluez-lib-3.36.tar.gz
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC=arm-linux-gcc
#make
#make install
2,编译安装libxml2-2.7.4.tar.gz
这个库是后面的dbus依赖的,你也能够用expat来代替libxml2,官方网站上说dbus必须依赖于他们中的一个。
#tar zxvf libxml2-2.7.4.tar.gz
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC=arm-linux-gcc
#make
#make install
3,编译安装dbus-1.0.2.tar.gz
#tar zxvf dbus-1.2.16.tar.gz
#echo ac_cv_have_abstract_sockets=yes>arm-linux.cache
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC="arm-linux-gcc -I/opt/libs/include -L/opt/libs/lib" --cache-file=arm-linux.cache --with-x=no
#make
#make install
4,编译安装glib-2.16.5
#tar -zxjf glib-2.16.5.tar.bz2
#echo ac_cv_type_long_long=yes>arm-linux.cache
#echo glib_cv_stack_grows=no>>arm-linux.cache
#echo glib_cv_uscore=no>>arm-linux.cache
#echo c_cv_func_posix_getpwuid_r=yes>>arm-linux.cache
#echo ac_cv_func_posix_getgrgid_r=yes>>arm-linux.cache
注意:">"和">>"的区别
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC="arm-linux-gcc -I/opt/libs/include -L/opt/libs/lib" --cache-file=arm-linux.cache
#make
#make install
5,编译安装libusb-0.1.12.tar.gz
这个库不须要什么依赖,直接解压,配置,编译而后安装便可。
#tar zxvf libusb-0.1.12.tar.gz
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC="arm-linux-gcc -I/opt/libs/include -L/opt/libs/lib"
#make
#make install
6,编译安装bluez-utils-3.36.tar.gz
#tar zxvf bluez-utils-3.36.tar.gz
#./configure --profix=/opt/libs --host=arm-linux --target=arm-linux CC="arm-linux-gcc -I/opt/libs/include -L/opt/libs/lib" --disable-audio
#make
#make install
在配置 ./configure –prefix=/opt/arm/bluez 时 说明库会被安装在/opt/arm/bluez 下
#copy /opt/arm/bluez/sbin/* 到你的文件系统/sbin
cp bluez/sbin/* /sbin/
#copy /opt/arm/bluez/bin/ 下的 hcitool,rfcomm,sdptool 到你的文件系统 /bin
cp bluez/bin/* /bin/
#copy /opt/arm/bluez/lib/* 到文件系统的/lib下
#copy /opt/arm/bluez/etc/bluetooth/* 到你的文件系统 /etc
cp bluez/etc/bluetooth /etc/
移植后,会生成一些工具,涉及的工具:
hciconfig
查看蓝牙设备的属性,设置蓝牙设备的操做
rfcomm
用于建立设备绑定本地的蓝牙设备,/dev/rfcomm0,创建虚拟串口
sdptool
增长/删除蓝牙服务
pand
配置蓝牙
hcitool
用于链接断开设备
bluetoothd
蓝牙设备的守护进程
hciattach
初始化(串口/波特率)和绑定tty设备
测试
hciconfig hci0 up 启用蓝牙
hciconfig hci0 iscan配置开发板蓝牙可被查找
hcitool scan 查找蓝牙
Scanning ...
04:02:1F:A2:B2:AF huawei 10 # 手机
00:13:EF:A0:00:AF test # 蓝牙耳机
【参考】 http://blog.csdn.net/gatieme/article/details/48751743 http://blog.csdn.net/wangzhen209/article/category/5885205 http://blog.sina.com.cn/s/blog_533074eb01015hfc.html
hcid.conf配置文件 其中hcid.conf有关配对信息。其中security user;表示每次配对询问用户对方PIN,而auto则直接采用passkey中的PIN码。
若是修改了此文件则须要重启bluetooth服务。
重启蓝牙请使用以下命令
service bluetooth restart
或者
/etc/init.d/bluetooth restart
蓝牙设置状态:
UP
Down
Inquiry Scan
PAGE Scan
设置PSCAN和ISCAN后,才能被搜索或链接。Inquiry Scan状态表示设备可被inquiry. Page Scan状态表示设备可被链接。
hciconfig hci0 iscan
hciconfig hci0 pscan
或者:hciconfig hci0 piscan
基本socket知识
struct sockaddr
{
sa_family_t sa_family; //地址族
char sa_data[14]; //地址数据
};
若是是用AF_INET(IPV4),则它的地址类型sockaddr_in以下,恰好与struct sockaddr对应
struct sockaddr_in
{
sa_family_t sin_family; //地址族
uint16_t sip_port; //端口
struct in_addr sin_addr; //Internel 地址
unsigned char sin_zero[8]; //占位字节
};
若是是用Bluetooth协议族(PF_BLUETOOTH)中的协议l2cap(BTPROTO_L2CAP),则地址格式以下:
struct sockaddr_l2
{
sa_family_t l2_family; //地址族
unsigned short l2_psm; //PSM
bdaddr_t l2_bdaddr; //Bluetooth 地址
unsigned short l2_cid;
};
蓝牙驱动
在内核选中支持蓝牙相关选项后,从新编译内核,会出现下面几个和蓝牙有关的驱动:
bluetooth.ko
bnep.ko
rfcomm.ko
hci_uart.ko
能够写个脚本,须要在系统启动的时候,加载这几个驱动
#!/bin/bash
insmod bluetooth.ko
insmod bnep.ko
insmod rfcomm.ko
insmod hci_uart.ko
蓝牙启动脚本 /etc/init.d/bluetooth
执行蓝牙启动脚本 /etc/init.d/bluetooth start,开启蓝牙,此后就能够对蓝牙进程应用编程了
蓝牙应用编程
更多推荐
所有评论(0)