Skip to content

Commit 9a2b624

Browse files
Refactored return values to ArrayBuffer
1 parent 64d6576 commit 9a2b624

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ The promise will receive an object like this:
201201

202202
```js
203203
{
204-
value: <72>, // the platform-specific binary value of the characteristic
205-
valueDecoded: 65, // the plugin's best effort of auto-decoding the value (for testing purposes mostly)
204+
value: <ArrayBuffer>, // an ArrayBuffer which you can use to decode (see example below)
205+
valueRaw: <72>, // the platform-specific binary value of the characteristic: NSData (iOS), byte[] (Android)
206206
characteristicUUID: '434234-234234-234234-434'
207207
}
208208
```
@@ -215,7 +215,9 @@ bluetooth.read({
215215
serviceUUID: '180d',
216216
characteristicUUID: '3434-45234-34324-2343'
217217
}).then(function(result) {
218-
console.log("read: " + JSON.stringify(result));
218+
// fi. a heartrate monitor value (Uint8) can be retrieved like this:
219+
var data = new Uint8Array(result.value);
220+
console.log("Your heartrate is: " + data[1] + " bpm");
219221
}).then(function(err) {
220222
console.log("read error: " + err);
221223
});
@@ -253,6 +255,7 @@ bluetooth.startNotifying({
253255
serviceUUID: '180d',
254256
characteristicUUID: '3434-45234-34324-2343',
255257
onNotify: function (result) {
258+
// see the read example for how to decode ArrayBuffers
256259
console.log("read: " + JSON.stringify(result));
257260
}
258261
}).then(function() {
@@ -275,8 +278,12 @@ bluetooth.stopNotifying({
275278
});
276279
```
277280

281+
## Changelog
282+
* 1.1.0 To be compatible with any Bluetooth device out there, the value returned from `read` and `notify` is now an `ArrayBuffer`.
283+
* 1.0.0 Initial release
284+
278285
## Future work
279-
* Find a more convenient way (and document it) to read/write values.
286+
* Find an even better way to write values.
280287
* Support other properties of a characteristic.
281288
* Report advertising data peripherals broadcast.
282-
* Support interacting with multiple characteristics of the same peripheral at the same time.
289+
* Support interacting with multiple characteristics of the same peripheral at the same time.

bluetooth-common.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('./base64');
2+
13
var Bluetooth = {};
24

35
Bluetooth.requestCoarseLocationPermission = function () {
@@ -12,4 +14,15 @@ Bluetooth.hasCoarseLocationPermission = function () {
1214
});
1315
};
1416

17+
Bluetooth._base64ToArrayBuffer = function (b64) {
18+
var stringToArrayBuffer = function(str) {
19+
var ret = new Uint8Array(str.length);
20+
for (var i = 0; i < str.length; i++) {
21+
ret[i] = str.charCodeAt(i);
22+
}
23+
return ret.buffer;
24+
};
25+
return stringToArrayBuffer(atob(b64));
26+
};
27+
1528
module.exports = Bluetooth;

bluetooth.android.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var utils = require("utils/utils");
22
var application = require("application");
33
var Bluetooth = require("./bluetooth-common");
4+
45
var ACCESS_COARSE_LOCATION_PERMISSION_REQUEST_CODE = 222;
56

67
var adapter,
@@ -131,14 +132,14 @@ Bluetooth._MyGattCallback = android.bluetooth.BluetoothGattCallback.extend({
131132
* 3: disconnecting
132133
*/
133134
onConnectionStateChange: function(bluetoothGatt, status, newState) {
134-
console.log("------- _MyGattCallback.onConnectionStateChange, status: " + status);
135-
console.log("------- _MyGattCallback.onConnectionStateChange, newState: " + newState);
135+
console.log("------- _MyGattCallback.onConnectionStateChange, status: " + status + ", new state: " + newState);
136136

137137
// https://github.com/don/cordova-plugin-ble-central/blob/master/src/android/Peripheral.java#L191
138138
if (newState == 2 /* connected */ && status === 0 /* gatt success */) {
139139
console.log("---- discovering services..");
140140
bluetoothGatt.discoverServices();
141141
} else {
142+
// perhaps the device was manually disconnected, or in use by another device
142143
Bluetooth._disconnect(bluetoothGatt);
143144
}
144145
},
@@ -234,14 +235,10 @@ Bluetooth._MyGattCallback = android.bluetooth.BluetoothGattCallback.extend({
234235
if (value === null) {
235236
return null;
236237
}
237-
// TODO may be Uint16Array as well
238-
var data = new Uint8Array(value);
239-
for (var d in data) {
240-
if (data[d] !== undefined) {
241-
return data[d];
242-
}
243-
}
244-
return value;
238+
239+
// value is of Java type: byte[]
240+
var b = android.util.Base64.encodeToString(value, android.util.Base64.NO_WRAP);
241+
return Bluetooth._base64ToArrayBuffer(b);
245242
},
246243

247244
onCharacteristicRead: function(bluetoothGatt, bluetoothGattCharacteristic, status) {
@@ -257,8 +254,8 @@ Bluetooth._MyGattCallback = android.bluetooth.BluetoothGattCallback.extend({
257254
if (stateObject.onReadPromise) {
258255
var value = bluetoothGattCharacteristic.getValue();
259256
stateObject.onReadPromise({
260-
value: value,
261-
valueDecoded: this._decodeValue(value),
257+
valueRaw: value,
258+
value: this._decodeValue(value),
262259
characteristicUUID: bluetoothGattCharacteristic.getUuid()
263260
});
264261
}
@@ -276,11 +273,9 @@ Bluetooth._MyGattCallback = android.bluetooth.BluetoothGattCallback.extend({
276273

277274
if (stateObject.onNotifyCallback) {
278275
var value = bluetoothGattCharacteristic.getValue();
279-
var data = new Uint8Array(value);
280-
console.log("------- _MyGattCallback.onCharacteristicChanged, decodedvalue: " + data[1]);
281276
stateObject.onNotifyCallback({
282-
value: value,
283-
valueDecoded: data[1],
277+
valueRaw: value,
278+
value: this._decodeValue(value),
284279
characteristicUUID: bluetoothGattCharacteristic.getUuid()
285280
});
286281
}

bluetooth.ios.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var Bluetooth = require("./bluetooth-common");
2-
require('./base64');
32

43
Bluetooth._state = {
54
manager: null,
@@ -120,23 +119,14 @@ var CBPeripheralDelegateImpl = (function (_super) {
120119
}
121120
};
122121

123-
CBPeripheralDelegateImpl.prototype._decodeValue = function(value) {
122+
CBPeripheralDelegateImpl.prototype._toArrayBuffer = function(value) {
124123
if (value === null) {
125124
return null;
126125
}
127-
var v = atob(value.base64EncodedStringWithOptions(0));
128-
var l = v.length;
129-
var ret = new Uint8Array(l);
130-
for (var i = 0; i < l; i++) {
131-
ret[i] = v.charCodeAt(i);
132-
}
133-
var data = new Uint8Array(ret.buffer);
134-
for (var d in data) {
135-
if (data[d] !== undefined) {
136-
return data[d];
137-
}
138-
}
139-
return value;
126+
127+
// value is of ObjC type: NSData
128+
var b = value.base64EncodedStringWithOptions(0);
129+
return Bluetooth._base64ToArrayBuffer(b);
140130
};
141131

142132
// this is called when a value is read from a peripheral
@@ -152,17 +142,13 @@ var CBPeripheralDelegateImpl = (function (_super) {
152142
return;
153143
}
154144

155-
var value = characteristic.value;
156-
var valueDecoded = this._decodeValue(value);
157-
console.log("value received from peripheral: " + value + ", decoded: " + valueDecoded);
158-
159145
var result = {
160146
type: characteristic.isNotifying ? "notification" : "read",
161147
characteristicUUID: characteristic.UUID.UUIDString,
162-
value: value,
163-
valueDecoded: valueDecoded
148+
valueRaw: characteristic.value,
149+
value: this._toArrayBuffer(characteristic.value)
164150
};
165-
151+
166152
if (result.type === "read") {
167153
if (this._onReadPromise) {
168154
this._onReadPromise(result);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-bluetooth",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description" : "Connect to and interact with Bluetooth LE peripherals",
55
"main" : "bluetooth.js",
66
"nativescript": {

0 commit comments

Comments
 (0)