linux下藍牙開發(bluez應用)


編譯blueZ-5.25

需要先編譯安裝以下包:

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.10

  • glib2.28.6

  • bluez-libs-3.36

  • bluez-utils-3.36

  • bluez-libs-3.36(不需要其他庫)

  • bluez-utils-3.36:

  • 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 ,麻煩給個星星,謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM