BLE event indicating client has changed characteristic value

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

BLE event indicating client has changed characteristic value

Postby rwel59 » Fri Dec 08, 2017 6:59 pm

I'm writing a ble server app where the primary communications role is sending data to a connected device (phone). However, there is some communication from the connected device back to the esp server.

I was planning on having the app update a server characteristic which will then be read by the eps32 but I cannot find an event flag that will indicate to the server that the client has updated the value. Is there something available for this? If not, what is the best method to perform this 2-way communication?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE event indicating client has changed characteristic value

Postby kolban » Sat Dec 09, 2017 6:12 am

In your story, is the ESP32 the BLE Client or the BLE Server?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: BLE event indicating client has changed characteristic value

Postby rwel59 » Sat Dec 09, 2017 12:29 pm

I've configured the device as a BLE server - my rationale is that the majority of communications will be outbound data from the device.

Inbound data is primarily for device configuration.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE event indicating client has changed characteristic value

Postby kolban » Sat Dec 09, 2017 2:00 pm

Imagine you have two BLE devices ... lets call them A and B. Let us now assume that device A is being a BLE server and device B would then be a BLE client.
Device A (the server) ... starts up and starts advertising. It now passively sits there waiting for a client to explicitly connect with it. Device B, when it starts up, would scan the environment looks for suitable advertisers. Upon finding device A, it may choose to connect with it. At this point B is a client of A and A is the server to B. Device B (the client) can now send a request to the server (A) to read data from it ... or Device B (the client) can now send a request to the server (A) to ask it to change data on it (write). Think of Device A (the server) as now being a "database" of values where each value is called a "characteristic".

In normal operation, you should think of Device A as a server of requests initiated by clients.

Device A is hosting values of data. These can set by clients or they can be re-calculated by the server itself over time (for example, sensor readings). These new values become available to device B (the client) anytime device B chooses to read them. There is one more possibility ... and that is device B can ask Device A to notify it when data changes. In that scenario, when ever data changes at the database (Device A, the server), a notification is sent to device B to tell it explicitly that something in the data has changed.

And that's the high level of the story. The parts of a BLE communication are not symmetric. One will be a server, one will be a client. Not that doesn't preclude either device being BOTH a server and a client at the same time ... but for the purposes of a single communication between them ... one is a client ... one is a server.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: BLE event indicating client has changed characteristic value

Postby rwel59 » Sat Dec 09, 2017 2:26 pm

yep, that is consistent with where I'm at.

So, my server (the esp32) is the primary changer of data which the client (in this case, the phone) gets notified of changes. That is all working well.

I do need to set up the ability for the phone to occasionally send data to my device and herein lies my question. I believe that the client can change the value of a characteristic (pretty sure I did that a couple of days ago). What I'm looking for is an event that signals the server that the client has made this change.

If there is no such event I can either monitor the characteristic for changes or I can look into setting up another service where the esp32 device also acts as a client.

UPDATE: just found your documentation in esp32-snippets explaining the characteristic callback function. Working on it now but it looks like it addresses my issue.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE event indicating client has changed characteristic value

Postby kolban » Sat Dec 09, 2017 5:26 pm

Almost by definition, a BLE Server (the ESP32 in your story) is a custom piece of software. As best I understand, there is no industry API that has been standardized so how one writes BLE Server code is a function of which library you are using. At the BLE lowest level, the BLE client sends a radio packet that contains a protocol that says "Here is a requested new value for a characteristic" and it is up to the server implementation on how to handle that.

I wrote some BLE C++ wrappers ... in that logic, when you create a BLE Server, you create a BLEService and associate the BLEServer with one or more BLECharacteristic objects and then start the server advertising and listening.
At this point, your ESP32 will be "managing" the values of the characteristic. However, should you need to specific work when either a client sets a new characteristic value or requests to read the current characteristic, you can create an instance of the BLECharacteristicCallback class and register that with the associated BLECharacteristic you are working with. Now when a client performs a write of a new value, the onWrite() method of the callback is invoked. If the client attempts to read the value, the onRead() callback is invoked.

This appears to be the most flexible solution ... however if anyone wishes or can think of an alternative, post back.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: Google [Bot] and 82 guests