@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.
@@ -1,20 +1,22 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
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
- import { ConnectionState } from "@psdk/frame-father";
11
- import Timeout from 'await-timeout';
12
- import Taro from "@tarojs/taro";
13
- export class TaroBleConnectedDevice {
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
- Taro.onBLECharacteristicValueChange((value) => __awaiter(this, void 0, void 0, function* () {
29
+ taro_1.default.onBLECharacteristicValueChange(async (value) => {
28
30
  // this.dataOfRead.push(value)
29
31
  if (this.notifyCallback) {
30
- yield this.notifyCallback(value);
32
+ await this.notifyCallback(value);
31
33
  }
32
- }));
33
- Taro.onBLEConnectionStateChange(value => {
34
- this._connectionState = value.connected ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
35
34
  });
36
- Taro.notifyBLECharacteristicValueChange({
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 __awaiter(this, void 0, void 0, function* () {
48
- return new Promise((resolve, reject) => {
49
- if (this.scpair.read == null) {
50
- reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
51
- return;
52
- }
53
- Taro.readBLECharacteristicValue({
54
- deviceId: this.device.deviceId,
55
- serviceId: this.scpair.service.uuid,
56
- characteristicId: this.scpair.read.uuid,
57
- success: result => {
58
- if (result.errMsg.indexOf('ok') == -1) {
59
- reject(result.errMsg);
60
- return;
61
- }
62
- resolve();
63
- },
64
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
81
- return new Promise((resolve, reject) => {
82
- Taro.closeBLEConnection({
83
- deviceId: this.device.deviceId,
84
- success: () => {
85
- this.device = null;
86
- this.scpair = null;
87
- resolve();
88
- },
89
- // @ts-ignore
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
- return __awaiter(this, void 0, void 0, function* () {
100
- var _a;
101
- if (this.dataOfRead.length > 0) {
102
- this.dataOfRead.splice(0, this.dataOfRead.length);
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
- yield this.realRead();
105
- const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000);
106
- const startTime = +new Date();
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 __awaiter(this, void 0, void 0, function* () {
124
- return new Promise((resolve, reject) => {
125
- Taro.writeBLECharacteristicValue({
126
- deviceId: this.device.deviceId,
127
- serviceId: this.scpair.service.uuid,
128
- characteristicId: this.scpair.write.uuid,
129
- // @ts-ignore
130
- value: data.buffer,
131
- success: result => {
132
- if (result.errMsg.indexOf('ok') == -1) {
133
- reject(result.errMsg);
134
- return;
135
- }
136
- resolve();
137
- },
138
- // @ts-ignore
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
- export * from './connected';
2
- export * from './provider';
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
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
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
- import { Jluetooth, JluetoothDevice, TBluetoothHelpers, } from "@psdk/device-bluetooth-traits";
11
- import { ConnectionState } from "@psdk/frame-father";
12
- import Timeout from 'await-timeout';
13
- import { TaroBleConnectedDevice } from "./connected";
14
- import Taro from "@tarojs/taro";
15
- export class TaroBleBluetooth extends Jluetooth {
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 __awaiter(this, void 0, void 0, function* () {
28
- return new Promise((resolve, reject) => {
29
- Taro.getBluetoothAdapterState({
30
- success: (r) => resolve(r),
31
- // @ts-ignore
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
- return __awaiter(this, void 0, void 0, function* () {
39
- var _a;
40
- if (this.isConnected()) {
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 = new TaroBleConnectedDevice({
58
- device: origin,
59
- scpair,
60
- });
61
- yield this.stopDiscovery();
62
- return Promise.resolve(this.connectedDevice);
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
- return __awaiter(this, void 0, void 0, function* () {
70
- yield this.requestLocation();
71
- yield this.closeBluetoothAdapter();
72
- yield Timeout.set(300);
73
- yield this.openBluetoothAdapter();
74
- const state = yield this.bluetoothAdapterState();
75
- if (!state.available) {
76
- yield Timeout.set(500);
77
- }
78
- if (state.discovering) {
79
- yield this.stopDiscovery();
80
- yield Timeout.set(500);
81
- }
82
- return new Promise((resolve, reject) => {
83
- Taro.startBluetoothDevicesDiscovery({
84
- // services: this.allowServices,
85
- allowDuplicatesKey: false,
86
- interval: 0,
87
- success: () => resolve(),
88
- // @ts-ignore
89
- fail: (e) => reject(e),
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 __awaiter(this, void 0, void 0, function* () {
97
- return new Promise((resolve, reject) => {
98
- Taro.stopBluetoothDevicesDiscovery({
99
- success: (result) => resolve(result),
100
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
109
- return new Promise((resolve, reject) => {
110
- Taro.openBluetoothAdapter({
111
- success: (ret) => {
112
- if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
113
- reject(ret);
114
- return;
115
- }
116
- resolve();
117
- },
118
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
126
- return new Promise((resolve, reject) => {
127
- Taro.closeBluetoothAdapter({
128
- success: (ret) => {
129
- if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
130
- reject(ret);
131
- return;
132
- }
133
- resolve();
134
- },
135
- // @ts-ignore
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
- Taro.onBluetoothDeviceFound((result) => __awaiter(this, void 0, void 0, function* () {
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 (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.allowNoName) !== null && _b !== void 0 ? _b : true;
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
- yield this.discoveredCallback(devices);
150
+ await this.discoveredCallback(devices);
165
151
  }
166
- }));
152
+ });
167
153
  }
168
- createBLEConnection(options) {
169
- return __awaiter(this, void 0, void 0, function* () {
170
- return new Promise((resolve, reject) => {
171
- Taro.createBLEConnection({
172
- deviceId: options.deviceId,
173
- timeout: options.timeout,
174
- success: () => resolve(),
175
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
183
- return new Promise((resolve, reject) => {
184
- Taro.getBLEDeviceServices({
185
- deviceId: options.deviceId,
186
- success: (result) => {
187
- if (result.errMsg.indexOf('ok') == -1) {
188
- reject(result.errMsg);
189
- return;
190
- }
191
- resolve(result.services);
192
- },
193
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
201
- return new Promise((resolve, reject) => {
202
- Taro.getBLEDeviceCharacteristics({
203
- deviceId: options.deviceId,
204
- serviceId: options.serviceId,
205
- success: (result) => {
206
- if (result.errMsg.indexOf('ok') == -1) {
207
- reject(result.errMsg);
208
- return;
209
- }
210
- resolve(result.characteristics);
211
- },
212
- // @ts-ignore
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 __awaiter(this, void 0, void 0, function* () {
220
- return new Promise((resolve, reject) => {
221
- // Taro.authorize({
222
- // scope: 'scope.userLocation',
223
- // success: () => resolve(),
224
- // // @ts-ignore
225
- // fail: (e) => reject(e),
226
- // });
227
- Taro.getLocation({
228
- success: () => resolve(),
229
- // @ts-ignore
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
- return __awaiter(this, void 0, void 0, function* () {
237
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
238
- // fill data
239
- const _characteristicsMap = new Map();
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
- const characteristics = yield this.getBLEDeviceCharacteristics({
242
- deviceId: options.deviceId,
243
- serviceId: service.uuid,
244
- });
245
- _characteristicsMap.set(service.uuid, characteristics);
246
- }
247
- // use special first
248
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowedWriteCharacteristic) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.allowedReadCharacteristic)) {
249
- for (const service of options.services) {
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
- if (writeCharacteristic != null && readCharacteristic != null) {
267
- return {
268
- service,
269
- write: writeCharacteristic,
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
- // choose best characteristic
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, (_f = this.options) === null || _f === void 0 ? void 0 : _f.allowServices)) {
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
- // let readCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
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 && (properties.notify);
282
+ return properties.write;
283
+ });
284
+ const readCharacteristic = characteristics.find(characteristic => {
285
+ const properties = characteristic.properties;
286
+ return (properties.notify);
287
287
  });
288
- if (pickedCharacteristic) {
288
+ if (writeCharacteristic && readCharacteristic) {
289
289
  return {
290
290
  service,
291
- write: pickedCharacteristic,
292
- read: pickedCharacteristic,
291
+ write: writeCharacteristic,
292
+ read: readCharacteristic,
293
293
  };
294
294
  }
295
295
  }
296
- if ((_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.allowDetectDifferentCharacteristic) !== null && _h !== void 0 ? _h : true) {
297
- // choose different characteristic with write and read properties
298
- for (const service of options.services) {
299
- if (!TBluetoothHelpers.isAllowServices(service.uuid, (_j = this.options) === null || _j === void 0 ? void 0 : _j.allowServices)) {
300
- continue;
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
- else {
322
- // only write
323
- for (const service of options.services) {
324
- if (!TBluetoothHelpers.isAllowServices(service.uuid, (_k = this.options) === null || _k === void 0 ? void 0 : _k.allowServices)) {
325
- continue;
326
- }
327
- const characteristics = _characteristicsMap.get(service.uuid);
328
- const writeCharacteristic = characteristics.find(characteristic => {
329
- const properties = characteristic.properties;
330
- // @ts-ignore
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.3",
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
- "@babel/core": "^7.24.5",
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
- "typedoc": "^0.25.13",
27
- "typescript": "^5.4.5"
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.3.3",
31
- "@psdk/frame-father": "0.3.3",
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": "20a3fc1bb587f8d627f9b1e05dd27240b7785dec"
36
+ "gitHead": "13fa8cc6aacbf8f6bacb5f485da11a7610001f61"
39
37
  }