@psdk/device-ble-taro 0.3.3 → 0.4.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 +87 -93
- package/build/index.js +18 -2
- package/build/provider.js +232 -256
- package/package.json +9 -11
package/build/connected.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
4
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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;
|
|
14
16
|
constructor(options) {
|
|
15
17
|
this.device = options.device;
|
|
16
18
|
this.scpair = options.scpair;
|
|
17
|
-
this._connectionState = ConnectionState.CONNECTED;
|
|
19
|
+
this._connectionState = frame_father_1.ConnectionState.CONNECTED;
|
|
18
20
|
this.dataOfRead = [];
|
|
19
21
|
this.doListen();
|
|
20
22
|
}
|
|
@@ -24,16 +26,16 @@ export class TaroBleConnectedDevice {
|
|
|
24
26
|
doListen() {
|
|
25
27
|
if (this.scpair.read == null)
|
|
26
28
|
return;
|
|
27
|
-
|
|
29
|
+
taro_1.default.onBLECharacteristicValueChange(async (value) => {
|
|
28
30
|
// this.dataOfRead.push(value)
|
|
29
31
|
if (this.notifyCallback) {
|
|
30
|
-
|
|
32
|
+
await this.notifyCallback(value);
|
|
31
33
|
}
|
|
32
|
-
}));
|
|
33
|
-
Taro.onBLEConnectionStateChange(value => {
|
|
34
|
-
this._connectionState = value.connected ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
|
|
35
34
|
});
|
|
36
|
-
|
|
35
|
+
taro_1.default.onBLEConnectionStateChange(value => {
|
|
36
|
+
this._connectionState = value.connected ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
|
|
37
|
+
});
|
|
38
|
+
taro_1.default.notifyBLECharacteristicValueChange({
|
|
37
39
|
deviceId: this.device.deviceId,
|
|
38
40
|
serviceId: this.scpair.service.uuid,
|
|
39
41
|
characteristicId: this.scpair.read.uuid,
|
|
@@ -43,27 +45,25 @@ export class TaroBleConnectedDevice {
|
|
|
43
45
|
fail: (e) => console.error(e),
|
|
44
46
|
});
|
|
45
47
|
}
|
|
46
|
-
realRead() {
|
|
47
|
-
return
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
fail: (e) => reject(e),
|
|
66
|
-
});
|
|
48
|
+
async realRead() {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
if (this.scpair.read == null) {
|
|
51
|
+
reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
taro_1.default.readBLECharacteristicValue({
|
|
55
|
+
deviceId: this.device.deviceId,
|
|
56
|
+
serviceId: this.scpair.service.uuid,
|
|
57
|
+
characteristicId: this.scpair.read.uuid,
|
|
58
|
+
success: result => {
|
|
59
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
60
|
+
reject(result.errMsg);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
resolve();
|
|
64
|
+
},
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
fail: (e) => reject(e),
|
|
67
67
|
});
|
|
68
68
|
});
|
|
69
69
|
}
|
|
@@ -76,68 +76,61 @@ export class TaroBleConnectedDevice {
|
|
|
76
76
|
deviceName() {
|
|
77
77
|
return this.device.localName || this.device.name || 'NONE';
|
|
78
78
|
}
|
|
79
|
-
disconnect() {
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
fail: (e) => reject(e),
|
|
91
|
-
});
|
|
79
|
+
async disconnect() {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
taro_1.default.closeBLEConnection({
|
|
82
|
+
deviceId: this.device.deviceId,
|
|
83
|
+
success: () => {
|
|
84
|
+
this.device = null;
|
|
85
|
+
this.scpair = null;
|
|
86
|
+
resolve();
|
|
87
|
+
},
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
fail: (e) => reject(e),
|
|
92
90
|
});
|
|
93
91
|
});
|
|
94
92
|
}
|
|
95
93
|
canRead() {
|
|
96
94
|
return this.scpair.read != null;
|
|
97
95
|
}
|
|
98
|
-
read(options) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
async read(options) {
|
|
97
|
+
if (this.dataOfRead.length > 0) {
|
|
98
|
+
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
99
|
+
}
|
|
100
|
+
await this.realRead();
|
|
101
|
+
const timeout = (options?.timeout ?? 5000);
|
|
102
|
+
const startTime = +new Date();
|
|
103
|
+
while (true) {
|
|
104
|
+
const now = +new Date();
|
|
105
|
+
if (now - startTime > timeout) {
|
|
106
|
+
return new Uint8Array([]);
|
|
103
107
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
while (true) {
|
|
108
|
-
const now = +new Date();
|
|
109
|
-
if (now - startTime > timeout) {
|
|
110
|
-
return new Uint8Array([]);
|
|
111
|
-
}
|
|
112
|
-
if (this.dataOfRead.length == 0) {
|
|
113
|
-
yield Timeout.set(100);
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
const data = this.dataOfRead.pop();
|
|
117
|
-
const value = data.value;
|
|
118
|
-
return new Uint8Array(value);
|
|
108
|
+
if (this.dataOfRead.length == 0) {
|
|
109
|
+
await await_timeout_1.default.set(100);
|
|
110
|
+
continue;
|
|
119
111
|
}
|
|
120
|
-
|
|
112
|
+
const data = this.dataOfRead.pop();
|
|
113
|
+
const value = data.value;
|
|
114
|
+
return new Uint8Array(value);
|
|
115
|
+
}
|
|
121
116
|
}
|
|
122
|
-
write(data) {
|
|
123
|
-
return
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
fail: (e) => reject(e)
|
|
140
|
-
});
|
|
117
|
+
async write(data) {
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
taro_1.default.writeBLECharacteristicValue({
|
|
120
|
+
deviceId: this.device.deviceId,
|
|
121
|
+
serviceId: this.scpair.service.uuid,
|
|
122
|
+
characteristicId: this.scpair.write.uuid,
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
value: data.buffer,
|
|
125
|
+
success: result => {
|
|
126
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
127
|
+
reject(result.errMsg);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
resolve();
|
|
131
|
+
},
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
fail: (e) => reject(e)
|
|
141
134
|
});
|
|
142
135
|
});
|
|
143
136
|
}
|
|
@@ -145,3 +138,4 @@ export class TaroBleConnectedDevice {
|
|
|
145
138
|
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
146
139
|
}
|
|
147
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,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
4
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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;
|
|
16
16
|
constructor(options) {
|
|
17
17
|
super();
|
|
18
18
|
this.options = options;
|
|
@@ -21,138 +21,124 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
21
21
|
isConnected() {
|
|
22
22
|
if (!this.connectedDevice)
|
|
23
23
|
return false;
|
|
24
|
-
return this.connectedDevice.connectionState() === ConnectionState.CONNECTED;
|
|
24
|
+
return this.connectedDevice.connectionState() === frame_father_1.ConnectionState.CONNECTED;
|
|
25
25
|
}
|
|
26
|
-
bluetoothAdapterState() {
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
fail: (e) => reject(e)
|
|
33
|
-
});
|
|
26
|
+
async bluetoothAdapterState() {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
taro_1.default.getBluetoothAdapterState({
|
|
29
|
+
success: (r) => resolve(r),
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
fail: (e) => reject(e)
|
|
34
32
|
});
|
|
35
33
|
});
|
|
36
34
|
}
|
|
37
|
-
connect(device, options) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!((_a = options === null || options === void 0 ? void 0 : options.autoSwitchDevice) !== null && _a !== void 0 ? _a : false)) {
|
|
42
|
-
return this.connectedDevice;
|
|
43
|
-
}
|
|
44
|
-
yield this.connectedDevice.disconnect();
|
|
45
|
-
}
|
|
46
|
-
const origin = device.origin;
|
|
47
|
-
yield this.createBLEConnection({ deviceId: origin.deviceId, timeout: options === null || options === void 0 ? void 0 : options.timeout, });
|
|
48
|
-
yield Timeout.set(500);
|
|
49
|
-
const services = yield this.getBLEDeviceServices({ deviceId: origin.deviceId });
|
|
50
|
-
const scpair = yield this.findSCPair({
|
|
51
|
-
deviceId: origin.deviceId,
|
|
52
|
-
services,
|
|
53
|
-
});
|
|
54
|
-
if (!scpair) {
|
|
55
|
-
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
|
|
35
|
+
async connect(device, options) {
|
|
36
|
+
if (this.isConnected()) {
|
|
37
|
+
if (!(options?.autoSwitchDevice ?? false)) {
|
|
38
|
+
return this.connectedDevice;
|
|
56
39
|
}
|
|
57
|
-
this.connectedDevice
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
40
|
+
await this.connectedDevice.disconnect();
|
|
41
|
+
}
|
|
42
|
+
const origin = device.origin;
|
|
43
|
+
await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options?.timeout, });
|
|
44
|
+
await await_timeout_1.default.set(500);
|
|
45
|
+
const services = await this.getBLEDeviceServices({ deviceId: origin.deviceId });
|
|
46
|
+
const scpair = await this.findSCPair({
|
|
47
|
+
deviceId: origin.deviceId,
|
|
48
|
+
services,
|
|
63
49
|
});
|
|
50
|
+
if (!scpair) {
|
|
51
|
+
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
|
|
52
|
+
}
|
|
53
|
+
this.connectedDevice = new connected_1.TaroBleConnectedDevice({
|
|
54
|
+
device: origin,
|
|
55
|
+
scpair,
|
|
56
|
+
});
|
|
57
|
+
await this.stopDiscovery();
|
|
58
|
+
return Promise.resolve(this.connectedDevice);
|
|
64
59
|
}
|
|
65
60
|
discovered(callback) {
|
|
66
61
|
this.discoveredCallback = callback;
|
|
67
62
|
}
|
|
68
|
-
startDiscovery() {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// complete: () => resolve(),
|
|
91
|
-
});
|
|
63
|
+
async startDiscovery() {
|
|
64
|
+
await this.requestLocation();
|
|
65
|
+
await this.closeBluetoothAdapter();
|
|
66
|
+
await await_timeout_1.default.set(300);
|
|
67
|
+
await this.openBluetoothAdapter();
|
|
68
|
+
const state = await this.bluetoothAdapterState();
|
|
69
|
+
if (!state.available) {
|
|
70
|
+
await await_timeout_1.default.set(500);
|
|
71
|
+
}
|
|
72
|
+
if (state.discovering) {
|
|
73
|
+
await this.stopDiscovery();
|
|
74
|
+
await await_timeout_1.default.set(500);
|
|
75
|
+
}
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
taro_1.default.startBluetoothDevicesDiscovery({
|
|
78
|
+
// services: this.allowServices,
|
|
79
|
+
allowDuplicatesKey: false,
|
|
80
|
+
interval: 0,
|
|
81
|
+
success: () => resolve(),
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
fail: (e) => reject(e),
|
|
84
|
+
// complete: () => resolve(),
|
|
92
85
|
});
|
|
93
86
|
});
|
|
94
87
|
}
|
|
95
|
-
stopDiscovery() {
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
fail: (e) => reject(e),
|
|
102
|
-
});
|
|
88
|
+
async stopDiscovery() {
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
taro_1.default.stopBluetoothDevicesDiscovery({
|
|
91
|
+
success: (result) => resolve(result),
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
fail: (e) => reject(e),
|
|
103
94
|
});
|
|
104
95
|
});
|
|
105
96
|
}
|
|
106
97
|
// ### inner function
|
|
107
|
-
openBluetoothAdapter() {
|
|
108
|
-
return
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
fail: (e) => reject(e)
|
|
120
|
-
});
|
|
98
|
+
async openBluetoothAdapter() {
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
taro_1.default.openBluetoothAdapter({
|
|
101
|
+
success: (ret) => {
|
|
102
|
+
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
103
|
+
reject(ret);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
resolve();
|
|
107
|
+
},
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
fail: (e) => reject(e)
|
|
121
110
|
});
|
|
122
111
|
});
|
|
123
112
|
}
|
|
124
|
-
closeBluetoothAdapter() {
|
|
125
|
-
return
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
fail: (e) => reject(e)
|
|
137
|
-
});
|
|
113
|
+
async closeBluetoothAdapter() {
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
taro_1.default.closeBluetoothAdapter({
|
|
116
|
+
success: (ret) => {
|
|
117
|
+
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
118
|
+
reject(ret);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
resolve();
|
|
122
|
+
},
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
fail: (e) => reject(e)
|
|
138
125
|
});
|
|
139
126
|
});
|
|
140
127
|
}
|
|
141
128
|
onBluetoothDeviceFound() {
|
|
142
|
-
|
|
129
|
+
taro_1.default.onBluetoothDeviceFound(async (result) => {
|
|
143
130
|
const foundDevices = result.devices;
|
|
144
131
|
if (!foundDevices.length) {
|
|
145
132
|
return;
|
|
146
133
|
}
|
|
147
134
|
const devices = foundDevices
|
|
148
135
|
.filter(item => {
|
|
149
|
-
var _a, _b;
|
|
150
136
|
const name = item.name || item.localName;
|
|
151
137
|
if (name)
|
|
152
138
|
return true;
|
|
153
|
-
return
|
|
139
|
+
return this.options?.allowNoName ?? true;
|
|
154
140
|
})
|
|
155
|
-
.map(item => new JluetoothDevice({
|
|
141
|
+
.map(item => new device_bluetooth_traits_1.JluetoothDevice({
|
|
156
142
|
origin: item,
|
|
157
143
|
name: item.name || item.localName,
|
|
158
144
|
deviceId: item.deviceId,
|
|
@@ -161,183 +147,173 @@ export class TaroBleBluetooth extends Jluetooth {
|
|
|
161
147
|
return;
|
|
162
148
|
}
|
|
163
149
|
if (this.discoveredCallback) {
|
|
164
|
-
|
|
150
|
+
await this.discoveredCallback(devices);
|
|
165
151
|
}
|
|
166
|
-
})
|
|
152
|
+
});
|
|
167
153
|
}
|
|
168
|
-
createBLEConnection(options) {
|
|
169
|
-
return
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
fail: (e) => reject(e),
|
|
177
|
-
});
|
|
154
|
+
async createBLEConnection(options) {
|
|
155
|
+
return new Promise((resolve, reject) => {
|
|
156
|
+
taro_1.default.createBLEConnection({
|
|
157
|
+
deviceId: options.deviceId,
|
|
158
|
+
timeout: options.timeout,
|
|
159
|
+
success: () => resolve(),
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
fail: (e) => reject(e),
|
|
178
162
|
});
|
|
179
163
|
});
|
|
180
164
|
}
|
|
181
|
-
getBLEDeviceServices(options) {
|
|
182
|
-
return
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
fail: (e) => reject(e),
|
|
195
|
-
});
|
|
165
|
+
async getBLEDeviceServices(options) {
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
taro_1.default.getBLEDeviceServices({
|
|
168
|
+
deviceId: options.deviceId,
|
|
169
|
+
success: (result) => {
|
|
170
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
171
|
+
reject(result.errMsg);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
resolve(result.services);
|
|
175
|
+
},
|
|
176
|
+
// @ts-ignore
|
|
177
|
+
fail: (e) => reject(e),
|
|
196
178
|
});
|
|
197
179
|
});
|
|
198
180
|
}
|
|
199
|
-
getBLEDeviceCharacteristics(options) {
|
|
200
|
-
return
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
fail: (e) => reject(e),
|
|
214
|
-
});
|
|
181
|
+
async getBLEDeviceCharacteristics(options) {
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
taro_1.default.getBLEDeviceCharacteristics({
|
|
184
|
+
deviceId: options.deviceId,
|
|
185
|
+
serviceId: options.serviceId,
|
|
186
|
+
success: (result) => {
|
|
187
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
188
|
+
reject(result.errMsg);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
resolve(result.characteristics);
|
|
192
|
+
},
|
|
193
|
+
// @ts-ignore
|
|
194
|
+
fail: (e) => reject(e),
|
|
215
195
|
});
|
|
216
196
|
});
|
|
217
197
|
}
|
|
218
|
-
requestLocation() {
|
|
219
|
-
return
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
fail: (e) => reject(e),
|
|
231
|
-
});
|
|
198
|
+
async requestLocation() {
|
|
199
|
+
return new Promise((resolve, reject) => {
|
|
200
|
+
// Taro.authorize({
|
|
201
|
+
// scope: 'scope.userLocation',
|
|
202
|
+
// success: () => resolve(),
|
|
203
|
+
// // @ts-ignore
|
|
204
|
+
// fail: (e) => reject(e),
|
|
205
|
+
// });
|
|
206
|
+
taro_1.default.getLocation({
|
|
207
|
+
success: () => resolve(),
|
|
208
|
+
// @ts-ignore
|
|
209
|
+
fail: (e) => reject(e),
|
|
232
210
|
});
|
|
233
211
|
});
|
|
234
212
|
}
|
|
235
|
-
findSCPair(options) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const
|
|
213
|
+
async findSCPair(options) {
|
|
214
|
+
// fill data
|
|
215
|
+
const _characteristicsMap = new Map();
|
|
216
|
+
for (const service of options.services) {
|
|
217
|
+
const characteristics = await this.getBLEDeviceCharacteristics({
|
|
218
|
+
deviceId: options.deviceId,
|
|
219
|
+
serviceId: service.uuid,
|
|
220
|
+
});
|
|
221
|
+
_characteristicsMap.set(service.uuid, characteristics);
|
|
222
|
+
}
|
|
223
|
+
// use special first
|
|
224
|
+
if (this.options?.allowedWriteCharacteristic && this.options?.allowedReadCharacteristic) {
|
|
240
225
|
for (const service of options.services) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_c = this.options) === null || _c === void 0 ? void 0 : _c.allowServices)) {
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
const characteristics = _characteristicsMap.get(service.uuid);
|
|
254
|
-
let writeCharacteristic = null;
|
|
255
|
-
let readCharacteristic = null;
|
|
256
|
-
for (const characteristic of characteristics) {
|
|
257
|
-
if (((_d = this.options) === null || _d === void 0 ? void 0 : _d.allowedWriteCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
|
|
258
|
-
writeCharacteristic = characteristic;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
for (const characteristic of characteristics) {
|
|
262
|
-
if (((_e = this.options) === null || _e === void 0 ? void 0 : _e.allowedReadCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
|
|
263
|
-
readCharacteristic = characteristic;
|
|
264
|
-
}
|
|
226
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
230
|
+
let writeCharacteristic = null;
|
|
231
|
+
let readCharacteristic = null;
|
|
232
|
+
for (const characteristic of characteristics) {
|
|
233
|
+
if (this.options?.allowedWriteCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
|
|
234
|
+
writeCharacteristic = characteristic;
|
|
265
235
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
read: readCharacteristic,
|
|
271
|
-
};
|
|
236
|
+
}
|
|
237
|
+
for (const characteristic of characteristics) {
|
|
238
|
+
if (this.options?.allowedReadCharacteristic.toLowerCase() == characteristic.uuid.toLowerCase()) {
|
|
239
|
+
readCharacteristic = characteristic;
|
|
272
240
|
}
|
|
273
241
|
}
|
|
242
|
+
if (writeCharacteristic != null && readCharacteristic != null) {
|
|
243
|
+
return {
|
|
244
|
+
service,
|
|
245
|
+
write: writeCharacteristic,
|
|
246
|
+
read: readCharacteristic,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// choose best characteristic
|
|
252
|
+
for (const service of options.services) {
|
|
253
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
254
|
+
continue;
|
|
274
255
|
}
|
|
275
|
-
|
|
256
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
257
|
+
// let readCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
|
|
258
|
+
// let writeCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
|
|
259
|
+
const pickedCharacteristic = characteristics.find(characteristic => {
|
|
260
|
+
const properties = characteristic.properties;
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
return properties.write && (properties.notify);
|
|
263
|
+
});
|
|
264
|
+
if (pickedCharacteristic) {
|
|
265
|
+
return {
|
|
266
|
+
service,
|
|
267
|
+
write: pickedCharacteristic,
|
|
268
|
+
read: pickedCharacteristic,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (this.options?.allowDetectDifferentCharacteristic ?? true) {
|
|
273
|
+
// choose different characteristic with write and read properties
|
|
276
274
|
for (const service of options.services) {
|
|
277
|
-
if (!TBluetoothHelpers.isAllowServices(service.uuid,
|
|
275
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
278
276
|
continue;
|
|
279
277
|
}
|
|
280
278
|
const characteristics = _characteristicsMap.get(service.uuid);
|
|
281
|
-
|
|
282
|
-
// let writeCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
|
|
283
|
-
const pickedCharacteristic = characteristics.find(characteristic => {
|
|
279
|
+
const writeCharacteristic = characteristics.find(characteristic => {
|
|
284
280
|
const properties = characteristic.properties;
|
|
285
281
|
// @ts-ignore
|
|
286
|
-
return properties.write
|
|
282
|
+
return properties.write;
|
|
283
|
+
});
|
|
284
|
+
const readCharacteristic = characteristics.find(characteristic => {
|
|
285
|
+
const properties = characteristic.properties;
|
|
286
|
+
return (properties.notify);
|
|
287
287
|
});
|
|
288
|
-
if (
|
|
288
|
+
if (writeCharacteristic && readCharacteristic) {
|
|
289
289
|
return {
|
|
290
290
|
service,
|
|
291
|
-
write:
|
|
292
|
-
read:
|
|
291
|
+
write: writeCharacteristic,
|
|
292
|
+
read: readCharacteristic,
|
|
293
293
|
};
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const characteristics = _characteristicsMap.get(service.uuid);
|
|
303
|
-
const writeCharacteristic = characteristics.find(characteristic => {
|
|
304
|
-
const properties = characteristic.properties;
|
|
305
|
-
// @ts-ignore
|
|
306
|
-
return properties.write;
|
|
307
|
-
});
|
|
308
|
-
const readCharacteristic = characteristics.find(characteristic => {
|
|
309
|
-
const properties = characteristic.properties;
|
|
310
|
-
return (properties.notify);
|
|
311
|
-
});
|
|
312
|
-
if (writeCharacteristic && readCharacteristic) {
|
|
313
|
-
return {
|
|
314
|
-
service,
|
|
315
|
-
write: writeCharacteristic,
|
|
316
|
-
read: readCharacteristic,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
// only write
|
|
299
|
+
for (const service of options.services) {
|
|
300
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, this.options?.allowServices)) {
|
|
301
|
+
continue;
|
|
319
302
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
return properties.write;
|
|
332
|
-
});
|
|
333
|
-
if (writeCharacteristic) {
|
|
334
|
-
return {
|
|
335
|
-
service,
|
|
336
|
-
write: writeCharacteristic,
|
|
337
|
-
};
|
|
338
|
-
}
|
|
303
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
304
|
+
const writeCharacteristic = characteristics.find(characteristic => {
|
|
305
|
+
const properties = characteristic.properties;
|
|
306
|
+
// @ts-ignore
|
|
307
|
+
return properties.write;
|
|
308
|
+
});
|
|
309
|
+
if (writeCharacteristic) {
|
|
310
|
+
return {
|
|
311
|
+
service,
|
|
312
|
+
write: writeCharacteristic,
|
|
313
|
+
};
|
|
339
314
|
}
|
|
340
315
|
}
|
|
341
|
-
}
|
|
316
|
+
}
|
|
342
317
|
}
|
|
343
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.4.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.4.0",
|
|
29
|
+
"@psdk/frame-father": "0.4.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": "13fa8cc6aacbf8f6bacb5f485da11a7610001f61"
|
|
39
37
|
}
|