summaryrefslogtreecommitdiff
path: root/examples/bluetooth/ble_temperature.py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-07-20 16:58:10 +1000
committerDamien George <damien@micropython.org>2020-07-20 23:26:41 +1000
commit9d823a5d9a6730edde8e1df1e5ff4add1ad17094 (patch)
treeebad885e38b5d7edde4ad0cd16d7eb5447890df1 /examples/bluetooth/ble_temperature.py
parent3c7ca2004c78ec386e136b947ed5e05a39b61aaf (diff)
extmod/modbluetooth: Add event for "indicate acknowledgement".
This commit adds the IRQ_GATTS_INDICATE_DONE BLE event which will be raised with the status of gatts_indicate (unlike notify, indications require acknowledgement). An example of its use is added to ble_temperature.py, and to the multitests in ble_characteristic.py. Implemented for btstack and nimble bindings, tested in both directions between unix/btstack and pybd/nimble.
Diffstat (limited to 'examples/bluetooth/ble_temperature.py')
-rw-r--r--examples/bluetooth/ble_temperature.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/examples/bluetooth/ble_temperature.py b/examples/bluetooth/ble_temperature.py
index fd24e74d7..0e2da2239 100644
--- a/examples/bluetooth/ble_temperature.py
+++ b/examples/bluetooth/ble_temperature.py
@@ -13,13 +13,14 @@ from micropython import const
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
+_IRQ_GATTS_INDICATE_DONE = const(20)
# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
# org.bluetooth.characteristic.temperature
_TEMP_CHAR = (
bluetooth.UUID(0x2A6E),
- bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
+ bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY | bluetooth.FLAG_INDICATE,
)
_ENV_SENSE_SERVICE = (
_ENV_SENSE_UUID,
@@ -52,15 +53,21 @@ class BLETemperature:
self._connections.remove(conn_handle)
# Start advertising again to allow a new connection.
self._advertise()
+ elif event == _IRQ_GATTS_INDICATE_DONE:
+ conn_handle, value_handle, status, = data
- def set_temperature(self, temp_deg_c, notify=False):
+ def set_temperature(self, temp_deg_c, notify=False, indicate=False):
# Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius.
# Write the local value, ready for a central to read.
self._ble.gatts_write(self._handle, struct.pack("<h", int(temp_deg_c * 100)))
- if notify:
+ if notify or indicate:
for conn_handle in self._connections:
- # Notify connected centrals to issue a read.
- self._ble.gatts_notify(conn_handle, self._handle)
+ if notify:
+ # Notify connected centrals.
+ self._ble.gatts_notify(conn_handle, self._handle)
+ if indicate:
+ # Indicate connected centrals.
+ self._ble.gatts_indicate(conn_handle, self._handle)
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
@@ -76,7 +83,7 @@ def demo():
while True:
# Write every second, notify every 10 seconds.
i = (i + 1) % 10
- temp.set_temperature(t, notify=i == 0)
+ temp.set_temperature(t, notify=i == 0, indicate=False)
# Random walk the temperature.
t += random.uniform(-0.5, 0.5)
time.sleep_ms(1000)