linux下藍牙開發(bluez應用)

編譯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

腳本地址:https://github.com/liangjfblue/bluezTest ,麻煩給個星星,謝謝

執行藍牙啓動腳本 /etc/init.d/bluetooth start,開啓藍牙,此後就能夠對藍牙進程應用編程了

藍牙應用編程

demo地址:https://github.com/liangjfblue/bluezTest ,麻煩給個星星,謝謝

相關文章
相關標籤/搜索