在Raspberry上使用蓝牙4.0 BLE

来源:互联网 发布:新顶级域名注册量 编辑:程序博客网 时间:2024/05/16 18:29

蓝牙适配器配合树莓派使用,需进行如下设置
安装蓝牙软件

sudo apt-get install bluetooth bluez-utils bluez-compat blueman

开启蓝牙服务 ,成功开启应返回OK

service bluetooth status

查看BlueZ版本:
#dpkg –status bluez | grep ‘^Version:’
//dpkg是Debian Packager 的简写。为 Debian专门开发的套件管理系统,方便软件的安装、更新及移除。所有源自Debian的Linux 发行版都使用 dpkg,例如Ubuntu

pi@raspberrypi:~$ dpkg --status bluez | grep '^Version:'Version: 4.99-2$ sudo apt-get install bluetooth bluez-utils bluez-compat blueman

//安装信息如下,可以看出树莓派官方源里面blueZ还是4.99-2。

... ...Setting up blueman (1.23-1) ...Reloading system message bus config...done.Setting up bluetooth (4.99-2) ...Setting up bluez-alsa:armhf (4.99-2) ...Setting up bluez-compat (4.99-2) ...Setting up bluez-cups (4.99-2) ...Setting up bluez-gstreamer (4.99-2) ...Setting up bluez-utils (4.99-2) ...

由于BlueZ 5.0以后可以支持蓝牙4.0 BLE,因此需要更新BlueZ

通过交叉编译获取bluez

首先在PC中安装编译依赖库文件
$ sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev

新建一个文件夹,然后获取bluez的源码

$ wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xz

通过BlueZ的根目录下的README文件,可以知道,编译bluez的依赖文件和配置选项。

... ...In order to compile Bluetooth utilities you need following software packages:     - GCC compiler     - GLib library     - D-Bus library     - udev library (optional)     - readline (command line clients)... ... --disable-systemdDisable integration with systemdBy default the integration with systemd is enabled and installed. This gives the best integration into all distributions based on systemd.This option is provided for distributions that do not support systemd. In that case all integration with the init system is up to the package.... ...

然后进行配置

$ ./configure --disable-systemd... ...configure: error: D-Bus >= 1.6 is required  //提示D-Bus版本太低

(更新源之后,dbus的版本并没有变化)
查看dbus的版本信息

$ dbus-daemon --versionD-Bus Message Bus Daemon 1.4.18

也可以使用apt-cache show package 获取包的相关信息,如说明、大小、版本等

$ apt-cache show package libdbus-1-dev

从中找到一个dbus的网站
发现dbus当前有新版本“The current stable branch is D-Bus 1.10.x.”
http://www.freedesktop.org/wiki/Software/dbus/
http://dbus.freedesktop.org/releases/dbus/

源码安装三步骤:配置(configure)、编译(make)和安装(make install)

$ ./configurechecking for XML_ParserCreate_MM in -lexpat... noconfigure: error: Explicitly requested expat but expat not found

(2015.10.8遇到该问题,暂时没解决)

Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。

–prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share
如果配置–prefix,如:
$ ./configure –prefix=/usr/local/test
可以把所有资源文件放在/usr/local/test的路径中
如果要卸载程序,也可以在原来的make目录下用一次make uninstall

以下在树莓派上操作

首先卸载之前安装的bluetooth bluez-utils bluez-compat blueman
(Bluez的版本太低,不支持low energy 蓝牙)
注意由于使用apt-get安装过一次后,导致使用dpkg命令查询时会出现
$ dpkg –status bluez | grep ‘^Version:’
Version: 4.99-2(说明曾使用apt-get或dpkg安装过该版本的package)

参照http://www.elinux.org/RPi_Bluetooth_LE,执行即可
在树莓派上安装bluez编译依赖库后发现,dbus的版本满足要求

pi@raspberrypi:~$ dbus-daemon --versionD-Bus Message Bus Daemon 1.6.8Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others

在/usr/src/下新建一个bluez目录用来存放bluez源码
/usr/src/bluez/bluez-5.32

pi@raspberrypi:/usr/src/bluez/bluez-5.32$ hciconfighci0:   Type: BR/EDR  Bus: USB        BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8        UP RUNNING PSCAN         RX bytes:929 acl:0 sco:0 events:45 errors:0        TX bytes:928 acl:0 sco:0 commands:45 errors:0

开始扫描

pi@raspberrypi:/usr/src/bluez/bluez-5.32$ sudo hcitool lescanLE Scan ...90:59:AF:0B:36:04 (unknown)90:59:AF:0B:36:04 SensorTag90:59:AF:0B:36:04 (unknown)90:59:AF:0B:36:04 SensorTag... ... ...

可以看到扫到了SensorTag设备,使用Ctrl+C来终止扫描过程。

关于hcitool工具的使用:

root@raspberrypi:/home/pi# hcitool --helphcitool - HCI Tool ver 5.32Usage:        hcitool [options] <command> [command parameters]Options:        --help  Display help        -i dev  HCI deviceCommands:        dev     Display local devices        inq     Inquire remote devices        scan    Scan for remote devices        name    Get name from remote device        info    Get information from remote device        spinq   Start periodic inquiry        epinq   Exit periodic inquiry        cmd     Submit arbitrary HCI commands        con     Display active connections        cc      Create connection to remote device        dc      Disconnect from remote device        sr      Switch master/slave role        cpt     Change connection packet type        rssi    Display connection RSSI        lq      Display link quality        tpl     Display transmit power level        afh     Display AFH channel map        lp      Set/display link policy settings        lst     Set/display link supervision timeout        auth    Request authentication        enc     Set connection encryption        key     Change connection link key        clkoff  Read clock offset        clock   Read local or remote clock        lescan  Start LE scan        leinfo  Get LE remote information        lewladd Add device to LE White List        lewlrm  Remove device from LE White List        lewlsz  Read size of LE White List        lewlclr Clear LE White List        lerladd Add device to LE Resolving List        lerlrm  Remove device from LE Resolving List        lerlclr Clear LE Resolving List        lerlsz  Read size of LE Resolving List        lerlon  Enable LE Address Resolution        lerloff Disable LE Address Resolution        lecc    Create a LE Connection        ledc    Disconnect a LE Connection        lecup   LE Connection UpdateFor more information on the usage of each command use:        hcitool <command> --help
0 0