Skip to content

Commit 566e0c4

Browse files
committed
add ability to read a characteristic
1 parent d833bd8 commit 566e0c4

File tree

4 files changed

+84
-21
lines changed

4 files changed

+84
-21
lines changed

libs/bluetooth/jswrap_bluetooth.c

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,26 @@ Web Bluetooth-style GATT characteristic - get this using `BluetoothRemoteGATTSer
10461046
],
10471047
"return" : ["JsVar", "A Promise that is resolved (or rejected) when the characteristic is written" ]
10481048
}
1049+
1050+
Write a characteristic's value
1051+
1052+
```
1053+
var device;
1054+
NRF.connect(device_address).then(function(d) {
1055+
device = d;
1056+
return d.getPrimaryService("service_uuid);
1057+
}).then(function(s) {
1058+
console.log("Service ",s);
1059+
return s.getCharacteristic(characteristic_uuid);
1060+
}).then(function(c) {
1061+
return c.writeValue("Hello");
1062+
}).then(function(d) {
1063+
device.disconnect();
1064+
}).catch(function() {
1065+
console.log("Something's broken.");
1066+
});
1067+
```
1068+
10491069
**Note:** This is only available on some devices
10501070
*/
10511071
JsVar *jswrap_nrf_BluetoothRemoteGATTCharacteristic_writeValue(JsVar *characteristic, JsVar *data) {
@@ -1063,7 +1083,48 @@ JsVar *jswrap_nrf_BluetoothRemoteGATTCharacteristic_writeValue(JsVar *characteri
10631083
return 0;
10641084
#endif
10651085
}
1086+
/*JSON{
1087+
"type" : "method",
1088+
"class" : "BluetoothRemoteGATTCharacteristic",
1089+
"name" : "readValue",
1090+
"generate" : "jswrap_nrf_BluetoothRemoteGATTCharacteristic_readValue",
1091+
"return" : ["JsVar", "A Promise that is resolved (or rejected) with data when the characteristic is read" ]
1092+
}
1093+
1094+
Read a characteristic's value
1095+
1096+
```
1097+
var device;
1098+
NRF.connect(device_address).then(function(d) {
1099+
device = d;
1100+
return d.getPrimaryService("service_uuid);
1101+
}).then(function(s) {
1102+
console.log("Service ",s);
1103+
return s.getCharacteristic(characteristic_uuid);
1104+
}).then(function(c) {
1105+
return c.readValue();
1106+
}).then(function(d) {
1107+
console.log("Got:", JSON.stringify(d));
1108+
device.disconnect();
1109+
}).catch(function() {
1110+
console.log("Something's broken.");
1111+
});
1112+
```
1113+
1114+
**Note:** This is only available on some devices
1115+
*/
1116+
JsVar *jswrap_nrf_BluetoothRemoteGATTCharacteristic_readValue(JsVar *characteristic) {
1117+
#if CENTRAL_LINK_COUNT>0
1118+
if (!bleNewTask(BLETASK_CHARACTERISTIC_READ))
1119+
return 0;
10661120

1121+
jsble_central_characteristicRead(characteristic);
1122+
return jsvLockAgainSafe(blePromise);
1123+
#else
1124+
jsExceptionHere(JSET_ERROR, "Unimplemented");
1125+
return 0;
1126+
#endif
1127+
}
10671128

10681129
/* ---------------------------------------------------------------------
10691130
* TESTING
@@ -1096,25 +1157,6 @@ NRF.connect("d1:53:36:1a:7a:17").then(function(d) {
10961157
});
10971158
10981159
1099-
// getting just what we want
1100-
var device;
1101-
NRF.connect("d1:53:36:1a:7a:17").then(function(d) {
1102-
device = d;
1103-
console.log("Connected ",d);
1104-
return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); // UART
1105-
}).then(function(s) {
1106-
console.log("Service ",s);
1107-
return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e"); // RX
1108-
}).then(function(c) {
1109-
console.log("Characteristic ",c);
1110-
return c.writeValue("LED1.set()\n");
1111-
}).then(function() {
1112-
console.log("Written. Disconnecting");
1113-
device.disconnect();
1114-
}).catch(function() {
1115-
console.log("Oops");
1116-
});
1117-
11181160
// ------------------------------ on BLE server (microbit) - allow display of data
11191161
NRF.setServices({
11201162
0xBCDE : {

libs/bluetooth/jswrap_bluetooth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef enum {
2020
BLETASK_PRIMARYSERVICE, ///< Find primary service
2121
BLETASK_CHARACTERISTIC, ///< Find characteristics
2222
BLETASK_CHARACTERISTIC_WRITE, ///< Write to a characteristic
23+
BLETASK_CHARACTERISTIC_READ, ///< Read from a characteristic
2324
} BleTask;
2425

2526
bool bleInTask(BleTask task);
@@ -53,3 +54,4 @@ JsVar *jswrap_BluetoothRemoteGATTServer_getPrimaryServices(JsVar *parent);
5354
JsVar *jswrap_BluetoothRemoteGATTService_getCharacteristic(JsVar *parent, JsVar *characteristic);
5455
JsVar *jswrap_BluetoothRemoteGATTService_getCharacteristics(JsVar *parent);
5556
JsVar *jswrap_nrf_BluetoothRemoteGATTCharacteristic_writeValue(JsVar *characteristic, JsVar *data);
57+
JsVar *jswrap_nrf_BluetoothRemoteGATTCharacteristic_readValue(JsVar *characteristic);

targets/nrf5x/bluetooth.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void SWI1_IRQHandler(bool radio_evt) {
224224
static void on_ble_evt(ble_evt_t * p_ble_evt)
225225
{
226226
uint32_t err_code;
227-
// jsiConsolePrintf("\n[%d]\n", p_ble_evt->header.evt_id);
227+
//jsiConsolePrintf("\n[%d]\n", p_ble_evt->header.evt_id);
228228

229229
switch (p_ble_evt->header.evt_id) {
230230
case BLE_GAP_EVT_TIMEOUT:
@@ -517,6 +517,15 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
517517
case BLE_GATTC_EVT_DESC_DISC_RSP:
518518
jsiConsolePrintf("DESC\n");
519519
break;
520+
521+
case BLE_GATTC_EVT_READ_RSP: {
522+
ble_gattc_evt_read_rsp_t *p_read = &p_ble_evt->evt.gattc_evt.params.read_rsp;
523+
JsVar *data = jsvNewStringOfLength(p_read->len);
524+
if (data) jsvSetString(data, (char*)&p_read->data[0], p_read->len);
525+
bleCompleteTaskSuccess(BLETASK_CHARACTERISTIC_READ, data);
526+
jsvUnLock(data);
527+
break;
528+
}
520529
#endif
521530

522531
default:
@@ -1287,7 +1296,7 @@ void jsble_central_getCharacteristics(JsVar *service, ble_uuid_t uuid) {
12871296
}
12881297
}
12891298

1290-
void jsble_central_characteristicWrite(JsVar *characteristic, char *dataPtr, size_t dataLen) {
1299+
void jsble_central_characteristicWrite(JsVar *characteristic, char *dataPtr, size_t dataLen) {
12911300
const ble_gattc_write_params_t write_params = {
12921301
.write_op = BLE_GATT_OP_WRITE_CMD,
12931302
.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE,
@@ -1301,4 +1310,12 @@ void jsble_central_getCharacteristics(JsVar *service, ble_uuid_t uuid) {
13011310
if (jsble_check_error(err_code))
13021311
bleCompleteTaskFail(BLETASK_CHARACTERISTIC_WRITE, 0);
13031312
}
1313+
1314+
void jsble_central_characteristicRead(JsVar *characteristic) {
1315+
uint16_t handle = jsvGetIntegerAndUnLock(jsvObjectGetChild(characteristic, "handle_value", 0));
1316+
uint32_t err_code;
1317+
err_code = sd_ble_gattc_read(m_central_conn_handle, handle, 0/*offset*/);
1318+
if (jsble_check_error(err_code))
1319+
bleCompleteTaskFail(BLETASK_CHARACTERISTIC_READ, 0);
1320+
}
13041321
#endif

targets/nrf5x/bluetooth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,6 @@ void jsble_central_getPrimaryServices(ble_uuid_t uuid);
116116
void jsble_central_getCharacteristics(JsVar *service, ble_uuid_t uuid);
117117
// Write data to the given characteristic. When done call bleCompleteTask
118118
void jsble_central_characteristicWrite(JsVar *characteristic, char *dataPtr, size_t dataLen);
119+
// Read data from the given characteristic. When done call bleCompleteTask
120+
void jsble_central_characteristicRead(JsVar *characteristic);
119121
#endif

0 commit comments

Comments
 (0)