micropython / micropython

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems
https://micropython.org
Other
19.15k stars 7.67k forks source link

Can not apply advertisment data #4288

Open linycm opened 5 years ago

linycm commented 5 years ago

Hi @glennrub ,

I got a similar issue with https://github.com/micropython/micropython/issues/4179 .

I test bluetooth example on my NRF board PCA10040, then got error for cannot apply advertisment data. I put some in REPL as below:

from ubluepy import Service, Characteristic, UUID, Peripheral, constants uuid_env_sense = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e") serv_env_sense = Service(uuid_env_sense) periph = Peripheral() periph.addService(serv_env_sense) periph.advertise(device_name="micr_temp", services=[serv_env_sense])

And I got feedback: Traceback (most recent call last): File "", in OSError: Can not apply advertisment data. status: 0x09

Rdgs, Martin

linycm commented 5 years ago

Maybe my example something wrong, I used old firmware version (should be softdevice3.0) as below, that work for bluetooth connection.

MicroPython v1.8.1-3865-gd91c351-dirty on 2018-05-01; PCA10040 with NRF52832

from machine import RTCounter, Temp from ubluepy import Service, Characteristic, UUID, Peripheral, constants uuid_env_sense = UUID("0x181A") serv_env_sense = Service(uuid_env_sense) temp_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ temp_attrs = Characteristic.ATTR_CCCD uuid_temp = UUID("0x2A6E") char_temp = Characteristic(uuid_temp, props = temp_props, attrs = temp_attrs) serv_env_sense.addCharacteristic(char_temp) periph = Peripheral() periph.addService(serv_env_sense) periph.advertise(device_name="micr_temp", services=[serv_env_sense])

But when I changed to updated version firmware with softdevice 6.0, bluetooth signal can be advertised but cannot connect.

Rdgs, Martin

edwios commented 5 years ago

I have the same problem, using the stock example ubluepy_temp.py, advertising is ok, event_handler EVT_GAP_CONNECTED ok, but then that's all. Connection soon timeout after a while.

glennrub commented 5 years ago

@linycm, are you running this from a build of master branch?

I'm not able to re-produce the error, as the code you posted advertise and connects for me. Do you have any log from the central that could hint on why the connection fails?

Alternatively, you could enable some logging from the ble_drv.c by toggling the BLE_DRIVER_VERBOSE to 1 to see why the connect might fail on the peripheral side. https://github.com/micropython/micropython/blob/master/ports/nrf/drivers/bluetooth/ble_drv.c#L43

linycm commented 5 years ago

@glennrub, I am running from the build of master branch: MicroPython v1.9.4-674-g27ca9ab-dirty on 2018-11-12; PCA10040 with NRF52832

log from the central: Connection Alert: Timeout interrogating the peripheral

I set BLE_DRIVER_VERBOSE to 1 in file ble_drv.c and compile again. I put the last code in REPL and got log as below:

Is enabled status: 0 SoftDevice enable status: 0 IRQ enable status: 0 BLE_CONN_CFG_GAP status: 0 BLE_GAP_CFG_ROLE_COUNT status: 0 BLE_CONN_CFG_GATTS status: 0 BLE ram size: 10640 BLE enable status: 0 adding service Is enabled status: 0 Device name applied encoded uuid for service 0: 1a 18 ADV: uuid size: 2, type: 1, uuid: 181a, vs_idx: 0 Set Adv data size: 18

after using mobile phone to connect the board by bluetooth, log as below: GAP CONNECT GAP CONN PARAM UPDATE unhandled evt: 0x23 GAP DISCONNECT Is enabled status: 0 Then the board reboot

linycm commented 5 years ago

@glennrub, I recompiled with updated ARM 2018 compiler for MicroPython v1.9.4, then it works when runing ubluepy_temp.py and communicating with Android phone. But it does not work with IOS phone. Could you help to check whether is ok if we revise ubluepy_temp.py for IOS?

Thanks! Rdgs, Martin

glennrub commented 5 years ago

@linycm , sorry for taking so long. Thanks a lot for your log!

Based on the log i do see an unhandled event, 0x23. This translates into BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST.

I do not have any way of provoking your error using iOS, so i have made a patch that you could try out: https://github.com/glennrub/micropython/commit/4b57af5fdde3ec11aec6a0526133baaa9e89762e

Could you try out the patch and see if this solves the issue?

dhalbert commented 5 years ago

We saw the same error in the CircuitPython BLE code. iPhone 7 and later, and modern iPads apparently have extended-length BLE packet capability in the hardware. iPhone 6s and earlier don't. We just say we don't provide this: https://github.com/adafruit/circuitpython/blob/master/ports/nrf/common-hal/bleio/Peripheral.c#L269

linycm commented 5 years ago

@glennrub , I compiled the new version again. Try again but I cannot connect BLE in iphone7 yet.

silbo commented 5 years ago

@glennrub I tested it also on my nrf52840DK (PCA10056) board, it freezes when I try to connect to the device and the client device doesn't finish connecting to it (tested with Android, Mac and iOS) it seems to get stuck at discovering services