@psdk/device-ble-taro 0.7.15 → 0.7.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,26 @@
1
1
  import { ConnectedDevice, ConnectionState, ReadOptions } from "@psdk/frame-father";
2
2
  import Taro from "@tarojs/taro";
3
+ import { BleFlowControlOptions } from "@psdk/device-bluetooth-traits";
3
4
  export declare class TaroBleConnectedDevice implements ConnectedDevice {
4
5
  private device;
5
6
  private scpair;
7
+ private flowControlWriter?;
6
8
  private _connectionState;
7
9
  private dataOfRead;
8
10
  private notifyCallback?;
11
+ private notifyReady;
9
12
  constructor(options: {
10
13
  device: Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice;
11
14
  scpair: ServiceCharacteristicPair;
15
+ flowControl?: BleFlowControlOptions;
12
16
  });
13
17
  notify(callback: (value: Uint8Array) => Promise<void>): void;
14
18
  private doListen;
19
+ private enableNotify;
20
+ private createFlowControlWriter;
21
+ private notifyFlowControl;
22
+ private isSameCharacteristicUuid;
23
+ private writeChunk;
15
24
  private realRead;
16
25
  origin(): Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice;
17
26
  connectionState(): ConnectionState;
@@ -26,5 +35,6 @@ export interface ServiceCharacteristicPair {
26
35
  service: Taro.getBLEDeviceServices.BLEService;
27
36
  write: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
28
37
  read?: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
38
+ flowControl?: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
29
39
  }
30
40
  //# sourceMappingURL=connected.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"connected.d.ts","sourceRoot":"","sources":["../src/connected.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,eAAe,EAAE,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAEjF,OAAO,IAAI,MAAM,cAAc,CAAC;AAEhC,qBAAa,sBAAuB,YAAW,eAAe;I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,EAAE,IAAI,CAAC,2BAA2B,CAAC,iBAAiB,CAAC;CAClE"}
@@ -7,27 +7,43 @@ exports.TaroBleConnectedDevice = void 0;
7
7
  const frame_father_1 = require("@psdk/frame-father");
8
8
  const await_timeout_1 = __importDefault(require("await-timeout"));
9
9
  const taro_1 = __importDefault(require("@tarojs/taro"));
10
+ const device_bluetooth_traits_1 = require("@psdk/device-bluetooth-traits");
10
11
  class TaroBleConnectedDevice {
11
12
  device;
12
13
  scpair;
14
+ flowControlWriter;
13
15
  _connectionState;
14
16
  dataOfRead;
15
17
  notifyCallback;
18
+ notifyReady;
16
19
  constructor(options) {
17
20
  this.device = options.device;
18
21
  this.scpair = options.scpair;
19
22
  this._connectionState = frame_father_1.ConnectionState.CONNECTED;
20
23
  this.dataOfRead = [];
21
- this.doListen();
24
+ this.flowControlWriter = this.createFlowControlWriter(options.flowControl);
25
+ this.notifyReady = this.doListen();
22
26
  }
23
27
  notify(callback) {
24
28
  this.notifyCallback = callback;
25
29
  }
26
- doListen() {
27
- if (this.scpair.read == null)
28
- return;
30
+ async doListen() {
29
31
  taro_1.default.onBLECharacteristicValueChange(async (value) => {
30
32
  let data = new Uint8Array(value.value);
33
+ if (this.flowControlWriter &&
34
+ this.scpair.flowControl &&
35
+ value.characteristicId &&
36
+ this.isSameCharacteristicUuid(value.characteristicId, this.scpair.flowControl.uuid)) {
37
+ this.notifyFlowControl(data);
38
+ return;
39
+ }
40
+ if (!this.scpair.read) {
41
+ return;
42
+ }
43
+ if (value.characteristicId &&
44
+ !this.isSameCharacteristicUuid(value.characteristicId, this.scpair.read.uuid)) {
45
+ return;
46
+ }
31
47
  if (this.notifyCallback) {
32
48
  await this.notifyCallback(data);
33
49
  }
@@ -36,14 +52,62 @@ class TaroBleConnectedDevice {
36
52
  taro_1.default.onBLEConnectionStateChange(value => {
37
53
  this._connectionState = value.connected ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
38
54
  });
39
- taro_1.default.notifyBLECharacteristicValueChange({
40
- deviceId: this.device.deviceId,
41
- serviceId: this.scpair.service.uuid,
42
- characteristicId: this.scpair.read.uuid,
43
- state: true,
44
- success: result => console.log(result),
45
- // @ts-ignore
46
- fail: (e) => console.error(e),
55
+ if (this.scpair.read != null) {
56
+ await this.enableNotify(this.scpair.read.uuid);
57
+ }
58
+ if (this.scpair.flowControl != null) {
59
+ await this.enableNotify(this.scpair.flowControl.uuid);
60
+ }
61
+ }
62
+ async enableNotify(characteristicId) {
63
+ return new Promise((resolve, reject) => {
64
+ taro_1.default.notifyBLECharacteristicValueChange({
65
+ deviceId: this.device.deviceId,
66
+ serviceId: this.scpair.service.uuid,
67
+ characteristicId,
68
+ state: true,
69
+ success: result => {
70
+ console.log(result);
71
+ resolve();
72
+ },
73
+ // @ts-ignore
74
+ fail: (e) => reject(e),
75
+ });
76
+ });
77
+ }
78
+ createFlowControlWriter(options) {
79
+ if (!this.scpair.flowControl || !(options?.enabled ?? false)) {
80
+ return undefined;
81
+ }
82
+ return new device_bluetooth_traits_1.BleFlowControlWriter({
83
+ ...device_bluetooth_traits_1.DEFAULT_BLE_FLOW_CONTROL_OPTIONS,
84
+ ...options,
85
+ });
86
+ }
87
+ notifyFlowControl(data) {
88
+ this.flowControlWriter.onNotify(data);
89
+ }
90
+ isSameCharacteristicUuid(left, right) {
91
+ return left.replace(/-/g, '').toLowerCase() == right.replace(/-/g, '').toLowerCase();
92
+ }
93
+ async writeChunk(data) {
94
+ return new Promise((resolve, reject) => {
95
+ taro_1.default.writeBLECharacteristicValue({
96
+ deviceId: this.device.deviceId,
97
+ serviceId: this.scpair.service.uuid,
98
+ characteristicId: this.scpair.write.uuid,
99
+ // @ts-ignore
100
+ value: data.buffer,
101
+ success: result => {
102
+ if (result.errMsg.indexOf('ok') == -1) {
103
+ reject(result.errMsg);
104
+ return;
105
+ }
106
+ resolve();
107
+ },
108
+ // @ts-ignore
109
+ fail: (e) => reject(e)
110
+ });
47
111
  });
48
112
  }
49
113
  async realRead() {
@@ -116,24 +180,11 @@ class TaroBleConnectedDevice {
116
180
  }
117
181
  }
118
182
  async write(data) {
119
- return new Promise((resolve, reject) => {
120
- taro_1.default.writeBLECharacteristicValue({
121
- deviceId: this.device.deviceId,
122
- serviceId: this.scpair.service.uuid,
123
- characteristicId: this.scpair.write.uuid,
124
- // @ts-ignore
125
- value: data.buffer,
126
- success: result => {
127
- if (result.errMsg.indexOf('ok') == -1) {
128
- reject(result.errMsg);
129
- return;
130
- }
131
- resolve();
132
- },
133
- // @ts-ignore
134
- fail: (e) => reject(e)
135
- });
136
- });
183
+ if (this.flowControlWriter) {
184
+ await this.notifyReady;
185
+ return this.flowControlWriter.write(data, { writeChunk: chunk => this.writeChunk(chunk) });
186
+ }
187
+ return this.writeChunk(data);
137
188
  }
138
189
  flush() {
139
190
  this.dataOfRead.splice(0, this.dataOfRead.length);
@@ -18,6 +18,7 @@ export declare class TaroBleBluetooth extends Jluetooth<Taro.onBluetoothDeviceFo
18
18
  private extractMacFromAdvertisData;
19
19
  private formatMacAddress;
20
20
  private createBLEConnection;
21
+ private setBLEMTU;
21
22
  private getBLEDeviceServices;
22
23
  private getBLEDeviceCharacteristics;
23
24
  private requestLocation;
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,cAAc,EACd,SAAS,EACT,eAAe,EACf,eAAe,EAEhB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAC,eAAe,EAAkB,MAAM,oBAAoB,CAAC;AAGpE,OAAO,IAAI,MAAM,cAAc,CAAC;AAEhC,qBAAa,gBAAiB,SAAQ,SAAS,CAAC,IAAI,CAAC,sBAAsB,CAAC,6BAA6B,CAAC;IAExG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAkB;IAG3C,OAAO,CAAC,kBAAkB,CAAC,CAAgH;IAC3I,OAAO,CAAC,eAAe,CAAC,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,IAAI,OAAO;IAMhB,qBAAqB,IAAI,OAAO,CAAC,qBAAqB,CAAC;IAUvD,OAAO,CACX,MAAM,EAAE,eAAe,CAAC,IAAI,CAAC,sBAAsB,CAAC,6BAA6B,CAAC,EAClF,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,eAAe,CAAC;IAmC3B,UAAU,CACR,QAAQ,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,sBAAsB,CAAC,6BAA6B,CAAC,CAAC,KAChG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAIrB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B/B,aAAa,IAAI,OAAO,CAAC,YAAY,CAAC;YAY9B,oBAAoB;YAgBpB,qBAAqB;IAgBnC,OAAO,CAAC,sBAAsB;IAqC9B,OAAO,CAAC,0BAA0B;IAalC,OAAO,CAAC,gBAAgB;YAQV,mBAAmB;YAYnB,SAAS;YA0BT,oBAAoB;YAkBpB,2BAA2B;YAmB3B,eAAe;YAgBf,UAAU;CA6HzB"}
package/build/provider.js CHANGED
@@ -9,6 +9,7 @@ const frame_father_1 = require("@psdk/frame-father");
9
9
  const await_timeout_1 = __importDefault(require("await-timeout"));
10
10
  const connected_1 = require("./connected");
11
11
  const taro_1 = __importDefault(require("@tarojs/taro"));
12
+ const ANDROID_BLE_MTU = 247;
12
13
  class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
13
14
  options;
14
15
  discoveredCallback;
@@ -42,6 +43,7 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
42
43
  const origin = device.origin;
43
44
  await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options?.timeout, });
44
45
  await await_timeout_1.default.set(500);
46
+ const negotiatedMtu = await this.setBLEMTU(origin.deviceId);
45
47
  const services = await this.getBLEDeviceServices({ deviceId: origin.deviceId });
46
48
  const scpair = await this.findSCPair({
47
49
  deviceId: origin.deviceId,
@@ -50,9 +52,14 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
50
52
  if (!scpair) {
51
53
  return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
52
54
  }
55
+ let flowControl = this.options?.flowControl;
56
+ if (flowControl?.enabled && negotiatedMtu > 0) {
57
+ flowControl = { ...flowControl, initialMtu: negotiatedMtu };
58
+ }
53
59
  this.connectedDevice = new connected_1.TaroBleConnectedDevice({
54
60
  device: origin,
55
61
  scpair,
62
+ flowControl,
56
63
  });
57
64
  await this.stopDiscovery();
58
65
  return Promise.resolve(this.connectedDevice);
@@ -189,6 +196,30 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
189
196
  });
190
197
  });
191
198
  }
199
+ async setBLEMTU(deviceId) {
200
+ try {
201
+ if (String(taro_1.default.getSystemInfoSync().platform).toLowerCase() !== 'android') {
202
+ return 0;
203
+ }
204
+ }
205
+ catch {
206
+ return 0;
207
+ }
208
+ if (typeof taro_1.default.setBLEMTU !== 'function') {
209
+ return 0;
210
+ }
211
+ return new Promise((resolve) => {
212
+ taro_1.default.setBLEMTU({
213
+ deviceId,
214
+ mtu: ANDROID_BLE_MTU,
215
+ success: () => resolve(ANDROID_BLE_MTU),
216
+ fail: (error) => {
217
+ console.warn('[BLE] setBLEMTU failed; using default MTU', error);
218
+ resolve(0);
219
+ },
220
+ });
221
+ });
222
+ }
192
223
  async getBLEDeviceServices(options) {
193
224
  return new Promise((resolve, reject) => {
194
225
  taro_1.default.getBLEDeviceServices({
@@ -247,6 +278,16 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
247
278
  });
248
279
  _characteristicsMap.set(service.uuid, characteristics);
249
280
  }
281
+ if (this.options?.flowControl?.enabled) {
282
+ return (0, device_bluetooth_traits_1.findBleFlowControlCharacteristics)({
283
+ services: options.services,
284
+ flowControlOptions: this.options.flowControl,
285
+ isServiceAllowed: service => device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices),
286
+ getServiceUuid: service => service.uuid,
287
+ getCharacteristics: service => _characteristicsMap.get(service.uuid),
288
+ getCharacteristicUuid: characteristic => characteristic.uuid,
289
+ });
290
+ }
250
291
  // use special first
251
292
  if (this.options?.allowedWriteCharacteristic && this.options?.allowedReadCharacteristic) {
252
293
  for (const service of options.services) {