Skip to content

Commit cbaa0ee

Browse files
committed
Adapt writeQ to wait for end of write operations on iOS and better performance all around
fixes #617
1 parent 27bf1a5 commit cbaa0ee

File tree

5 files changed

+174
-99
lines changed

5 files changed

+174
-99
lines changed

readme.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ This plugin allows you to interact with Bluetooth LE devices on Android, iOS, an
9696

9797
* Cordova 5.0.0 or higher
9898
* Android 4.3 or higher, Android Cordova library 5.0.0 or higher, target Android API 23 or higher
99-
* iOS 7 or higher
99+
* iOS 10 or higher
100100
* Windows Phone 8.1 (Tested on Nokia Lumia 630)
101101
* Windows 10 UWP
102102
* Device hardware must be certified for Bluetooth LE. i.e. Nexus 7 (2012) doesn't support Bluetooth LE even after upgrading to 4.3 (or higher) without a modification
@@ -1281,19 +1281,25 @@ var string = bluetoothle.bytesToString(bytes); //This should equal Write Hello W
12811281

12821282

12831283
### writeQ ###
1284-
Write Quick / Queue, use this method to quickly execute write without response commands when writing more than 20 bytes at a time. The data will automatically be split up into 20 bytes packets. On iOS, these packets are written immediately since iOS uses queues. You probably won't see much of a performance increase using writeQ. On Android, a queue isn't used internally. Instead another call shouldn't be made until onCharacteristicWrite is called. This could be done at the Javascript layer, but the Javascript to plugin "bridge" must be crossed twice, which leads to some significant slow downs when milliseconds make a difference. For even better write throughput, use requestConnectionPriority('high') as well. Note, no callback will occur on write without response on iOS.
1284+
Write Quick / Queue, use this method to quickly execute write without response commands when writing more than 20 bytes at a time. The data will automatically be split up into 20 bytes packets by default or you can increase that by setting `chunkSize`. On iOS, these packets are written immediately since iOS uses queues. You probably won't see much of a performance increase using writeQ unless you use `type="noResponse"` and set `chunkSize` higher than 20. On Android, a queue isn't used internally. Instead another call shouldn't be made until onCharacteristicWrite is called. This could be done at the Javascript layer, but the Javascript to plugin "bridge" must be crossed twice, which leads to some significant slow downs when milliseconds make a difference. For even better write throughput, use requestConnectionPriority('high') and mtu(SAME_VALUE_AS_CHUNK_SIZE_PARAM) as well.
12851285

12861286
Warnings
12871287
* This is experimental. Test heavily before using in any production code.
1288-
* iOS won't see much performance gain, but Android should.
1288+
* To see a performance gain you should use this in combination with requestConnectionPriority('high') and mtu(`MTU_VALUE`) and then calling this method with `type="noResponse"` and set `chunkSize` to `MTU_VALUE`.
1289+
* Only supported on iOS11+.
12891290
* Only supports one call at a time. Don't execute back to back, use on multiple devices, or multiple characteristics.
12901291

12911292
```javascript
12921293
bluetoothle.writeQ(writeSuccess, writeError, params);
12931294
```
12941295

12951296
##### Params #####
1296-
See write() above.
1297+
* address = The address/identifier provided by the scan's return object
1298+
* service = The service's UUID
1299+
* characteristic = The characteristic's UUID
1300+
* value = Base64 encoded string
1301+
* type = Set to "noResponse" to enable write without response, all other values will write normally.
1302+
* chunkSize = Define the size of packets. This should be according to MTU value
12971303

12981304
##### Success #####
12991305
See write() above.

src/android/BluetoothLePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ private void writeQAction(JSONArray args, CallbackContext callbackContext) {
21112111
queueQuick.clear();
21122112

21132113
int length = value.length;
2114-
int chunkSize = 20;
2114+
int chunkSize = obj.optInt("chunkSize", 20);
21152115
int offset = 0;
21162116

21172117
do {

src/ios/BluetoothLePlugin.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
int requestId;
1616
NSMutableDictionary* requestsHash;
1717
NSMutableDictionary* servicesHash;
18+
19+
BOOL writeQIsRunning;
20+
int writeQtype;
21+
NSInteger writeQLocation;
22+
NSInteger writeQLength;
23+
NSInteger writeQChunkSize;
24+
NSData *writeQData;
25+
CBCharacteristic *currentWriteCharacteristic;
1826
}
1927

2028
- (void)initialize:(CDVInvokedUrlCommand *)command;

0 commit comments

Comments
 (0)