ukBaz / python-bluezero

A simple Python interface to Bluez
MIT License
395 stars 112 forks source link

Is there anyway that I can modify Generic Access Profile? #338

Closed denny61302 closed 3 years ago

denny61302 commented 3 years ago

I am trying to create a customer service with a notification called back.

Currently, I have to use my phone to enable the notification callback.

I also built another BLE device using a Cypress chip. In this chip, I can set up the Peripheral Preferred Connection Parameters [R] (0x2A04) in the Generic Access (0x1800) service.

The problem I faced is that the Cypress BLE will call the notification automatically once the connection is built, but the same service built on the RPI 3B+ is not. I have to enable the notification through a mobile app (NRFconnect)

I am not sure what casues this issue, so I record log files and saw the only difference is the setting of the Peripheral Preferred Connection Parameters [R] (0x2A04) in the Generic Access (0x1800) service.

The followings are the log files from NRFconnect

The correct one from the Cypress BLE chip

nRF Connect, 2021-06-15 Austofix ezyaim 2.0 (00:A0:50:00:00:01) V 15:31:33.280 Connecting to 00:A0:50:00:00:01... D 15:31:33.281 gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, preferred PHY = LE 1M) D 15:31:33.353 [Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED D 15:31:33.377 [Callback] Connection state changed with status: 0 and new state: CONNECTED (2) I 15:31:33.377 Connected to 00:A0:50:00:00:01 V 15:31:33.396 Discovering services... D 15:31:33.396 gatt.discoverServices() I 15:31:33.843 Connection parameters updated (interval: 7.5ms, latency: 0, timeout: 5000ms) D 15:31:34.141 [Callback] Services discovered with status: 0 I 15:31:34.141 Services discovered V 15:31:34.156 Generic Access (0x1800)

The wrong one from the RPI 3B+

nRF Connect, 2021-06-15 Austofix ezyaim 2.0 (B8:27:EB:84:55:D3) V 15:46:39.293 Connecting to B8:27:EB:84:55:D3... D 15:46:39.293 gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, preferred PHY = LE 1M) D 15:46:39.966 [Callback] Connection state changed with status: 0 and new state: CONNECTED (2) I 15:46:39.966 Connected to B8:27:EB:84:55:D3 V 15:46:40.007 Discovering services... D 15:46:40.007 gatt.discoverServices() D 15:46:40.009 [Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED I 15:46:40.727 Connection parameters updated (interval: 7.5ms, latency: 0, timeout: 5000ms) D 15:46:40.947 [Callback] Services discovered with status: 0 I 15:46:40.947 Services discovered V 15:46:40.966 Generic Access (0x1800)

Is there anyone faces the same issuse?

ukBaz commented 3 years ago

This is an issue I've been looking at for a while in #321

I have asked the question on the BlueZ slack channel but not received a actionable answer. Your data is interesting because it is not something I was aware of previously. Do you have a reference with more information about Peripheral Preferred Connection Parameters [R] (0x2A04)?

I've looked on bluetooth.com and found the value in the list of 16-bit UUIDs but I can't find mention of it in the GATT Specification Supplement

Looking in the BlueZ source code there seems to be some hardcoded numbers associated with it: https://github.com/bluez/bluez/blob/7c3ca2a6b940d36c553fabe38066fabc66530dc9/unit/test-gatt.c#L1685-L1687

So in summary, I think the issue might be an underlying one with BlueZ but if you can share more information about the characteristic we might be able to work something out.

denny61302 commented 3 years ago

Thanks for the reply.

I did find the GAP and Peripheral Preferred Connection Parameters UUIDs are defined in the BlueZ source code.

https://github.com/bluez/bluez/blob/7c3ca2a6b940d36c553fabe38066fabc66530dc9/lib/uuid.h#L82

This snapshot is the Peripheral Preferred Connection Parameters setting of my Cypress chip.

image

I have double-checked my log file, showing that it contains 8 bytes of data.

Hope this will help.

ukBaz commented 3 years ago

Thanks for sharing that. There is nothing in that screenshot that suggests it is controlling the enablement of a notification callback. Could getting Peripheral Preferred Connection Parameters to appear to be a separate issue getting a custom characteristics to start with notifications enabled by default?

denny61302 commented 3 years ago

Agreed. Just like you said there is no enablement of a notification callback in Peripheral Preferred Connection Parameters.

BTW, my application is based on the cpu_temperature example.

I also tried to set notification to True when I created the characteristic, but there is no effect so far.

Here is the snippet of my code. Did I miss something in here?

ble.add_characteristic(srv_id=1, chr_id=2, uuid='0000FFF2-0000-1000-8000-00805F9B34FB',
                                   value=[], **notifying=True**,
                                   flags=['notify'],
                                   read_callback=None,
                                   write_callback=None,
                                   notify_callback=notify_callback
                                   )

In the beginning, I was trying to use ble_uart example, but I was not sure how can I send values through uart notification.

ukBaz commented 3 years ago

I also tried to set notification to True when I created the characteristic, but there is no effect so far. Here is the snippet of my code. Did I miss something in here?

Nope! That is exactly the same problem as we are working on. I'm going to close this issue as it sounds like a duplication of #321 because this is about activating notifications by default. If I have misunderstood then let me know and I'll reopen.