@psdk/device-ble-taro 0.5.17 → 0.5.18

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.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ @psdk/device-ble-taro
2
+ ===
3
+
4
+ ```
5
+ npm i @psdk/device-ble-taro
6
+ ```
7
+
8
+ <!--harmony-->
9
+
10
+ ## How to use
11
+
12
+ ```js
13
+ const bluetooth = new TaroBleBluetooth({});
14
+ bluetooth.discovered(async (devices) => {
15
+ // ...
16
+ });
17
+
18
+ await bluetooth.startDiscovery();
19
+ ```
20
+
@@ -8,6 +8,11 @@ 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
10
  class TaroBleConnectedDevice {
11
+ device;
12
+ scpair;
13
+ _connectionState;
14
+ dataOfRead;
15
+ notifyCallback;
11
16
  constructor(options) {
12
17
  this.device = options.device;
13
18
  this.scpair = options.scpair;
@@ -89,12 +94,11 @@ class TaroBleConnectedDevice {
89
94
  return this.scpair.read != null;
90
95
  }
91
96
  async read(options) {
92
- var _a;
93
97
  if (this.dataOfRead.length > 0) {
94
98
  this.dataOfRead.splice(0, this.dataOfRead.length);
95
99
  }
96
100
  await this.realRead();
97
- const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000);
101
+ const timeout = (options?.timeout ?? 5000);
98
102
  const startTime = +new Date();
99
103
  while (true) {
100
104
  const now = +new Date();
package/build/provider.js CHANGED
@@ -10,6 +10,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
12
  class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
13
+ options;
14
+ discoveredCallback;
15
+ connectedDevice;
13
16
  constructor(options) {
14
17
  super();
15
18
  this.options = options;
@@ -30,15 +33,14 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
30
33
  });
31
34
  }
32
35
  async connect(device, options) {
33
- var _a;
34
36
  if (this.isConnected()) {
35
- if (!((_a = options === null || options === void 0 ? void 0 : options.autoSwitchDevice) !== null && _a !== void 0 ? _a : false)) {
37
+ if (!(options?.autoSwitchDevice ?? false)) {
36
38
  return this.connectedDevice;
37
39
  }
38
40
  await this.connectedDevice.disconnect();
39
41
  }
40
42
  const origin = device.origin;
41
- await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options === null || options === void 0 ? void 0 : options.timeout, });
43
+ await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options?.timeout, });
42
44
  await await_timeout_1.default.set(500);
43
45
  const services = await this.getBLEDeviceServices({ deviceId: origin.deviceId });
44
46
  const scpair = await this.findSCPair({
@@ -131,11 +133,10 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
131
133
  }
132
134
  const devices = foundDevices
133
135
  .filter(item => {
134
- var _a, _b;
135
136
  const name = item.name || item.localName;
136
137
  if (name)
137
138
  return true;
138
- return (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.allowNoName) !== null && _b !== void 0 ? _b : true;
139
+ return this.options?.allowNoName ?? true;
139
140
  })
140
141
  .map(item => new device_bluetooth_traits_1.JluetoothDevice({
141
142
  origin: item,
@@ -210,7 +211,6 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
210
211
  });
211
212
  }
212
213
  async findSCPair(options) {
213
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
214
214
  // fill data
215
215
  const _characteristicsMap = new Map();
216
216
  for (const service of options.services) {
@@ -221,21 +221,21 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
221
221
  _characteristicsMap.set(service.uuid, characteristics);
222
222
  }
223
223
  // use special first
224
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowedWriteCharacteristic) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.allowedReadCharacteristic)) {
224
+ if (this.options?.allowedWriteCharacteristic && this.options?.allowedReadCharacteristic) {
225
225
  for (const service of options.services) {
226
- if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_c = this.options) === null || _c === void 0 ? void 0 : _c.allowServices)) {
226
+ if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
227
227
  continue;
228
228
  }
229
229
  const characteristics = _characteristicsMap.get(service.uuid);
230
230
  let writeCharacteristic = null;
231
231
  let readCharacteristic = null;
232
232
  for (const characteristic of characteristics) {
233
- if (((_d = this.options) === null || _d === void 0 ? void 0 : _d.allowedWriteCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
233
+ if (this.options?.allowedWriteCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
234
234
  writeCharacteristic = characteristic;
235
235
  }
236
236
  }
237
237
  for (const characteristic of characteristics) {
238
- if (((_e = this.options) === null || _e === void 0 ? void 0 : _e.allowedReadCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
238
+ if (this.options?.allowedReadCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
239
239
  readCharacteristic = characteristic;
240
240
  }
241
241
  }
@@ -250,7 +250,7 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
250
250
  }
251
251
  // choose best characteristic
252
252
  for (const service of options.services) {
253
- if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_f = this.options) === null || _f === void 0 ? void 0 : _f.allowServices)) {
253
+ if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
254
254
  continue;
255
255
  }
256
256
  const characteristics = _characteristicsMap.get(service.uuid);
@@ -269,10 +269,10 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
269
269
  };
270
270
  }
271
271
  }
272
- if ((_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.allowDetectDifferentCharacteristic) !== null && _h !== void 0 ? _h : true) {
272
+ if (this.options?.allowDetectDifferentCharacteristic ?? true) {
273
273
  // choose different characteristic with write and read properties
274
274
  for (const service of options.services) {
275
- if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_j = this.options) === null || _j === void 0 ? void 0 : _j.allowServices)) {
275
+ if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
276
276
  continue;
277
277
  }
278
278
  const characteristics = _characteristicsMap.get(service.uuid);
@@ -297,7 +297,7 @@ class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
297
297
  else {
298
298
  // only write
299
299
  for (const service of options.services) {
300
- if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_k = this.options) === null || _k === void 0 ? void 0 : _k.allowServices)) {
300
+ if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
301
301
  continue;
302
302
  }
303
303
  const characteristics = _characteristicsMap.get(service.uuid);