@psdk/device-ble-taro 0.5.17 → 0.5.19
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 +20 -0
- package/build/connected.js +6 -2
- package/build/provider.js +14 -14
- package/dist/index.js +4 -4
- package/dist/index.js.map +4 -4
- package/package.json +4 -4
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
|
+
|
package/build/connected.js
CHANGED
|
@@ -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 = (
|
|
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 (!(
|
|
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
|
|
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
|
|
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 (
|
|
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,
|
|
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 (
|
|
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 (
|
|
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,
|
|
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 (
|
|
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,
|
|
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,
|
|
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);
|