Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@psdk/device-ble-wechat

Package Overview
Dependencies
Maintainers
0
Versions
92
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@psdk/device-ble-wechat - npm Package Compare versions

Comparing version 0.3.3 to 0.3.4

1

build/connected.d.ts

@@ -1,2 +0,1 @@

/// <reference types="wechat-miniprogram" />
import { ConnectedDevice, ConnectionState, ReadOptions } from "@psdk/frame-father";

@@ -3,0 +2,0 @@ export declare class WechatBleConnectedDevice implements ConnectedDevice {

155

build/connected.js

@@ -1,10 +0,1 @@

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { ConnectionState } from "@psdk/frame-father";

@@ -26,8 +17,8 @@ import Timeout from 'await-timeout';

return;
wx.onBLECharacteristicValueChange((value) => __awaiter(this, void 0, void 0, function* () {
wx.onBLECharacteristicValueChange(async (value) => {
// this.dataOfRead.push(value)
if (this.notifyCallback) {
yield this.notifyCallback(value);
await this.notifyCallback(value);
}
}));
});
wx.onBLEConnectionStateChange(value => {

@@ -46,23 +37,21 @@ this._connectionState = value.connected ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;

}
realRead() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
if (this.scpair.read == null) {
reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
return;
}
wx.readBLECharacteristicValue({
deviceId: this.device.deviceId,
serviceId: this.scpair.service.uuid,
characteristicId: this.scpair.read.uuid,
success: result => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e),
});
async realRead() {
return new Promise((resolve, reject) => {
if (this.scpair.read == null) {
reject(`the connect device not support read operation: service id: [${this.scpair.service.uuid}]`);
return;
}
wx.readBLECharacteristicValue({
deviceId: this.device.deviceId,
serviceId: this.scpair.service.uuid,
characteristicId: this.scpair.read.uuid,
success: result => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e),
});

@@ -80,15 +69,13 @@ });

}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.closeBLEConnection({
deviceId: this.device.deviceId,
success: () => {
this.device = null;
this.scpair = null;
resolve();
},
// @ts-ignore
fail: (e) => reject(e),
});
async disconnect() {
return new Promise((resolve, reject) => {
wx.closeBLEConnection({
deviceId: this.device.deviceId,
success: () => {
this.device = null;
this.scpair = null;
resolve();
},
// @ts-ignore
fail: (e) => reject(e),
});

@@ -100,45 +87,41 @@ });

}
read(options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (this.dataOfRead.length > 0) {
this.dataOfRead.splice(0, this.dataOfRead.length);
async read(options) {
var _a;
if (this.dataOfRead.length > 0) {
this.dataOfRead.splice(0, this.dataOfRead.length);
}
await this.realRead();
const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000);
const startTime = +new Date();
while (true) {
const now = +new Date();
if (now - startTime > timeout) {
return new Uint8Array([]);
}
yield this.realRead();
const timeout = ((_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000);
const startTime = +new Date();
while (true) {
const now = +new Date();
if (now - startTime > timeout) {
return new Uint8Array([]);
}
if (this.dataOfRead.length == 0) {
yield Timeout.set(100);
continue;
}
const data = this.dataOfRead.pop();
const value = data.value;
return new Uint8Array(value);
if (this.dataOfRead.length == 0) {
await Timeout.set(100);
continue;
}
});
const data = this.dataOfRead.pop();
const value = data.value;
return new Uint8Array(value);
}
}
write(data) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.writeBLECharacteristicValue({
deviceId: this.device.deviceId,
serviceId: this.scpair.service.uuid,
characteristicId: this.scpair.write.uuid,
// @ts-ignore
value: data.buffer,
success: result => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});
async write(data) {
return new Promise((resolve, reject) => {
wx.writeBLECharacteristicValue({
deviceId: this.device.deviceId,
serviceId: this.scpair.service.uuid,
characteristicId: this.scpair.write.uuid,
// @ts-ignore
value: data.buffer,
success: result => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});

@@ -145,0 +128,0 @@ });

@@ -1,2 +0,1 @@

/// <reference types="wechat-miniprogram" />
import { BasicMessage, BluetoothAdapterState, ConnectOptions, Jluetooth, JluetoothDevice, ProviderOptions } from "@psdk/device-bluetooth-traits";

@@ -3,0 +2,0 @@ import { ConnectedDevice } from "@psdk/frame-father";

@@ -1,10 +0,1 @@

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Jluetooth, JluetoothDevice, TBluetoothHelpers, } from "@psdk/device-bluetooth-traits";

@@ -34,29 +25,27 @@ import { ConnectionState } from "@psdk/frame-father";

}
connect(device, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (this.isConnected()) {
if (!((_a = options === null || options === void 0 ? void 0 : options.autoSwitchDevice) !== null && _a !== void 0 ? _a : false)) {
return this.connectedDevice;
}
yield this.connectedDevice.disconnect();
async connect(device, options) {
var _a;
if (this.isConnected()) {
if (!((_a = options === null || options === void 0 ? void 0 : options.autoSwitchDevice) !== null && _a !== void 0 ? _a : false)) {
return this.connectedDevice;
}
const origin = device.origin;
yield this.createBLEConnection({ deviceId: origin.deviceId, timeout: options === null || options === void 0 ? void 0 : options.timeout, });
yield Timeout.set(500);
const services = yield this.getBLEDeviceServices({ deviceId: origin.deviceId });
const scpair = yield this.findSCPair({
deviceId: origin.deviceId,
services,
});
if (!scpair) {
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
}
this.connectedDevice = new WechatBleConnectedDevice({
device: origin,
scpair,
});
yield this.stopDiscovery();
return Promise.resolve(this.connectedDevice);
await this.connectedDevice.disconnect();
}
const origin = device.origin;
await this.createBLEConnection({ deviceId: origin.deviceId, timeout: options === null || options === void 0 ? void 0 : options.timeout, });
await Timeout.set(500);
const services = await this.getBLEDeviceServices({ deviceId: origin.deviceId });
const scpair = await this.findSCPair({
deviceId: origin.deviceId,
services,
});
if (!scpair) {
return Promise.reject(`Can not get write characteristic by device: ${device.name}`);
}
this.connectedDevice = new WechatBleConnectedDevice({
device: origin,
scpair,
});
await this.stopDiscovery();
return Promise.resolve(this.connectedDevice);
}

@@ -66,26 +55,24 @@ discovered(callback) {

}
startDiscovery() {
return __awaiter(this, void 0, void 0, function* () {
yield this.requestLocation();
yield this.closeBluetoothAdapter();
yield Timeout.set(300);
yield this.openBluetoothAdapter();
const state = yield this.bluetoothAdapterState();
if (!state.available) {
yield Timeout.set(500);
}
if (state.discovering) {
yield this.stopDiscovery();
yield Timeout.set(500);
}
return new Promise((resolve, reject) => {
wx.startBluetoothDevicesDiscovery({
// services: this.allowServices,
allowDuplicatesKey: false,
interval: 0,
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
// complete: () => resolve(),
});
async startDiscovery() {
await this.requestLocation();
await this.closeBluetoothAdapter();
await Timeout.set(300);
await this.openBluetoothAdapter();
const state = await this.bluetoothAdapterState();
if (!state.available) {
await Timeout.set(500);
}
if (state.discovering) {
await this.stopDiscovery();
await Timeout.set(500);
}
return new Promise((resolve, reject) => {
wx.startBluetoothDevicesDiscovery({
// services: this.allowServices,
allowDuplicatesKey: false,
interval: 0,
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
// complete: () => resolve(),
});

@@ -104,35 +91,31 @@ });

// ### inner function
openBluetoothAdapter() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.openBluetoothAdapter({
// @ts-ignore
success: (ret) => {
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
reject(ret);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});
async openBluetoothAdapter() {
return new Promise((resolve, reject) => {
wx.openBluetoothAdapter({
// @ts-ignore
success: (ret) => {
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
reject(ret);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});
});
}
closeBluetoothAdapter() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.closeBluetoothAdapter({
// @ts-ignore
success: (ret) => {
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
reject(ret);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});
async closeBluetoothAdapter() {
return new Promise((resolve, reject) => {
wx.closeBluetoothAdapter({
// @ts-ignore
success: (ret) => {
if (ret.errMsg && ret.errMsg.indexOf('ok') == -1) {
reject(ret);
return;
}
resolve();
},
// @ts-ignore
fail: (e) => reject(e)
});

@@ -142,3 +125,3 @@ });

onBluetoothDeviceFound() {
wx.onBluetoothDeviceFound((result) => __awaiter(this, void 0, void 0, function* () {
wx.onBluetoothDeviceFound(async (result) => {
const foundDevices = result.devices;

@@ -165,181 +148,171 @@ if (!foundDevices.length) {

if (this.discoveredCallback) {
yield this.discoveredCallback(devices);
await this.discoveredCallback(devices);
}
}));
});
}
createBLEConnection(options) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.createBLEConnection({
deviceId: options.deviceId,
timeout: options.timeout,
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
});
async createBLEConnection(options) {
return new Promise((resolve, reject) => {
wx.createBLEConnection({
deviceId: options.deviceId,
timeout: options.timeout,
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
});
});
}
getBLEDeviceServices(options) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.getBLEDeviceServices({
deviceId: options.deviceId,
success: (result) => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve(result.services);
},
// @ts-ignore
fail: (e) => reject(e),
});
async getBLEDeviceServices(options) {
return new Promise((resolve, reject) => {
wx.getBLEDeviceServices({
deviceId: options.deviceId,
success: (result) => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve(result.services);
},
// @ts-ignore
fail: (e) => reject(e),
});
});
}
getBLEDeviceCharacteristics(options) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
wx.getBLEDeviceCharacteristics({
deviceId: options.deviceId,
serviceId: options.serviceId,
success: (result) => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve(result.characteristics);
},
// @ts-ignore
fail: (e) => reject(e),
});
async getBLEDeviceCharacteristics(options) {
return new Promise((resolve, reject) => {
wx.getBLEDeviceCharacteristics({
deviceId: options.deviceId,
serviceId: options.serviceId,
success: (result) => {
if (result.errMsg.indexOf('ok') == -1) {
reject(result.errMsg);
return;
}
resolve(result.characteristics);
},
// @ts-ignore
fail: (e) => reject(e),
});
});
}
requestLocation() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
// wx.authorize({
// scope: 'scope.userLocation',
// success: () => resolve(),
// // @ts-ignore
// fail: (e) => reject(e),
// });
wx.getLocation({
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
});
async requestLocation() {
return new Promise((resolve, reject) => {
// wx.authorize({
// scope: 'scope.userLocation',
// success: () => resolve(),
// // @ts-ignore
// fail: (e) => reject(e),
// });
wx.getLocation({
success: () => resolve(),
// @ts-ignore
fail: (e) => reject(e),
});
});
}
findSCPair(options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
// fill data
const _characteristicsMap = new Map();
async findSCPair(options) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
// fill data
const _characteristicsMap = new Map();
for (const service of options.services) {
const characteristics = await this.getBLEDeviceCharacteristics({
deviceId: options.deviceId,
serviceId: service.uuid,
});
_characteristicsMap.set(service.uuid, characteristics);
}
// use special first
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowedWriteCharacteristic) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.allowedReadCharacteristic)) {
for (const service of options.services) {
const characteristics = yield this.getBLEDeviceCharacteristics({
deviceId: options.deviceId,
serviceId: service.uuid,
});
_characteristicsMap.set(service.uuid, characteristics);
}
// use special first
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowedWriteCharacteristic) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.allowedReadCharacteristic)) {
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_c = this.options) === null || _c === void 0 ? void 0 : _c.allowServices)) {
continue;
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_c = this.options) === null || _c === void 0 ? void 0 : _c.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.uuid);
let writeCharacteristic = null;
let readCharacteristic = null;
for (const characteristic of characteristics) {
if (((_d = this.options) === null || _d === void 0 ? void 0 : _d.allowedWriteCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
writeCharacteristic = characteristic;
}
const characteristics = _characteristicsMap.get(service.uuid);
let writeCharacteristic = null;
let readCharacteristic = null;
for (const characteristic of characteristics) {
if (((_d = this.options) === null || _d === void 0 ? void 0 : _d.allowedWriteCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
writeCharacteristic = characteristic;
}
}
for (const characteristic of characteristics) {
if (((_e = this.options) === null || _e === void 0 ? void 0 : _e.allowedReadCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
readCharacteristic = characteristic;
}
for (const characteristic of characteristics) {
if (((_e = this.options) === null || _e === void 0 ? void 0 : _e.allowedReadCharacteristic.toLowerCase()) == characteristic.uuid.toLowerCase()) {
readCharacteristic = characteristic;
}
}
if (writeCharacteristic != null && readCharacteristic != null) {
return {
service,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
if (writeCharacteristic != null && readCharacteristic != null) {
return {
service,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
// choose best characteristic
}
// choose best characteristic
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_f = this.options) === null || _f === void 0 ? void 0 : _f.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.uuid);
// let readCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
// let writeCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
const pickedCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write && (properties.notify);
});
if (pickedCharacteristic) {
return {
service,
write: pickedCharacteristic,
read: pickedCharacteristic,
};
}
}
if ((_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.allowDetectDifferentCharacteristic) !== null && _h !== void 0 ? _h : true) {
// choose different characteristic with write and read properties
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_f = this.options) === null || _f === void 0 ? void 0 : _f.allowServices)) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_j = this.options) === null || _j === void 0 ? void 0 : _j.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.uuid);
// let readCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
// let writeCharacteristic: GetBLEDeviceCharacteristicsSuccessData;
const pickedCharacteristic = characteristics.find(characteristic => {
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write && (properties.notify);
return properties.write;
});
if (pickedCharacteristic) {
const readCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
return (properties.notify);
});
if (writeCharacteristic && readCharacteristic) {
return {
service,
write: pickedCharacteristic,
read: pickedCharacteristic,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
if ((_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.allowDetectDifferentCharacteristic) !== null && _h !== void 0 ? _h : true) {
// choose different characteristic with write and read properties
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_j = this.options) === null || _j === void 0 ? void 0 : _j.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.uuid);
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write;
});
const readCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
return (properties.notify);
});
if (writeCharacteristic && readCharacteristic) {
return {
service,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
else {
// only write
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_k = this.options) === null || _k === void 0 ? void 0 : _k.allowServices)) {
continue;
}
}
else {
// only write
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.uuid, (_k = this.options) === null || _k === void 0 ? void 0 : _k.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.uuid);
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write;
});
if (writeCharacteristic) {
return {
service,
write: writeCharacteristic,
};
}
const characteristics = _characteristicsMap.get(service.uuid);
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write;
});
if (writeCharacteristic) {
return {
service,
write: writeCharacteristic,
};
}
}
});
}
}
}
{
"name": "@psdk/device-ble-wechat",
"version": "0.3.3",
"version": "0.3.4",
"description": "psdk",

@@ -30,4 +30,4 @@ "main": "build/index.js",

"dependencies": {
"@psdk/device-bluetooth-traits": "0.3.3",
"@psdk/frame-father": "0.3.3",
"@psdk/device-bluetooth-traits": "0.3.4",
"@psdk/frame-father": "0.3.4",
"await-timeout": "^1.1.1"

@@ -39,3 +39,3 @@ },

],
"gitHead": "20a3fc1bb587f8d627f9b1e05dd27240b7785dec"
"gitHead": "b8c2fdcb52f1bd3e9d75d004d37d717bd465b4c9"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc