Play with BLE(2)_Use bluepy to scan BLE broadcast data

1 Introduction

Under the linux platform, bluez is a very good software that provides many command-line-based testing tools, such as hciconfig, hcitool, hcidump, bluetoothctl, etc. Using these tools, we can easily test and demo various Bluetooth functions. For example, in " Playing with BLE(1)_Eddystone beacon ", we use the hcitool command to demonstrate the magical effect of turning a mobile phone into a beacon device.

The Beacon demo is, in essence, a test and verification of the BLE Advertising (broadcast) functionality. Naturally, we will be curious: how to receive these broadcast data (in fact, the BLE Scanning function)? That's what this article is about.

Although hcitool (and later bluetoothctl) can perform simple LE scan operations, the returned results only include simple addresses and names, which obviously cannot meet our needs (you must know that BLE broadcast data may contain other content, such as our Beacon demo). How to do it? Don't worry, powerful python is here.

2. Introduction to bluepy [1]

bluepy is a BLE interface encapsulated by python script in linux platform. Why use python? To be honest, before writing this article, my impression of python was extremely bland, but at this time, my favor has greatly increased. Take the Bluetooth test as an example:

Under linux, the native interface provided by bluez is a C library (bluetooth lib). We want to use this interface to play some functions in the command line, which is almost impossible;

While providing the C library, bluez encapsulates a layer of dbus API for the convenience of graphical interface operation. Although in theory, using commands such as dbus-send can meet our testing needs on the command line. However, I don't think anyone likes the long list of parameters behind dbus-send...

In the open source world, where there is demand, there is motivation, so there are many Bluetooth APIs packaged in python. Taking BLE as an example, bluepy is a better one. With this interface, the test of the Bluetooth function is really cool!

3. bluepy installation

The installation of bluepy is very simple. Before starting, let me explain my test environment:

The hardware environment is Raspberry Pi 2 (everyone should use a PC or development board running linux at will);

OS is raspbian (Ubuntu), the version should not be very important;

The version of bluez is 5.23 (a relatively new version, but there should be no problem with the old version).

安装bluepy只需要两个命令,如下:

#安装python的包管理工具—pip

sudo apt-get install python-pip libglib2.0-dev

#使用pip安装bluepy

sudo pip install bluepy

4. Using bluepy

本章我们将使用bluepy的Scanner class,扫描正在广播的BLE设备。bluepy的文档中有sample code:
http://ianharvey.github.io/bluepy-doc/scanner.html
在本地新建一个python脚本(ble_scan.py),将sample code中的代码拷贝进去即可,如下:
from bluepy.btle import Scanner, DefaultDelegate

class ScanDelegate(DefaultDelegate): 
    def __init__(self): 
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData): 
        if isNewDev: 
            print "Discovered device", dev.addr 
        elif isNewData: 
            print "Received new data from", dev.addr

scanner = Scanner().withDelegate(ScanDelegate()) 
devices = scanner.scan(10.0)

for dev in devices: 
    print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi) 
    for (adtype, desc, value) in dev.getScanData(): 
        print "  %s = %s" % (desc, value)

按照“玩转BLE(1)_Eddystone beacon”中的说明,将自己的手机变成一个Eddystone Beacon设备。然后执行python脚本,扫描:

sudo python ble_scan.py

结果如下:

pi@raspberrypi:~$ sudo python ble_scan.py 
Discovered device cb:07:c8:52:ce:d6 
Discovered device 18:dc:56:a6:25:6f 
Device 18:dc:56:a6:25:6f (public), RSSI=-59 dB 
  Flags = 06 
  Complete 16b Services = aafe 
  16b Service Data = aafe00f0000102030405060708090a0b0e0f000000 
Device cb:07:c8:52:ce:d6 (random), RSSI=-85 dB 
  Complete Local Name = My Mambo 
  Complete 16b Services = e7fe 
  Manufacturer = 0000cb07c852ced6 
  128b Service Solicitation = d0002d121e4b0fa4994eceb531f40579 
  Flags = 06

对比一下我们在“玩转BLE(1)_Eddystone beacon”中广播的数据(对应三个颜色):

1e 02 01 06 03 03 aa fe 17 16 aa fe 00 -10 00 01 02 03 04 05 06 07 08 09 0a 0b 0e 0f 00 00 00 00

成功了!

5. 总结

本文简单的介绍了利用bluepy功能,扫描BLE的广播包的过程。并没有对bluepy的软件逻辑,以及BLE Advertising的细节,做过多的说明,感兴趣的同学可以自行阅读,不理解的地方可以留言讨论。

另外,后续BLE Advertising & Scanning有关的分析文章中,会借助该工具,以帮助理解。

6. 参考文档

[1] bluepy github主页,https://github.com/IanHarvey/bluepy

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325415897&siteId=291194637