@psdk/device-ble-taro 0.2.44 → 0.2.45
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.d.ts +27 -0
- package/build/connected.d.ts.map +1 -0
- package/build/connected.js +143 -0
- package/build/index.d.ts +2 -23
- package/build/index.d.ts.map +1 -1
- package/build/index.js +15 -103
- package/build/provider.d.ts +24 -0
- package/build/provider.d.ts.map +1 -0
- package/build/provider.js +341 -0
- package/package.json +3 -3
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ConnectedDevice, ConnectionState, ReadOptions } from "@psdk/frame-father";
|
|
2
|
+
import Taro from "@tarojs/taro";
|
|
3
|
+
export declare class TaroBleConnectedDevice implements ConnectedDevice {
|
|
4
|
+
private device;
|
|
5
|
+
private scpair;
|
|
6
|
+
private _connectionState;
|
|
7
|
+
private dataOfRead;
|
|
8
|
+
constructor(options: {
|
|
9
|
+
device: Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice;
|
|
10
|
+
scpair: ServiceCharacteristicPair;
|
|
11
|
+
});
|
|
12
|
+
private doListen;
|
|
13
|
+
private realRead;
|
|
14
|
+
connectionState(): ConnectionState;
|
|
15
|
+
deviceName(): string;
|
|
16
|
+
disconnect(): Promise<void>;
|
|
17
|
+
canRead(): boolean;
|
|
18
|
+
read(options?: ReadOptions): Promise<Uint8Array>;
|
|
19
|
+
write(data: Uint8Array): Promise<void>;
|
|
20
|
+
flush(): void;
|
|
21
|
+
}
|
|
22
|
+
export interface ServiceCharacteristicPair {
|
|
23
|
+
service: Taro.getBLEDeviceServices.BLEService;
|
|
24
|
+
write: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
|
|
25
|
+
read?: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=connected.d.ts.map
|
|
@@ -0,0 +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;IAC5D,OAAO,CAAC,MAAM,CAA4D;IAC1E,OAAO,CAAC,MAAM,CAA4B;IAE1C,OAAO,CAAC,gBAAgB,CAAkB;IAC1C,OAAO,CAAC,UAAU,CAA4D;gBAE3D,OAAO,EAAE;QAC1B,MAAM,EAAE,IAAI,CAAC,sBAAsB,CAAC,6BAA6B,CAAC;QAClE,MAAM,EAAE,yBAAyB,CAAC;KACnC;IAQD,OAAO,CAAC,QAAQ;YAkBF,QAAQ;IAuBtB,eAAe,IAAI,eAAe;IAIlC,UAAU,IAAI,MAAM;IAId,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAejC,OAAO,IAAI,OAAO;IAIZ,IAAI,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAsBhD,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB5C,KAAK,IAAI,IAAI;CAId;AAED,MAAM,WAAW,yBAAyB;IACxC,OAAO,EAAE,IAAI,CAAC,oBAAoB,CAAC,UAAU,CAAC;IAC9C,KAAK,EAAE,IAAI,CAAC,2BAA2B,CAAC,iBAAiB,CAAC;IAC1D,IAAI,CAAC,EAAE,IAAI,CAAC,2BAA2B,CAAC,iBAAiB,CAAC;CAC3D"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.TaroBleConnectedDevice = void 0;
|
|
16
|
+
const frame_father_1 = require("@psdk/frame-father");
|
|
17
|
+
const await_timeout_1 = __importDefault(require("await-timeout"));
|
|
18
|
+
const taro_1 = __importDefault(require("@tarojs/taro"));
|
|
19
|
+
class TaroBleConnectedDevice {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.device = options.device;
|
|
22
|
+
this.scpair = options.scpair;
|
|
23
|
+
this._connectionState = frame_father_1.ConnectionState.CONNECTED;
|
|
24
|
+
this.dataOfRead = [];
|
|
25
|
+
this.doListen();
|
|
26
|
+
}
|
|
27
|
+
doListen() {
|
|
28
|
+
taro_1.default.onBLECharacteristicValueChange(value => {
|
|
29
|
+
this.dataOfRead.push(value);
|
|
30
|
+
});
|
|
31
|
+
taro_1.default.onBLEConnectionStateChange(value => {
|
|
32
|
+
this._connectionState = value.connected ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
|
|
33
|
+
});
|
|
34
|
+
// Taro.notifyBLECharacteristicValueChange({
|
|
35
|
+
// deviceId: this.device.deviceId,
|
|
36
|
+
// serviceId: this.readPair.service.uuid,
|
|
37
|
+
// characteristicId: this.readPair.characteristic.uuid,
|
|
38
|
+
// state: true,
|
|
39
|
+
// success: result => console.log(result),
|
|
40
|
+
// // @ts-ignore
|
|
41
|
+
// fail: (e) => console.error(e),
|
|
42
|
+
// });
|
|
43
|
+
}
|
|
44
|
+
realRead() {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
if (this.scpair.read == null) {
|
|
48
|
+
reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
taro_1.default.readBLECharacteristicValue({
|
|
52
|
+
deviceId: this.device.deviceId,
|
|
53
|
+
serviceId: this.scpair.service.uuid,
|
|
54
|
+
characteristicId: this.scpair.read.uuid,
|
|
55
|
+
success: result => {
|
|
56
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
57
|
+
reject(result.errMsg);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
resolve();
|
|
61
|
+
},
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
fail: (e) => reject(e),
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
connectionState() {
|
|
69
|
+
return this._connectionState;
|
|
70
|
+
}
|
|
71
|
+
deviceName() {
|
|
72
|
+
return this.device.localName || this.device.name || 'NONE';
|
|
73
|
+
}
|
|
74
|
+
disconnect() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
taro_1.default.closeBLEConnection({
|
|
78
|
+
deviceId: this.device.deviceId,
|
|
79
|
+
success: () => {
|
|
80
|
+
this.device = null;
|
|
81
|
+
this.scpair = null;
|
|
82
|
+
resolve();
|
|
83
|
+
},
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
fail: (e) => reject(e),
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
canRead() {
|
|
91
|
+
return this.scpair.read != null;
|
|
92
|
+
}
|
|
93
|
+
read(options) {
|
|
94
|
+
var _a;
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
if (this.dataOfRead.length > 0) {
|
|
97
|
+
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
98
|
+
}
|
|
99
|
+
yield this.realRead();
|
|
100
|
+
const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000);
|
|
101
|
+
const startTime = +new Date();
|
|
102
|
+
while (true) {
|
|
103
|
+
const now = +new Date();
|
|
104
|
+
if (now - startTime > timeout) {
|
|
105
|
+
return new Uint8Array([]);
|
|
106
|
+
}
|
|
107
|
+
if (this.dataOfRead.length == 0) {
|
|
108
|
+
yield await_timeout_1.default.set(100);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const data = this.dataOfRead.pop();
|
|
112
|
+
const value = data.value;
|
|
113
|
+
return new Uint8Array(value);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
write(data) {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
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
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
flush() {
|
|
140
|
+
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.TaroBleConnectedDevice = TaroBleConnectedDevice;
|
package/build/index.d.ts
CHANGED
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export declare class TaroBleConnectedDevice implements ConnectedDevice {
|
|
4
|
-
private readonly device;
|
|
5
|
-
private readonly service;
|
|
6
|
-
private readonly characteristic;
|
|
7
|
-
private _connectionState;
|
|
8
|
-
private dataOfRead;
|
|
9
|
-
constructor(options: {
|
|
10
|
-
device: Taro.getBluetoothDevices.SuccessCallbackResultBlueToothDevice;
|
|
11
|
-
service: Taro.getBLEDeviceServices.BLEService;
|
|
12
|
-
characteristic: Taro.getBLEDeviceCharacteristics.BLECharacteristic;
|
|
13
|
-
});
|
|
14
|
-
private doListen;
|
|
15
|
-
private realRead;
|
|
16
|
-
connectionState(): ConnectionState;
|
|
17
|
-
deviceName(): string;
|
|
18
|
-
disconnect(): Promise<void>;
|
|
19
|
-
canRead(): boolean;
|
|
20
|
-
read(options?: ReadOptions): Promise<Uint8Array>;
|
|
21
|
-
write(data: Uint8Array): Promise<void>;
|
|
22
|
-
flush(): void;
|
|
23
|
-
}
|
|
1
|
+
export * from './connected';
|
|
2
|
+
export * from './provider';
|
|
24
3
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|
package/build/index.js
CHANGED
|
@@ -1,106 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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);
|
|
13
15
|
};
|
|
14
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports
|
|
16
|
-
|
|
17
|
-
const taro_1 = __importDefault(require("@tarojs/taro"));
|
|
18
|
-
const await_timeout_1 = __importDefault(require("await-timeout"));
|
|
19
|
-
class TaroBleConnectedDevice {
|
|
20
|
-
constructor(options) {
|
|
21
|
-
this.device = options.device;
|
|
22
|
-
this.service = options.service;
|
|
23
|
-
this.characteristic = options.characteristic;
|
|
24
|
-
this._connectionState = frame_father_1.ConnectionState.CONNECTED;
|
|
25
|
-
this.doListen();
|
|
26
|
-
}
|
|
27
|
-
doListen() {
|
|
28
|
-
taro_1.default.onBLECharacteristicValueChange(value => {
|
|
29
|
-
this.dataOfRead.push(value);
|
|
30
|
-
});
|
|
31
|
-
taro_1.default.onBLEConnectionStateChange(value => {
|
|
32
|
-
this._connectionState = value.connected ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
realRead() {
|
|
36
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
return new Promise((resolve, reject) => {
|
|
38
|
-
taro_1.default.readBLECharacteristicValue({
|
|
39
|
-
deviceId: this.device.deviceId,
|
|
40
|
-
serviceId: this.service.uuid,
|
|
41
|
-
characteristicId: this.characteristic.uuid,
|
|
42
|
-
success: res => resolve(),
|
|
43
|
-
fail: res => reject(),
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
connectionState() {
|
|
49
|
-
return this._connectionState;
|
|
50
|
-
}
|
|
51
|
-
deviceName() {
|
|
52
|
-
return this.device.localName || this.device.name || 'NONE';
|
|
53
|
-
}
|
|
54
|
-
disconnect() {
|
|
55
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
canRead() {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
read(options) {
|
|
62
|
-
var _a;
|
|
63
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
if (this.dataOfRead.length > 0) {
|
|
65
|
-
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
66
|
-
}
|
|
67
|
-
yield this.realRead();
|
|
68
|
-
const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 3) * 1000;
|
|
69
|
-
const startTime = +new Date();
|
|
70
|
-
while (true) {
|
|
71
|
-
const now = +new Date();
|
|
72
|
-
if (now - startTime > timeout) {
|
|
73
|
-
return new Uint8Array([]);
|
|
74
|
-
}
|
|
75
|
-
if (this.dataOfRead.length == 0) {
|
|
76
|
-
yield await_timeout_1.default.set(50);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
const data = this.dataOfRead.pop();
|
|
80
|
-
const value = data.value;
|
|
81
|
-
return new Uint8Array(value);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
write(data) {
|
|
86
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
return new Promise((resolve, reject) => {
|
|
88
|
-
taro_1.default.writeBLECharacteristicValue({
|
|
89
|
-
deviceId: this.device.deviceId,
|
|
90
|
-
serviceId: this.service.uuid,
|
|
91
|
-
characteristicId: this.characteristic.uuid,
|
|
92
|
-
value: data.buffer,
|
|
93
|
-
success: res => resolve(),
|
|
94
|
-
fail: res => {
|
|
95
|
-
console.error('Failed to write data:', res.errMsg);
|
|
96
|
-
reject(res.errMsg);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
flush() {
|
|
103
|
-
this.dataOfRead.splice(0, this.dataOfRead.length);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
exports.TaroBleConnectedDevice = TaroBleConnectedDevice;
|
|
17
|
+
__exportStar(require("./connected"), exports);
|
|
18
|
+
__exportStar(require("./provider"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BasicMessage, BluetoothAdapterState, ConnectOptions, Jluetooth, JluetoothDevice, ProviderOptions } from "@psdk/device-bluetooth-traits";
|
|
2
|
+
import { ConnectedDevice } from "@psdk/frame-father";
|
|
3
|
+
import Taro from "@tarojs/taro";
|
|
4
|
+
export declare class TaroBleBluetooth extends Jluetooth<Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice> {
|
|
5
|
+
private readonly options?;
|
|
6
|
+
private discoveredCallback?;
|
|
7
|
+
private connectedDevice?;
|
|
8
|
+
constructor(options?: ProviderOptions);
|
|
9
|
+
isConnected(): boolean;
|
|
10
|
+
bluetoothAdapterState(): Promise<BluetoothAdapterState>;
|
|
11
|
+
connect(device: JluetoothDevice<Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice>, options?: ConnectOptions): Promise<ConnectedDevice>;
|
|
12
|
+
discovered(callback: (devices: Array<JluetoothDevice<Taro.onBluetoothDeviceFound.CallbackResultBlueToothDevice>>) => Promise<void>): void;
|
|
13
|
+
startDiscovery(): Promise<void>;
|
|
14
|
+
stopDiscovery(): Promise<BasicMessage>;
|
|
15
|
+
private openBluetoothAdapter;
|
|
16
|
+
private closeBluetoothAdapter;
|
|
17
|
+
private onBluetoothDeviceFound;
|
|
18
|
+
private createBLEConnection;
|
|
19
|
+
private getBLEDeviceServices;
|
|
20
|
+
private getBLEDeviceCharacteristics;
|
|
21
|
+
private requestLocation;
|
|
22
|
+
private findSCPair;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +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,CAAyB;gBAErC,OAAO,CAAC,EAAE,eAAe;IAMrC,WAAW,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;IA2B3B,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;YA0BhB,mBAAmB;YAYnB,oBAAoB;YAkBpB,2BAA2B;YAmB3B,eAAe;YAgBf,UAAU;CAwGzB"}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.TaroBleBluetooth = void 0;
|
|
16
|
+
const device_bluetooth_traits_1 = require("@psdk/device-bluetooth-traits");
|
|
17
|
+
const frame_father_1 = require("@psdk/frame-father");
|
|
18
|
+
const await_timeout_1 = __importDefault(require("await-timeout"));
|
|
19
|
+
const connected_1 = require("./connected");
|
|
20
|
+
const taro_1 = __importDefault(require("@tarojs/taro"));
|
|
21
|
+
class TaroBleBluetooth extends device_bluetooth_traits_1.Jluetooth {
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super();
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.onBluetoothDeviceFound();
|
|
26
|
+
}
|
|
27
|
+
isConnected() {
|
|
28
|
+
if (!this.connectedDevice)
|
|
29
|
+
return false;
|
|
30
|
+
return this.connectedDevice.connectionState() === frame_father_1.ConnectionState.CONNECTED;
|
|
31
|
+
}
|
|
32
|
+
bluetoothAdapterState() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
taro_1.default.getBluetoothAdapterState({
|
|
36
|
+
success: (r) => resolve(r),
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
fail: (e) => reject(e)
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
connect(device, options) {
|
|
44
|
+
var _a;
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
if (this.isConnected()) {
|
|
47
|
+
if (!((_a = options === null || options === void 0 ? void 0 : options.autoSwitchDevice) !== null && _a !== void 0 ? _a : false)) {
|
|
48
|
+
return this.connectedDevice;
|
|
49
|
+
}
|
|
50
|
+
yield this.connectedDevice.disconnect();
|
|
51
|
+
}
|
|
52
|
+
const origin = device.origin;
|
|
53
|
+
yield this.createBLEConnection({ deviceId: origin.deviceId, timeout: options === null || options === void 0 ? void 0 : options.timeout, });
|
|
54
|
+
yield await_timeout_1.default.set(500);
|
|
55
|
+
const services = yield this.getBLEDeviceServices({ deviceId: origin.deviceId });
|
|
56
|
+
const scpair = yield this.findSCPair({
|
|
57
|
+
deviceId: origin.deviceId,
|
|
58
|
+
services,
|
|
59
|
+
});
|
|
60
|
+
if (!scpair) {
|
|
61
|
+
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
|
|
62
|
+
}
|
|
63
|
+
this.connectedDevice = new connected_1.TaroBleConnectedDevice({
|
|
64
|
+
device: origin,
|
|
65
|
+
scpair,
|
|
66
|
+
});
|
|
67
|
+
yield this.stopDiscovery();
|
|
68
|
+
return Promise.resolve(this.connectedDevice);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
discovered(callback) {
|
|
72
|
+
this.discoveredCallback = callback;
|
|
73
|
+
}
|
|
74
|
+
startDiscovery() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
yield this.requestLocation();
|
|
77
|
+
yield this.closeBluetoothAdapter();
|
|
78
|
+
yield await_timeout_1.default.set(300);
|
|
79
|
+
yield this.openBluetoothAdapter();
|
|
80
|
+
const state = yield this.bluetoothAdapterState();
|
|
81
|
+
if (!state.available) {
|
|
82
|
+
yield await_timeout_1.default.set(500);
|
|
83
|
+
}
|
|
84
|
+
if (state.discovering) {
|
|
85
|
+
yield this.stopDiscovery();
|
|
86
|
+
yield await_timeout_1.default.set(500);
|
|
87
|
+
}
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
taro_1.default.startBluetoothDevicesDiscovery({
|
|
90
|
+
// services: this.allowServices,
|
|
91
|
+
allowDuplicatesKey: false,
|
|
92
|
+
interval: 0,
|
|
93
|
+
success: () => resolve(),
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
fail: (e) => reject(e),
|
|
96
|
+
// complete: () => resolve(),
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
stopDiscovery() {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
taro_1.default.stopBluetoothDevicesDiscovery({
|
|
105
|
+
success: (result) => resolve(result),
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
fail: (e) => reject(e),
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// ### inner function
|
|
113
|
+
openBluetoothAdapter() {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
taro_1.default.openBluetoothAdapter({
|
|
117
|
+
success: (ret) => {
|
|
118
|
+
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
119
|
+
reject(ret);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
resolve();
|
|
123
|
+
},
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
fail: (e) => reject(e)
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
closeBluetoothAdapter() {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
taro_1.default.closeBluetoothAdapter({
|
|
134
|
+
success: (ret) => {
|
|
135
|
+
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
|
|
136
|
+
reject(ret);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
resolve();
|
|
140
|
+
},
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
fail: (e) => reject(e)
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
onBluetoothDeviceFound() {
|
|
148
|
+
taro_1.default.onBluetoothDeviceFound((result) => __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
const foundDevices = result.devices;
|
|
150
|
+
if (!foundDevices.length) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const devices = foundDevices
|
|
154
|
+
.filter(item => {
|
|
155
|
+
var _a, _b;
|
|
156
|
+
const name = item.name || item.localName;
|
|
157
|
+
if (name)
|
|
158
|
+
return true;
|
|
159
|
+
return (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.allowNoName) !== null && _b !== void 0 ? _b : true;
|
|
160
|
+
})
|
|
161
|
+
.map(item => new device_bluetooth_traits_1.JluetoothDevice({
|
|
162
|
+
origin: item,
|
|
163
|
+
name: item.name || item.localName,
|
|
164
|
+
deviceId: item.deviceId,
|
|
165
|
+
}));
|
|
166
|
+
if (!devices.length) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (this.discoveredCallback) {
|
|
170
|
+
yield this.discoveredCallback(devices);
|
|
171
|
+
}
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
createBLEConnection(options) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
taro_1.default.createBLEConnection({
|
|
178
|
+
deviceId: options.deviceId,
|
|
179
|
+
timeout: options.timeout,
|
|
180
|
+
success: () => resolve(),
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
fail: (e) => reject(e),
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
getBLEDeviceServices(options) {
|
|
188
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
189
|
+
return new Promise((resolve, reject) => {
|
|
190
|
+
taro_1.default.getBLEDeviceServices({
|
|
191
|
+
deviceId: options.deviceId,
|
|
192
|
+
success: (result) => {
|
|
193
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
194
|
+
reject(result.errMsg);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
resolve(result.services);
|
|
198
|
+
},
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
fail: (e) => reject(e),
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
getBLEDeviceCharacteristics(options) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
taro_1.default.getBLEDeviceCharacteristics({
|
|
209
|
+
deviceId: options.deviceId,
|
|
210
|
+
serviceId: options.serviceId,
|
|
211
|
+
success: (result) => {
|
|
212
|
+
if (result.errMsg.indexOf('ok') == -1) {
|
|
213
|
+
reject(result.errMsg);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
resolve(result.characteristics);
|
|
217
|
+
},
|
|
218
|
+
// @ts-ignore
|
|
219
|
+
fail: (e) => reject(e),
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
requestLocation() {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
return new Promise((resolve, reject) => {
|
|
227
|
+
// Taro.authorize({
|
|
228
|
+
// scope: 'scope.userLocation',
|
|
229
|
+
// success: () => resolve(),
|
|
230
|
+
// // @ts-ignore
|
|
231
|
+
// fail: (e) => reject(e),
|
|
232
|
+
// });
|
|
233
|
+
taro_1.default.getLocation({
|
|
234
|
+
success: () => resolve(),
|
|
235
|
+
// @ts-ignore
|
|
236
|
+
fail: (e) => reject(e),
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
findSCPair(options) {
|
|
242
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
// fill data
|
|
245
|
+
const _characteristicsMap = new Map();
|
|
246
|
+
for (const service of options.services) {
|
|
247
|
+
const characteristics = yield this.getBLEDeviceCharacteristics({
|
|
248
|
+
deviceId: options.deviceId,
|
|
249
|
+
serviceId: service.uuid,
|
|
250
|
+
});
|
|
251
|
+
_characteristicsMap.set(service.uuid, characteristics);
|
|
252
|
+
}
|
|
253
|
+
// use special first
|
|
254
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowCharacteristic) {
|
|
255
|
+
for (const service of options.services) {
|
|
256
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_b = this.options) === null || _b === void 0 ? void 0 : _b.allowServices)) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
260
|
+
for (const characteristic of characteristics) {
|
|
261
|
+
if (((_c = this.options) === null || _c === void 0 ? void 0 : _c.allowCharacteristic) != characteristic.uuid)
|
|
262
|
+
continue;
|
|
263
|
+
const properties = characteristic.properties;
|
|
264
|
+
return {
|
|
265
|
+
service,
|
|
266
|
+
write: characteristic,
|
|
267
|
+
read: properties.read ? characteristic : null,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// choose best characteristic
|
|
273
|
+
for (const service of options.services) {
|
|
274
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_d = this.options) === null || _d === void 0 ? void 0 : _d.allowServices)) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
278
|
+
// let readCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
|
|
279
|
+
// let writeCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
|
|
280
|
+
const pickedCharacteristic = characteristics.find(characteristic => {
|
|
281
|
+
const properties = characteristic.properties;
|
|
282
|
+
// @ts-ignore
|
|
283
|
+
return (properties.writeNoResponse || properties.write) && (properties.read);
|
|
284
|
+
});
|
|
285
|
+
if (pickedCharacteristic) {
|
|
286
|
+
return {
|
|
287
|
+
service,
|
|
288
|
+
write: pickedCharacteristic,
|
|
289
|
+
read: pickedCharacteristic,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if ((_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.allowDetectDifferentCharacteristic) !== null && _f !== void 0 ? _f : false) {
|
|
294
|
+
// choose different characteristic with write and read properties
|
|
295
|
+
for (const service of options.services) {
|
|
296
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_g = this.options) === null || _g === void 0 ? void 0 : _g.allowServices)) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
300
|
+
const writeCharacteristic = characteristics.find(characteristic => {
|
|
301
|
+
const properties = characteristic.properties;
|
|
302
|
+
// @ts-ignore
|
|
303
|
+
return (properties.writeNoResponse || properties.write);
|
|
304
|
+
});
|
|
305
|
+
const readCharacteristic = characteristics.find(characteristic => {
|
|
306
|
+
const properties = characteristic.properties;
|
|
307
|
+
return (properties.read);
|
|
308
|
+
});
|
|
309
|
+
if (writeCharacteristic && readCharacteristic) {
|
|
310
|
+
return {
|
|
311
|
+
service,
|
|
312
|
+
write: writeCharacteristic,
|
|
313
|
+
read: readCharacteristic,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
// only write
|
|
320
|
+
for (const service of options.services) {
|
|
321
|
+
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.uuid, (_h = this.options) === null || _h === void 0 ? void 0 : _h.allowServices)) {
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
const characteristics = _characteristicsMap.get(service.uuid);
|
|
325
|
+
const writeCharacteristic = characteristics.find(characteristic => {
|
|
326
|
+
const properties = characteristic.properties;
|
|
327
|
+
// @ts-ignore
|
|
328
|
+
return (properties.writeNoResponse || properties.write);
|
|
329
|
+
});
|
|
330
|
+
if (writeCharacteristic) {
|
|
331
|
+
return {
|
|
332
|
+
service,
|
|
333
|
+
write: writeCharacteristic,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
exports.TaroBleBluetooth = TaroBleBluetooth;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdk/device-ble-taro",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.45",
|
|
4
4
|
"description": "psdk",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"webpack-cli": "^4.9.2"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@psdk/frame-father": "0.2.
|
|
49
|
+
"@psdk/frame-father": "0.2.45",
|
|
50
50
|
"await-timeout": "^1.1.1"
|
|
51
51
|
},
|
|
52
52
|
"files": [
|
|
53
53
|
"build",
|
|
54
54
|
"!build/browser/psdk*"
|
|
55
55
|
],
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "f50371b7b1d1f3888c493e86bba5dbf8224a8d91"
|
|
57
57
|
}
|