@psdk/device-ble-taro 0.3.4 → 0.5.0
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/build/connected.js +26 -15
- package/build/index.js +18 -2
- package/build/provider.js +44 -37
- package/package.json +9 -11
package/build/connected.js
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TaroBleConnectedDevice = void 0;
|
|
7
|
+
const frame_father_1 = require("@psdk/frame-father");
|
|
8
|
+
const await_timeout_1 = __importDefault(require("await-timeout"));
|
|
9
|
+
const taro_1 = __importDefault(require("@tarojs/taro"));
|
|
10
|
+
class TaroBleConnectedDevice {
|
|
11
|
+
device;
|
|
12
|
+
scpair;
|
|
13
|
+
_connectionState;
|
|
14
|
+
dataOfRead;
|
|
15
|
+
notifyCallback;
|
|
5
16
|
constructor(options) {
|
|
6
17
|
this.device = options.device;
|
|
7
18
|
this.scpair = options.scpair;
|
|
8
|
-
this._connectionState = ConnectionState.CONNECTED;
|
|
19
|
+
this._connectionState = frame_father_1.ConnectionState.CONNECTED;
|
|
9
20
|
this.dataOfRead = [];
|
|
10
21
|
this.doListen();
|
|
11
22
|
}
|
|
@@ -15,16 +26,16 @@ export class TaroBleConnectedDevice {
|
|
|
15
26
|
doListen() {
|
|
16
27
|
if (this.scpair.read == null)
|
|
17
28
|
return;
|
|
18
|
-
|
|
29
|
+
taro_1.default.onBLECharacteristicValueChange(async (value) => {
|
|
19
30
|
// this.dataOfRead.push(value)
|
|
20
31
|
if (this.notifyCallback) {
|
|
21
32
|
await this.notifyCallback(value);
|
|
22
33
|
}
|
|
23
34
|
});
|
|
24
|
-
|
|
25
|
-
this._connectionState = value.connected ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
|
|
35
|
+
taro_1.default.onBLEConnectionStateChange(value => {
|
|
36
|
+
this._connectionState = value.connected ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
|
|
26
37
|
});
|
|
27
|
-
|
|
38
|
+
taro_1.default.notifyBLECharacteristicValueChange({
|
|
28
39
|
deviceId: this.device.deviceId,
|
|
29
40
|
serviceId: this.scpair.service.uuid,
|
|
30
41
|
characteristicId: this.scpair.read.uuid,
|
|
@@ -40,7 +51,7 @@ export class TaroBleConnectedDevice {
|
|
|
40
51
|
reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
|
|
41
52
|
return;
|
|
42
53
|
}
|
|
43
|
-
|
|
54
|
+
taro_1.default.readBLECharacteristicValue({
|
|
44
55
|
deviceId: this.device.deviceId,
|
|
45
56
|
serviceId: this.scpair.service.uuid,
|
|
46
57
|
characteristicId: this.scpair.read.uuid,
|
|
@@ -67,7 +78,7 @@ export class TaroBleConnectedDevice {
|
|
|
67
78
|
}
|
|
68
79
|
async disconnect() {
|
|
69
80
|
return new Promise((resolve, reject) => {
|
|
70
|
-
|
|
81
|
+
taro_1.default.closeBLEConnection({
|
|
71
82
|
deviceId: this.device.deviceId,
|
|
72
83
|
success: () => {
|
|
73
84
|
this.device = null;
|
|
@@ -83,12 +94,11 @@ export class TaroBleConnectedDevice {
|
|
|
83
94
|
return this.scpair.read != null;
|
|
84
95
|
}
|
|
85
96
|
async read(options) {
|
|
86
|
-
var _a;
|
|
87
97
|
if (this.dataOfRead.length > 0) {
|
|
88
98
|
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
89
99
|
}
|
|
90
100
|
await this.realRead();
|
|
91
|
-
const timeout = (
|
|
101
|
+
const timeout = (options?.timeout ?? 5000);
|
|
92
102
|
const startTime = +new Date();
|
|
93
103
|
while (true) {
|
|
94
104
|
const now = +new Date();
|
|
@@ -96,7 +106,7 @@ export class TaroBleConnectedDevice {
|
|
|
96
106
|
return new Uint8Array([]);
|
|
97
107
|
}
|
|
98
108
|
if (this.dataOfRead.length == 0) {
|
|
99
|
-
await
|
|
109
|
+
await await_timeout_1.default.set(100);
|
|
100
110
|
continue;
|
|
101
111
|
}
|
|
102
112
|
const data = this.dataOfRead.pop();
|
|
@@ -106,7 +116,7 @@ export class TaroBleConnectedDevice {
|
|
|
106
116
|
}
|
|
107
117
|
async write(data) {
|
|
108
118
|
return new Promise((resolve, reject) => {
|
|
109
|
-
|
|
119
|
+
taro_1.default.writeBLECharacteristicValue({
|
|
110
120
|
deviceId: this.device.deviceId,
|
|
111
121
|
serviceId: this.scpair.service.uuid,
|
|
112
122
|
characteristicId: this.scpair.write.uuid,
|
|
@@ -128,3 +138,4 @@ export class TaroBleConnectedDevice {
|
|
|
128
138
|
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
129
139
|
}
|
|
130
140
|
}
|
|
141
|
+
exports.TaroBleConnectedDevice = TaroBleConnectedDevice;
|
package/build/index.js
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./connected"), exports);
|
|
18
|
+
__exportStar(require("./provider"), exports);
|
package/build/provider.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TaroBleBluetooth = void 0;
|
|
7
|
+
const device_bluetooth_traits_1 = require("@psdk/device-bluetooth-traits");
|
|
8
|
+
const frame_father_1 = require("@psdk/frame-father");
|
|
9
|
+
const await_timeout_1 = __importDefault(require("await-timeout"));
|
|
10
|
+
const connected_1 = require("./connected");
|
|
11
|
+
const taro_1 = __importDefault(require("@tarojs/taro"));
|
|
12
|
+
class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
|
|
13
|
+
options;
|
|
14
|
+
discoveredCallback;
|
|
15
|
+
connectedDevice;
|
|
7
16
|
constructor(options) {
|
|
8
17
|
super();
|
|
9
18
|
this.options = options;
|
|
@@ -12,11 +21,11 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
12
21
|
isConnected() {
|
|
13
22
|
if (!this.connectedDevice)
|
|
14
23
|
return false;
|
|
15
|
-
return this.connectedDevice.connectionState() === ConnectionState.CONNECTED;
|
|
24
|
+
return this.connectedDevice.connectionState() === frame_father_1.ConnectionState.CONNECTED;
|
|
16
25
|
}
|
|
17
26
|
async bluetoothAdapterState() {
|
|
18
27
|
return new Promise((resolve, reject) => {
|
|
19
|
-
|
|
28
|
+
taro_1.default.getBluetoothAdapterState({
|
|
20
29
|
success: (r) => resolve(r),
|
|
21
30
|
// @ts-ignore
|
|
22
31
|
fail: (e) => reject(e)
|
|
@@ -24,16 +33,15 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
24
33
|
});
|
|
25
34
|
}
|
|
26
35
|
async connect(device, options) {
|
|
27
|
-
var _a;
|
|
28
36
|
if (this.isConnected()) {
|
|
29
|
-
if (!(
|
|
37
|
+
if (!(options?.autoSwitchDevice ?? false)) {
|
|
30
38
|
return this.connectedDevice;
|
|
31
39
|
}
|
|
32
40
|
await this.connectedDevice.disconnect();
|
|
33
41
|
}
|
|
34
42
|
const origin = device.origin;
|
|
35
|
-
await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options
|
|
36
|
-
await
|
|
43
|
+
await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options?.timeout, });
|
|
44
|
+
await await_timeout_1.default.set(500);
|
|
37
45
|
const services = await this.getBLEDeviceServices({ deviceId: origin.deviceId });
|
|
38
46
|
const scpair = await this.findSCPair({
|
|
39
47
|
deviceId: origin.deviceId,
|
|
@@ -42,7 +50,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
42
50
|
if (!scpair) {
|
|
43
51
|
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
|
|
44
52
|
}
|
|
45
|
-
this.connectedDevice = new TaroBleConnectedDevice({
|
|
53
|
+
this.connectedDevice = new connected_1.TaroBleConnectedDevice({
|
|
46
54
|
device: origin,
|
|
47
55
|
scpair,
|
|
48
56
|
});
|
|
@@ -55,18 +63,18 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
55
63
|
async startDiscovery() {
|
|
56
64
|
await this.requestLocation();
|
|
57
65
|
await this.closeBluetoothAdapter();
|
|
58
|
-
await
|
|
66
|
+
await await_timeout_1.default.set(300);
|
|
59
67
|
await this.openBluetoothAdapter();
|
|
60
68
|
const state = await this.bluetoothAdapterState();
|
|
61
69
|
if (!state.available) {
|
|
62
|
-
await
|
|
70
|
+
await await_timeout_1.default.set(500);
|
|
63
71
|
}
|
|
64
72
|
if (state.discovering) {
|
|
65
73
|
await this.stopDiscovery();
|
|
66
|
-
await
|
|
74
|
+
await await_timeout_1.default.set(500);
|
|
67
75
|
}
|
|
68
76
|
return new Promise((resolve, reject) => {
|
|
69
|
-
|
|
77
|
+
taro_1.default.startBluetoothDevicesDiscovery({
|
|
70
78
|
// services: this.allowServices,
|
|
71
79
|
allowDuplicatesKey: false,
|
|
72
80
|
interval: 0,
|
|
@@ -79,7 +87,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
79
87
|
}
|
|
80
88
|
async stopDiscovery() {
|
|
81
89
|
return new Promise((resolve, reject) => {
|
|
82
|
-
|
|
90
|
+
taro_1.default.stopBluetoothDevicesDiscovery({
|
|
83
91
|
success: (result) => resolve(result),
|
|
84
92
|
// @ts-ignore
|
|
85
93
|
fail: (e) => reject(e),
|
|
@@ -89,7 +97,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
89
97
|
// ### inner function
|
|
90
98
|
async openBluetoothAdapter() {
|
|
91
99
|
return new Promise((resolve, reject) => {
|
|
92
|
-
|
|
100
|
+
taro_1.default.openBluetoothAdapter({
|
|
93
101
|
success: (ret) => {
|
|
94
102
|
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
95
103
|
reject(ret);
|
|
@@ -104,7 +112,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
104
112
|
}
|
|
105
113
|
async closeBluetoothAdapter() {
|
|
106
114
|
return new Promise((resolve, reject) => {
|
|
107
|
-
|
|
115
|
+
taro_1.default.closeBluetoothAdapter({
|
|
108
116
|
success: (ret) => {
|
|
109
117
|
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
110
118
|
reject(ret);
|
|
@@ -118,20 +126,19 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
118
126
|
});
|
|
119
127
|
}
|
|
120
128
|
onBluetoothDeviceFound() {
|
|
121
|
-
|
|
129
|
+
taro_1.default.onBluetoothDeviceFound(async (result) => {
|
|
122
130
|
const foundDevices = result.devices;
|
|
123
131
|
if (!foundDevices.length) {
|
|
124
132
|
return;
|
|
125
133
|
}
|
|
126
134
|
const devices = foundDevices
|
|
127
135
|
.filter(item => {
|
|
128
|
-
var _a, _b;
|
|
129
136
|
const name = item.name || item.localName;
|
|
130
137
|
if (name)
|
|
131
138
|
return true;
|
|
132
|
-
return
|
|
139
|
+
return this.options?.allowNoName ?? true;
|
|
133
140
|
})
|
|
134
|
-
.map(item => new JluetoothDevice({
|
|
141
|
+
.map(item => new device_bluetooth_traits_1.JluetoothDevice({
|
|
135
142
|
origin: item,
|
|
136
143
|
name: item.name || item.localName,
|
|
137
144
|
deviceId: item.deviceId,
|
|
@@ -146,7 +153,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
146
153
|
}
|
|
147
154
|
async createBLEConnection(options) {
|
|
148
155
|
return new Promise((resolve, reject) => {
|
|
149
|
-
|
|
156
|
+
taro_1.default.createBLEConnection({
|
|
150
157
|
deviceId: options.deviceId,
|
|
151
158
|
timeout: options.timeout,
|
|
152
159
|
success: () => resolve(),
|
|
@@ -157,7 +164,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
157
164
|
}
|
|
158
165
|
async getBLEDeviceServices(options) {
|
|
159
166
|
return new Promise((resolve, reject) => {
|
|
160
|
-
|
|
167
|
+
taro_1.default.getBLEDeviceServices({
|
|
161
168
|
deviceId: options.deviceId,
|
|
162
169
|
success: (result) => {
|
|
163
170
|
if (result.errMsg.indexOf('ok') == -1) {
|
|
@@ -173,7 +180,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
173
180
|
}
|
|
174
181
|
async getBLEDeviceCharacteristics(options) {
|
|
175
182
|
return new Promise((resolve, reject) => {
|
|
176
|
-
|
|
183
|
+
taro_1.default.getBLEDeviceCharacteristics({
|
|
177
184
|
deviceId: options.deviceId,
|
|
178
185
|
serviceId: options.serviceId,
|
|
179
186
|
success: (result) => {
|
|
@@ -196,7 +203,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
196
203
|
// // @ts-ignore
|
|
197
204
|
// fail: (e) => reject(e),
|
|
198
205
|
// });
|
|
199
|
-
|
|
206
|
+
taro_1.default.getLocation({
|
|
200
207
|
success: () => resolve(),
|
|
201
208
|
// @ts-ignore
|
|
202
209
|
fail: (e) => reject(e),
|
|
@@ -204,7 +211,6 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
204
211
|
});
|
|
205
212
|
}
|
|
206
213
|
async findSCPair(options) {
|
|
207
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
208
214
|
// fill data
|
|
209
215
|
const _characteristicsMap = new Map();
|
|
210
216
|
for (const service of options.services) {
|
|
@@ -215,21 +221,21 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
215
221
|
_characteristicsMap.set(service.uuid, characteristics);
|
|
216
222
|
}
|
|
217
223
|
// use special first
|
|
218
|
-
if (
|
|
224
|
+
if (this.options?.allowedWriteCharacteristic && this.options?.allowedReadCharacteristic) {
|
|
219
225
|
for (const service of options.services) {
|
|
220
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid,
|
|
226
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
221
227
|
continue;
|
|
222
228
|
}
|
|
223
229
|
const characteristics = _characteristicsMap.get(service.uuid);
|
|
224
230
|
let writeCharacteristic = null;
|
|
225
231
|
let readCharacteristic = null;
|
|
226
232
|
for (const characteristic of characteristics) {
|
|
227
|
-
if (
|
|
233
|
+
if (this.options?.allowedWriteCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
|
|
228
234
|
writeCharacteristic = characteristic;
|
|
229
235
|
}
|
|
230
236
|
}
|
|
231
237
|
for (const characteristic of characteristics) {
|
|
232
|
-
if (
|
|
238
|
+
if (this.options?.allowedReadCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
|
|
233
239
|
readCharacteristic = characteristic;
|
|
234
240
|
}
|
|
235
241
|
}
|
|
@@ -244,7 +250,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
244
250
|
}
|
|
245
251
|
// choose best characteristic
|
|
246
252
|
for (const service of options.services) {
|
|
247
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid,
|
|
253
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
248
254
|
continue;
|
|
249
255
|
}
|
|
250
256
|
const characteristics = _characteristicsMap.get(service.uuid);
|
|
@@ -263,10 +269,10 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
263
269
|
};
|
|
264
270
|
}
|
|
265
271
|
}
|
|
266
|
-
if (
|
|
272
|
+
if (this.options?.allowDetectDifferentCharacteristic ?? true) {
|
|
267
273
|
// choose different characteristic with write and read properties
|
|
268
274
|
for (const service of options.services) {
|
|
269
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid,
|
|
275
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
270
276
|
continue;
|
|
271
277
|
}
|
|
272
278
|
const characteristics = _characteristicsMap.get(service.uuid);
|
|
@@ -291,7 +297,7 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
291
297
|
else {
|
|
292
298
|
// only write
|
|
293
299
|
for (const service of options.services) {
|
|
294
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid,
|
|
300
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
295
301
|
continue;
|
|
296
302
|
}
|
|
297
303
|
const characteristics = _characteristicsMap.get(service.uuid);
|
|
@@ -310,3 +316,4 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
310
316
|
}
|
|
311
317
|
}
|
|
312
318
|
}
|
|
319
|
+
exports.TaroBleBluetooth = TaroBleBluetooth;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdk/device-ble-taro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "psdk",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -15,25 +15,23 @@
|
|
|
15
15
|
},
|
|
16
16
|
"author": "",
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"@babel/preset-env": "^7.24.5",
|
|
20
|
-
"@babel/preset-typescript": "^7.24.1",
|
|
21
|
-
"@tarojs/taro": "^3.6.29",
|
|
18
|
+
"@tarojs/taro": "^3.6.34",
|
|
22
19
|
"@types/jest": "^29.5.12",
|
|
23
20
|
"@vercel/ncc": "^0.38.1",
|
|
24
|
-
"babel-jest": "^29.7.0",
|
|
25
21
|
"jest": "^29.7.0",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
22
|
+
"ts-jest": "^29.1.5",
|
|
23
|
+
"ts-node": "^10.9.2",
|
|
24
|
+
"typedoc": "^0.26.4",
|
|
25
|
+
"typescript": "^5.5.3"
|
|
28
26
|
},
|
|
29
27
|
"dependencies": {
|
|
30
|
-
"@psdk/device-bluetooth-traits": "0.
|
|
31
|
-
"@psdk/frame-father": "0.
|
|
28
|
+
"@psdk/device-bluetooth-traits": "0.5.0",
|
|
29
|
+
"@psdk/frame-father": "0.5.0",
|
|
32
30
|
"await-timeout": "^1.1.1"
|
|
33
31
|
},
|
|
34
32
|
"files": [
|
|
35
33
|
"build",
|
|
36
34
|
"!build/browser/psdk*"
|
|
37
35
|
],
|
|
38
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "0de75b4e37274d7a68a04aa34501fa3297769185"
|
|
39
37
|
}
|