Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use python to scan and communicate with BLE device under Windows environment

I'm a newbee for python. I searched a lot on website trying to find a way to scan and communicate with BLE device under Windows environment using python, however, almost all the results are under Linux or Android environments. The reason why I ask this question is because I already made a test architecture using python on windows, what I need is just to add a new test case for testing bluetooth LE device into my architecture. Any suggestions will be appreciated! Thanks!

like image 400
28 revs Avatar asked Apr 14 '17 19:04

28 revs


People also ask

What is bleak python?

Bleak is a GATT client software, capable of connecting to BLE devices acting as GATT servers. It is designed to provide a asynchronous, cross-platform Python API to connect and communicate with e.g. sensors.

What is ble scanner for?

BLE Scanner was developed with a vision to help Bluetooth community, developers who wants to build BLE products & applications. BLE Scanner is used by not only developers but also users are using it to find their lost Fitness Trackers and other Bluetooth Smart Devices.

Can Python use Bluetooth?

PyBluez is a Python extension module written in C that provides access to system Bluetooth resources in an object oriented, modular manner.

How do I scan a BLE device?

To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.


1 Answers

Bleak is a Python package that supports BTLE on (not only) Windows. I tested the following code from the project page (after installing it with pip install bleak):

import asyncio
from bleak import BleakScanner

async def run():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

It successfully lists the discovered Bluetooth devices. Examples on how to connect are included in the Bleak project documentation.

like image 106
phispi Avatar answered Oct 26 '22 19:10

phispi