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

Add indication support for GATT Characteristics #935

Merged
merged 1 commit into from
Oct 4, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions libs/bluetooth/jswrap_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@ NRF.setServices({
readable : true, // optional, default is false
writable : true, // optional, default is false
notify : true, // optional, default is false
indicate : true, // optional, default is false
onWrite : function(evt) { // optional
console.log("Got ", evt.data);
}
Expand Down Expand Up @@ -1437,6 +1438,8 @@ void jswrap_nrf_bluetooth_setServices(JsVar *data) {
char_md.char_props.broadcast = 1;
if (jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "notify", 0)))
char_md.char_props.notify = 1;
if (jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "indicate", 0)))
char_md.char_props.indicate = 1;
if (jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "readable", 0)))
char_md.char_props.read = 1;
if (jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "writable", 0))) {
Expand Down Expand Up @@ -1550,7 +1553,21 @@ NRF.updateServices({
}
});
```
This only works if the characteristic was create with `notify: true` using `setServices`,
This only works if the characteristic was created with `notify: true` using `setServices`,
otherwise the characteristic will be updated but no notification will be sent.

To indicate (i.e. notify with ACK) connected clients of a change to the '0xABCD' characteristic in the '0xBCDE' service:
```
NRF.updateServices({
0xBCDE : {
0xABCD : {
value : "World",
indicate: true
}
}
});
```
This only works if the characteristic was created with `indicate: true` using `setServices`,
otherwise the characteristic will be updated but no notification will be sent.

**Note:** See `setServices` for more information
Expand Down Expand Up @@ -1591,6 +1608,7 @@ void jswrap_nrf_bluetooth_updateServices(JsVar *data) {
JsVar *charValue = jsvObjectGetChild(charVar, "value", 0);

bool notification_requested = jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "notify", 0));
bool indication_requested = jsvGetBoolAndUnLock(jsvObjectGetChild(charVar, "indicate", 0));

if (charValue) {
JSV_GET_AS_CHAR_ARRAY(vPtr, vLen, charValue);
Expand Down Expand Up @@ -1631,11 +1649,11 @@ void jswrap_nrf_bluetooth_updateServices(JsVar *data) {
APP_ERROR_CHECK(err_code);
}

// Notify connected clients if necessary
if (notification_requested && (m_conn_handle != BLE_CONN_HANDLE_INVALID)) {
// Notify/indicate connected clients if necessary
if ((notification_requested || indication_requested) && (m_conn_handle != BLE_CONN_HANDLE_INVALID)) {
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = char_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.type = indication_requested ? BLE_GATT_HVX_INDICATION : BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_len = &len;
hvx_params.p_data = p_value;
Expand Down