@psdk/device-ble-taro 0.2.45 → 0.2.50

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.
@@ -1,27 +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
- }
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
27
  //# sourceMappingURL=connected.d.ts.map
@@ -1,143 +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;
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,3 +1,3 @@
1
- export * from './connected';
2
- export * from './provider';
1
+ export * from './connected';
2
+ export * from './provider';
3
3
  //# sourceMappingURL=index.d.ts.map
package/build/index.js CHANGED
@@ -1,18 +1,18 @@
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);
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);
@@ -1,24 +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
- }
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
24
  //# sourceMappingURL=provider.d.ts.map
package/build/provider.js CHANGED
@@ -1,341 +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;
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.45",
3
+ "version": "0.2.50",
4
4
  "description": "psdk",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -46,12 +46,13 @@
46
46
  "webpack-cli": "^4.9.2"
47
47
  },
48
48
  "dependencies": {
49
- "@psdk/frame-father": "0.2.45",
49
+ "@psdk/device-bluetooth-traits": "0.2.50",
50
+ "@psdk/frame-father": "0.2.50",
50
51
  "await-timeout": "^1.1.1"
51
52
  },
52
53
  "files": [
53
54
  "build",
54
55
  "!build/browser/psdk*"
55
56
  ],
56
- "gitHead": "f50371b7b1d1f3888c493e86bba5dbf8224a8d91"
57
+ "gitHead": "e8db29e8b9301cf1a88872503ebe411a0147e46d"
57
58
  }