Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE: additional features #5186

Open
9 of 13 tasks
jimmo opened this issue Oct 6, 2019 · 49 comments
Open
9 of 13 tasks

BLE: additional features #5186

jimmo opened this issue Oct 6, 2019 · 49 comments

Comments

@jimmo
Copy link
Member

jimmo commented Oct 6, 2019

@peterhinch
Copy link
Contributor

As a total BLE n00b I'm looking forward to the docs and samples :)

@jonsmirl
Copy link

jonsmirl commented Oct 6, 2019

How about Unix BLE support? I'd like to have that so that I can use my desktop for development. Of course Unix BLE is way different but that would be hidden behind the API.

@dmazzella
Copy link
Contributor

Is BLE support for STM32WB also planned?

This was referenced Oct 7, 2019
@dpgeorge
Copy link
Member

dpgeorge commented Oct 8, 2019

Extract bytes from UUID class (buffer protocol?) for appending to adv_data.

Should be easiest and most flexible to use the buffer protocol, then bytes(uuid) would give the bytes value.

@dpgeorge dpgeorge changed the title BLE: Outstanding work BLE: additional features Oct 8, 2019
@dpgeorge
Copy link
Member

dpgeorge commented Oct 8, 2019

How about Unix BLE support?

This would be good but no progress has been made yet.

Is BLE support for STM32WB also planned?

Yes, but no timeline yet.

@dpgeorge dpgeorge pinned this issue Oct 8, 2019
@jonsmirl
Copy link

jonsmirl commented Oct 9, 2019

From the PR request thread...

