Bluepy: Python interface to Bluetooth LE on Linux: Difference between revisions
Jump to navigation
Jump to search
(Created page with "== Installation == Follow the instruction on official page on: https://github.com/IanHarvey/bluepy or the do the following: To install the current released version, on most De...") |
No edit summary |
||
Line 7: | Line 7: | ||
After installation, we can import the library into our python project: | After installation, we can import the library into our python project: | ||
import btle | import btle | ||
== Scan for deivces == | |||
The official document for Scanner class can be found at: http://ianharvey.github.io/bluepy-doc/scanner.html | |||
The library come with a class called “Scanner” that allow us to scan for LE devices. We will import the class with “from btle import Scanner” for example to scan and print all discovered devices. | |||
from bluepy.btle import Scanner | |||
scanner = Scanner() | |||
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) | |||
We can also set a custom scan delegate to handler the device discovery, for example a scan delegate function that print the address of discovered devices. | |||
from bluepy.btle import Scanner, DefaultDelegate | |||
import time | |||
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) | |||
while True: | |||
time.sleep(1) |
Revision as of 14:09, 13 December 2017
Installation
Follow the instruction on official page on: https://github.com/IanHarvey/bluepy or the do the following: To install the current released version, on most Debian-based systems:
sudo apt-get install python-pip libglib2.0-dev sudo pip install bluepy
After installation, we can import the library into our python project:
import btle
Scan for deivces
The official document for Scanner class can be found at: http://ianharvey.github.io/bluepy-doc/scanner.html The library come with a class called “Scanner” that allow us to scan for LE devices. We will import the class with “from btle import Scanner” for example to scan and print all discovered devices.
from bluepy.btle import Scanner scanner = Scanner() 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)
We can also set a custom scan delegate to handler the device discovery, for example a scan delegate function that print the address of discovered devices.
from bluepy.btle import Scanner, DefaultDelegate import time 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) while True: time.sleep(1)