Windows 10, Bluetooth LE and Python

82 views
Skip to first unread message

Wayne Keenan

unread,
Nov 15, 2017, 4:35:27 AM11/15/17
to python-bleson
tl;dr

best case:  Python C++ Extension
'best worst' case: External C# Binary and a form of IPC
'worst worst' case: Windows Store App distributed Python + Custom extensions  (Insane, should be vendor (MS/Python/IronPython) provided)


Above is how I currently see how bleson can access the Win10 UWP BluetoothLE API's (which is by no means an expert opinion or only set of options)

Background:

I thought I would try something a little less complex and is supported OOTB, so I played with running Python scripts on a Win10 IoT Core Pi 3; 
It is now confirmed (link to forum post below) that the MS Python extensions for WinIoT only support: i2c, SPI and GPIO but nothing else like the Windows.Device.BluetoothLE API's.  

Therfore the Python to Win10 API integration will have to be some form of native component

I suggest that this is a separate repo/module to the core bleson module and an optional runtime dependancy, being a mandatory import dependancy only for thebleson.providers.win32 package.


Some info for the road ahead:

1) Creating a C++ extension for Python

2) Bluetooth FAQ

Which states:
"Can I access these APIs from Win32?
Yes, all these APIs should work. This blog details the way to call Windows APIs from Desktop applications.""


3) UWP APIs callable from a classic desktop app

4) UWP APIs available to a packaged desktop app (Desktop Bridge)

5) Calling Windows 10 APIs From a Desktop Application


A handful of API's of interest to bleson that indicate they are callable from non-UWP apps (have DualApiPartition attrib, see link 3 above) , but I'm not 100% on the 'package identity' requirement of them so can't say for sure (the 'worst-worst' case if they aren't):

https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter

https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementwatcher

https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementpublisher

https://docs.microsoft.com/en-us/uwp/api/windows.devices.enumeration.devicewatcher


Of note:

Win IoT has moved on since I tried it a year or so ago; it has a GUI with Keyboard and mouse support for its minimal desktop which has a CMD line, browser and settings GUI where it's possible to configure wifi and scan and pair with Bluetooth RFCOMM and LE devices.

It possible to create foreground graphical apps, but not in python, they are background only (this should have been a clue as to the level of Python Wind10 API support)


I included most of the info above when posting on the forum where I've asked when support of UWP API's for Python on Desktop and IoT they eluded to might come: https://social.msdn.microsoft.com/Forums/en-US/1ca4719b-8129-4595-8542-5ea837c5f1dd/win10-iot-core-bluetooth-le-api-access-from-python?forum=WindowsIoT


Wayne Keenan

unread,
Nov 15, 2017, 5:03:49 AM11/15/17
to python-bleson
link 1 should be:  https://docs.microsoft.com/en-us/visualstudio/python/cpp-and-python

I 'd like to also apologies for the leap from 'it doesn't work on Win IoT' so the Win 10 Desktop platform (bleson is targeting) doesn't either, but, AFAIK, it doesn't.

Thanks,
Wayne 

Mobile: +447717781468  
Skype: wayne.keenan
Twitter: @wkeenan

The Bubbleworks Factory Limited 
Fenscape Offices, Harrier House, Sedgeway Business Park, 
Witchford, Cambridgeshire, CB6 2HY

Registered in England and Wales, 
Company Registration Number 10191037 / VAT GB 246639773

Follow @The_Bubbleworks on Twitter.




--
You received this message because you are subscribed to the Google Groups "python-bleson" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-bleson+unsubscribe@googlegroups.com.
To post to this group, send email to python...@googlegroups.com.
Visit this group at https://groups.google.com/group/python-bleson.
To view this discussion on the web, visit https://groups.google.com/d/msgid/python-bleson/4e3b6535-f733-4ad6-b8dc-fd806cc6cced%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wayne Keenan

unread,
Nov 16, 2017, 1:26:06 PM11/16/17
to python-bleson
Hi,

I've created a new Windows specific native Python module, https://github.com/TheCellule/blesonwin 

It is a wrapper around (some of) the C++WinRT Bluetooth LE API and doesn't need any kind of Bluetooth driver or USB tweaks.
It can scan (and pass data to Python) but not yet advertise.

Usage of the module has been integrated into the main Bleson win32 provider, now committed to PR#1, and the unmodified Observer example receives RSSI values along with other data, but the completeness and the correct transformation of the other data is WIP.

It's "pip install ." installable but you will need VS 2017 to compile it, I'll look at putting it on PyPi as a binary install soon, because...

It could really do with testing on a clean/non-developer machine to iron out any external dependancy, permission or other issues.

Thanks
Wayne

Wayne Keenan

unread,
Nov 17, 2017, 2:53:45 AM11/17/17
to python-bleson
With a bit of luck, running this pip command on a Win10 box should install the Bleson WinRT BluetoothLE wrapper wheel:

pip install blesonwin

Then in a Python3 REPL or script, try:

from time import sleep

from blesonwin import *


initialise()

on_advertisement(lambda x: print(x))

start_observer()

sleep(5)

stop_observer()


If it runs you might see some test output like:

{'RSSI': -94, 'ADDRESS': '5', 'LOCALNAME': ''}
8995013955798
03564548
{'RSSI': -84, 'ADDRESS': '0', 'LOCALNAME': ''}
96222817739678
03564548


I have low confidence of it actually running at this stage, especially anything outside of:

Windows 10 build 16299  (64bit)
Python 3.6.32


Thanks
Wayne

Reply all
Reply to author
Forward
0 new messages