ble.gap_advertise(100,'Micropython')
You need to pass in an advertising packet, eg `b'\x02\x01\x02\x04\x09MPY'

There needs to a helper API for constructing advertising packets. Most people are not familiar with the BLE spec.

@jonsmirl
Copy link

jonsmirl commented Oct 14, 2019

I've been poking around looking at Unix support for Bluetooth. The easiest strategy looks to be using DBUS. Bluez always builds with dbus support so there is no getting rid of it. Buildroot contains the package 'dbus-python', http://dbus.freedesktop.org/releases/dbus-python So can micropython access this package? If it can, then an approach like Adafruit used should work. https://github.com/adafruit/Adafruit_Python_BluefruitLE/blob/master/Adafruit_BluefruitLE/bluez_dbus/provider.py

Does not appear to be simple to get dbus-python working in micropython. Maybe python expert can comment on best way to achieve this?

Looks like an attempt to implement a moddbus.c is here....
https://github.com/turbinenreiter/micropython/blob/dbus2/extmod/moddbus.c
Branch DBUS2

@dpgeorge
Copy link
Member

Is BLE support for STM32WB also planned?

See #5212 for a start on this.

@dpgeorge
Copy link
Member

I've been poking around looking at Unix support for Bluetooth. The easiest strategy looks to be using DBUS.
...
Does not appear to be simple to get dbus-python working in micropython. Maybe python expert can comment on best way to achieve this?

Looks like an attempt to implement a moddbus.c is here

Thanks for looking into this, definitely a good thing to work on.

If Bluez is the way to go, then there are probably a few ways to interface with it:

  1. using system calls to run bluetoothctl
  2. using the ffi module to interface to libdbus.so
  3. implementing a new C module to interface to dbus, like moddbus.c

2 and 3 are basically the same, just 2 is implementing it in pure Python, 3 in C.

@jonsmirl
Copy link

jonsmirl commented Oct 14, 2019

One goal of mine is to have API compatibility between my desktop and target systems (ESP32, Unix) to facilitate a common development environment. Desktop is running bluez and lots of stuff depends on that, so I don't see any obvious way to avoid it. Another reason for keeping Bluez is if the target system is implementing Bluetooth audio.

I have not used Python much, which is a better approach, ffi or C module? Has anyone already coded micropython DBUS support using ffi? What about running the micropython pre-processor over the libdbus header file?

All of this Bluez, dbus, glib, etc support is fairly large so I could also see someone porting code over from a microcontroller to directly access the BLE device. But doing that will lose the ability to run other Linux apps. I'm sure there are boards around running Linux that only have BLE hardware with custom drivers and no BlueZ. Of course these variations will be hidden below the common API.

@ricou73
Copy link

ricou73 commented Oct 15, 2019

currently i do some test on esp32-20191011-v1.11-422-g98c2eabaf.bin with an esp32-wroom.
My python code is very simple, unfortunatly this test show ony 2 ble device where my phone or a lopy4 found 6 ble device. Any clue about why scanning seem so poor on result ? (if i test in my office it's worst, showing only 5 device where nrf connect found 20 of them)

import bluetooth
import ubinascii
import time
bt = bluetooth.BLE()
bt.active(True)
devlist=list()
def bt_irq(evt,data):
	global devlist
	if(evt==16) :
		if not (str(ubinascii.hexlify(data[1])) in devlist):
			devlist.append(str(ubinascii.hexlify(data[1])))
	else:
		print("Evt:"+str(evt)+" DATA:"+str(data))
		EndOf=True
bt.irq(bt_irq)
bt.gap_scan(60000)
time.sleep(60)
print("End of scan")
print(devlist)

@dmartauz
Copy link

dmartauz commented Oct 15, 2019

Try to specify scan interval and window - using the same value will start continuous scanning, e.g.:
bt.gap_scan(60000, 30000, 30000). Interval and window values are in microseconds.

@ricou73
Copy link

ricou73 commented Oct 15, 2019

I guess i have to wait the next available binary build because on g98c2eabaf tag i have this error calling gap_scan
TypeError: function expected at most 2 arguments, got 4
(i saw the modification about gap_scan is commited on the 11 october). I will keep you informed

@dmartauz
Copy link

You can build the binary yourself:
https://github.com/micropython/micropython/blob/master/ports/esp32/README.md

@jonsmirl
Copy link

jonsmirl commented Oct 16, 2019

Another way to do Linux support is to access the functions via a socket like this code from the Bluez source illustrates, it is a test program. ./configure --enable-testing will build it. The test program creates a peripheral device that I was able to access on my phone.
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/peripheral
As a bonus I believe this code can run either with or without the bluez stack running. This has potential to be much more memory efficient than the dbus approach since you can run it standalone is small systems.

Edit -- I see now that these commands are all available in ASCII form from bluetoothctl as mentioned earlier.

@ricou73
Copy link

ricou73 commented Oct 16, 2019

You can build the binary yourself:
https://github.com/micropython/micropython/blob/master/ports/esp32/README.md

I have tryed but miserably failed. I have and windows with msys2, xtensia and was able to build lopy4 custom firmware, but when i try micropython build i run into a lot of trouble.
the last was when i run make i have the following error (before that i have patched pyserial to successfull ompilation for esptool)

CC ../../extmod/modbtree.c
In file included from ../../extmod/modbtree.c:37:0:
../../lib/berkeley-db-1.xx/PORT/include/db.h:1:1: error: expected identifier or '(' before '.' token
../../include/db.h
^
In file included from ../../extmod/modbtree.c:38:0:
../../lib/berkeley-db-1.xx/PORT/include/../../btree/btree.h:89:3: error: data definition has no type or storage class [-Werror]
} PAGE;
^
../../lib/berkeley-db-1.xx/PORT/include/../../btree/btree.h:89:3: error: type defaults to 'int' in declaration of 'PAGE' [-Werror=implicit-int]
../../lib/berkeley-db-1.xx/PORT/include/../../btree/btree.h:126:2: error: unknown type name 'u_int32_t'
u_int32_t ksize; /* key size */
^

I have no idea to what i have to do for to correcting this, .

@jonsmirl
Copy link

jonsmirl commented Oct 16, 2019

The littlevgl port should work for esp32 and BT. Set it to use the IDF 4 commit tag it will list for you.
https://github.com/littlevgl/lv_micropython

It is working for me

@diginfo
Copy link

diginfo commented Oct 18, 2019

Is this a bug ??

import ubinascii
import bluetooth as bt
addr=ubinascii.unhexlify(b'cdd751150f7e')
bt.BLE.gap_connect(0,addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument has wrong type

@dpgeorge
Copy link
Member

bt.BLE.gap_connect(0,addr)

That should be: bt.BLE().gap_connect(...). But it also needs bt.BLE().active(1) to start it.

@diginfo
Copy link

diginfo commented Oct 18, 2019

Oops, that's embarrassing, sorrie about that

@jonsmirl
Copy link

jonsmirl commented Oct 18, 2019

New idea on how to do Unix support... How about writing a transport driver for the Nimble Host piece that calls the Linux kernel BT API? Both of these APIs are HCI based. The Linux side needs to go through the kernel so that it won't disrupt other uses of the adapter like Bluetooth Audio.

I am not familiar enough yet with the two APIs to understand what is involved. Anyone have more experience in this area? One issue is that both stacks implement L2CAP.

Edit: Nimble people informed me it has already been ported.
https://github.com/apache/mynewt-nimble/tree/master/porting/examples/linux

Can one of the micropython nimble experts look into enabling this? The downside is that it has to have exclusive use of the bluetooth device. For my use case I also need bluetooth audio running so that means I have to keep working on a DBUS/glib solution.

@dpgeorge
Copy link
Member

How about writing a transport driver for the Nimble Host piece that calls the Linux kernel BT API?
...
Nimble people informed me it has already been ported.

Yes, this would be a good solution because 1) it's the simplest way to get the bluetooth module working on Linux; 2) it will provide a way to test our nimble bindings.

The downside is that it has to have exclusive use of the bluetooth device.

Right, so it might also be a good idea to provide a dbus/other solution as an alternative on Linux.

@jonsmirl
Copy link

Quick solution for desktop is to just plug in an extra USB BT adapter. BlueZ can have one and micropython development can use the other.

@diginfo
Copy link

diginfo commented Nov 7, 2019

Can you please add the ability to:

  1. Get a list of connection handles
  2. Get the RSSI of a connected device

Many Thanks

@dmazzella
Copy link
Contributor

@jimmo @dpgeorge some progress on

  • Pairing & bonding support.

?

@Cubic-Rambo
Copy link

How to make the nRF52 board support the ubluetooth library?

@playxz
Copy link

playxz commented Nov 20, 2019

@jimmo @dpgeorge some progress on

  • Pairing & bonding support.

?

Any news?

@playxz
Copy link

playxz commented Nov 20, 2019

What about descriptors?
How can I create them?
May I have some examples?

Thx

@pacmac
Copy link

pacmac commented Dec 21, 2019

Is "Get the RSSI of a connected device" anywhere in the pipeline :-)

@dmazzella
Copy link
Contributor

@jimmo @dpgeorge some progress on

Pairing & bonding support.

?

news on this?

@FynnHR
Copy link

FynnHR commented Feb 6, 2020

Does pairing and bonding support also includes Passkey authorization?

@pacmac
Copy link

pacmac commented Feb 18, 2020

Is "Get the RSSI of a connected device" anywhere in the pipeline ??

This would be useful :-)

alu96 pushed a commit to alu96/micropython that referenced this issue Mar 23, 2020
Internally change the representation of UUIDs to LE uint8* to simplify this.

This allows UUIDs to be easily used in BLE payloads (such as advertising).

Ref: micropython#5186
@Walkline80
Copy link

Waiting for pairing & bonding support

c0d3z3r0 pushed a commit to c0d3z3r0/micropython that referenced this issue Apr 5, 2020
Internally change the representation of UUIDs to LE uint8* to simplify this.

This allows UUIDs to be easily used in BLE payloads (such as advertising).

Ref: micropython#5186
@dpgeorge
Copy link
Member

I've been poking around looking at Unix support for Bluetooth.

The bluetooth module is now supported on unix builds, via btstack. See 7563d58

@Walkline80
Copy link

According to NimBLE Host GAP Reference, I'm trying to add pairing & bonding function, and its worked for me, but there's no way to auto reconnect....

Still waiting and hoping for pairing & bonding support

@dmazzella
Copy link
Contributor

Updates on pairing & bonding support?

@street-grease-coder
Copy link

bump @pairing/bonding

@jimmo
Copy link
Member Author

jimmo commented Jan 5, 2021

Updates on pairing & bonding support?
bump @pairing/bonding

@dmazzella @street-grease-coder

This was implemented in #6662 and will be in the 1.14 release for STM32 (e.g. PYBD and WB55).

However it's not supported on ESP32 due to the need for synchronous event handlers. Looking for a volunteer to sort out the necessary plumbing on ESP32. Se notes in #6594 and #6651

@kt-work
Copy link

kt-work commented Feb 23, 2021

Is there any chance to get an event on completed advertisment transmission, that can be evaluated by the handler that has been passed to BLE.irq()?

@jimmo
Copy link
Member Author

jimmo commented Feb 24, 2021

Is there any chance to get an event on completed advertisment transmission, that can be evaluated by the handler that has been passed to BLE.irq()?

@kt-work I'm not sure whether this is supported by either NimBLE or BlueKitchen (or any HCI controller?)... can you provide a link to documentation for another BLE implementation that supports this and I can look into it.

I'm guessing you're looking at implementing some sort of data-over-advertising (e.g. mesh?). I had a look at NimBLE's mesh implementation and it works by starting advertising and sleeping for a number of intervals (i.e. it will always send more than one beacon).

@kt-work
Copy link

kt-work commented Feb 24, 2021 via email

@martinlombana
Copy link

Hi! @jimmo, when can we expect to have bonding capabilities for the ESP32 port? Thanks!

@street-grease-coder
Copy link

Hi! @jimmo, when can we expect to have bonding capabilities for the ESP32 port? Thanks!

Hey, I don't think this is how open source works. They're already doing an amazing job. The way to accelerate this is by contributing, not by asking for deadlines that people can't keep because many are doing it in their free time and the process is usually nonlinear

@martinlombana
Copy link

martinlombana commented Mar 8, 2021

Hi! @jimmo, when can we expect to have bonding capabilities for the ESP32 port? Thanks!

Hey, I don't think this is how open source works. They're already doing an amazing job. The way to accelerate this is by contributing, not by asking for deadlines that people can't keep because many are doing it in their free time and the process is usually nonlinear

Hey yourself! Yes, That is why I supported @jimmo and started paying 3 days ago. Using micropython, asking questions in order to develop my knoledge further, even though I am very new in micropython and low-level stuff, and hopefully, in the near future, start contributing to this community as well.

I am not pressuring anyone. As a matter of fact my question was just a question. You might have imprinted a tone that was not even there to begin with. I re-read my question 10 times, with an open mind, in case there was something I was not seing. It was just a request for information, as I know he is actively developing A LOT of things, for which I am very grateful. Not pressuring anyone here. I am not like that.

Cheers,

@street-grease-coder
Copy link

You should add how future readers can support Jimmo, I'd be in on that :) The rest is not my place to say - and further I'm also a newbie here

@leycec
Copy link

leycec commented Dec 8, 2021

We'd all love to see bonding-on-ESP32, of course – but there appear to be soft blockers in the ESP32 SDK API that sadly complicate this:

However it's not supported on ESP32 due to the need for synchronous event handlers. Looking for a volunteer to sort out the necessary plumbing on ESP32. See notes in #6594 and #6651

Ideally, Espressif themselves would either financially sponsor or supply in-house PR manpower for this community effort that economically benefits them. That isn't happening, so... here we are.

Meanwhile, I'm just delighted that the workhorse AIOBLE framework continues to receive timely commits. Keep up all the incumbent greatness, @jimmo. You're a living embedded systems legend.

@leycec
Copy link

leycec commented Dec 31, 2021

NRF5x BLE support would be phenomenal as well.

Indeed, this would probably make a great first issue for someone with enthusiasm! so, not me Why? Because Adafruit have already done the hard work by adding full support for BLE (including pairing and bonding, crucially) to CircuitPython's existing NRF5x port. Unsurprisingly, the Adapter.c and Connection.c files in the low-level _bleio C extension subdirectory of that port are where all of the good stuff pertaining to pairing happen; likewise, the bonding.c file manages bonding.

So that's a mostly solved problem. Phew! 😅

@dpgeorge dpgeorge unpinned this issue Feb 2, 2022
@puppet13th
Copy link

@dpgeorge @jimmo any new about implementing BLE.config() to set ble txpower. on wemos c3 mini v1.0, ble was unuseable because of antenna/hardware design. The wifi part can be workaround using lower txpower.
I have tried to hardcoded ble txpower using these instruction from the old forum and it works well. BLE connection was stable, but i have not test impact on the range though.

ble scan result comparison:

DEVICE1	-40	lolin_c3_mini_stock
DEVICE1	-48	lolin_c3_mini_0dbm
DEVICE1	-55	ai_thinker_c3
DEVICE1	-56	ai_thinker_c3_3dbm
DEVICE1	-60	lolin_c3_mini_3dbm
		
DEVICE2	-49	lolin_c3_mini_stock
DEVICE2	-57	ai_thinker_c3
DEVICE2	-58	ai_thinker_c3_3dbm
DEVICE2	-60	lolin_c3_mini_3dbm
DEVICE2	-61	lolin_c3_mini_0dbm
		
DEVICE3	-39	lolin_c3_mini_0dbm
DEVICE3	-42	ai_thinker_c3
DEVICE3	-50	lolin_c3_mini_3dbm
DEVICE3	-54	ai_thinker_c3_3dbm
DEVICE3	-59	lolin_c3_mini_stock
		
DEVICE4	-62	lolin_c3_mini_stock
DEVICE4	-64	lolin_c3_mini_0dbm
DEVICE4	-66	lolin_c3_mini_3dbm
DEVICE4	-74	ai_thinker_c3
DEVICE4	-76	ai_thinker_c3_3dbm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests