zoukankan      html  css  js  c++  java
  • 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 ,麻烦给个星星,谢谢

  • 相关阅读:
    深入理解ThreadLocal
    synchronized与Lock的区别与使用
    1亿个数中找出最小的100个数--最小堆
    B+/-Tree原理(mysql索引数据结构)
    深入理解token
    shiro(java安全框架)
    第一次项目上Linux服务器(四:CentOS6下Mysql数据库的安装与配置(转))
    第一次项目上Linux服务器(三:安装Tomcat及相关命令)
    第一次项目上Linux服务器(二:——安装jdk)
    第一次项目上Linux服务器(一:远程连接服务器)
  • 原文地址:https://www.cnblogs.com/liangjf/p/8677563.html
Copyright © 2011-2022 走看看