New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@psdk/ohos-bluetooth-le

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@psdk/ohos-bluetooth-le - npm Package Compare versions

Comparing version 0.5.17 to 0.5.18

9

build/connected.d.ts
import { ConnectedDevice, ConnectionState, ReadOptions } from "@psdk/frame-father";
import ble from '@ohos.bluetooth.ble';
import { ProviderOptions } from "@psdk/device-bluetooth-traits";
export interface OblecdOptions extends ProviderOptions {
export interface OblecdOptions {
device: ble.GattClientDevice;
deviceName: string;
scpair: ServiceCharacteristicPair;
}

@@ -14,6 +15,4 @@ export interface ServiceCharacteristicPair {

private readonly options;
private scpair?;
private _connectionState;
private _mtu?;
private _deviceName?;
private dataOfRead;

@@ -23,2 +22,3 @@ private _notifyCallback?;

get device(): ble.GattClientDevice;
get scpair(): ServiceCharacteristicPair;
private initialize;

@@ -34,4 +34,3 @@ notify(callback: (data: Uint8Array) => Promise<void>): void;

write(data: Uint8Array): Promise<void>;
private findSCPair;
}
//# sourceMappingURL=connected.d.ts.map

@@ -1,17 +0,15 @@

"use strict";
// @ts-nocheck
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OhosBluetoothLEConnectedDevice = void 0;
const frame_father_1 = require("@psdk/frame-father");
const _ohos_bluetooth_ble_1 = __importDefault(require("@ohos.bluetooth.ble"));
const _ohos_bluetooth_constant_1 = __importDefault(require("@ohos.bluetooth.constant"));
const device_bluetooth_traits_1 = require("@psdk/device-bluetooth-traits");
class OhosBluetoothLEConnectedDevice {
import { CollectionKit, ConnectionState } from "@psdk/frame-father";
import ble from '@ohos.bluetooth.ble';
import constant from '@ohos.bluetooth.constant';
export class OhosBluetoothLEConnectedDevice {
options;
_connectionState;
_mtu;
dataOfRead;
_notifyCallback;
constructor(options) {
this.options = options;
this._mtu = 512;
this._connectionState = frame_father_1.ConnectionState.DISCONNECTED;
this._mtu = 20;
this._connectionState = ConnectionState.CONNECTED;
this.initialize();

@@ -22,4 +20,6 @@ }

}
get scpair() {
return this.options.scpair;
}
initialize() {
this.device.getDeviceName().then(name => this._deviceName = name);
this.device.on('BLEMtuChange', (mtu) => {

@@ -31,11 +31,8 @@ this._mtu = mtu;

switch (connectState) {
case _ohos_bluetooth_constant_1.default.ProfileConnectionState.STATE_DISCONNECTED: {
this._connectionState = frame_father_1.ConnectionState.DISCONNECTED;
case constant.ProfileConnectionState.STATE_DISCONNECTED: {
this._connectionState = ConnectionState.DISCONNECTED;
break;
}
case _ohos_bluetooth_constant_1.default.ProfileConnectionState.STATE_CONNECTED: {
this._connectionState = frame_father_1.ConnectionState.CONNECTED;
device.setBLEMtuSize(this._mtu);
let services = await this.device.getServices();
this.scpair = this.findSCPair({ services });
case constant.ProfileConnectionState.STATE_CONNECTED: {
this._connectionState = ConnectionState.CONNECTED;
break;

@@ -54,2 +51,3 @@ }

});
this.device.setBLEMtuSize(512);
}

@@ -60,13 +58,11 @@ notify(callback) {

canRead() {
var _a;
return ((_a = this.scpair) === null || _a === void 0 ? void 0 : _a.read) != null;
return this.scpair?.read != null;
}
connectionState() {
if (!this.scpair)
return frame_father_1.ConnectionState.DISCONNECTED;
return ConnectionState.DISCONNECTED;
return this._connectionState;
}
deviceName() {
var _a;
return (_a = this._deviceName) !== null && _a !== void 0 ? _a : '';
return this.options.deviceName ?? '';
}

@@ -89,111 +85,14 @@ disconnect() {

}
await this.device.writeCharacteristicValue({
serviceUuid: this.scpair.service.serviceUuid,
characteristicUuid: this.scpair.write.characteristicUuid,
characteristicValue: data.buffer,
descriptors: []
}, _ohos_bluetooth_ble_1.default.GattWriteType.WRITE);
}
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) {
let characteristics = service.characteristics;
_characteristicsMap.set(service.serviceUuid, characteristics);
const writeData = Array.from(data);
const parts = CollectionKit.split(writeData, this._mtu);
for (const part of parts) {
const partBinary = new Uint8Array(part);
await this.device.writeCharacteristicValue({
serviceUuid: this.scpair.service.serviceUuid,
characteristicUuid: this.scpair.write.characteristicUuid,
characteristicValue: partBinary.buffer,
descriptors: []
}, ble.GattWriteType.WRITE);
}
// 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 (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.serviceUuid, (_c = this.options) === null || _c === void 0 ? void 0 : _c.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
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.characteristicUuid.toLowerCase()) {
writeCharacteristic = characteristic;
}
}
for (const characteristic of characteristics) {
if (((_e = this.options) === null || _e === void 0 ? void 0 : _e.allowedReadCharacteristic.toLowerCase()) == characteristic.characteristicUuid.toLowerCase()) {
readCharacteristic = characteristic;
}
}
if (writeCharacteristic != null && readCharacteristic != null) {
return {
service,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
}
// choose best characteristic
for (const service of options.services) {
if (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.serviceUuid, (_f = this.options) === null || _f === void 0 ? void 0 : _f.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
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 (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.serviceUuid, (_j = this.options) === null || _j === void 0 ? void 0 : _j.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
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 (!device_bluetooth_traits_1.TBluetoothHelpers.isAllowServices(service.serviceUuid, (_k = this.options) === null || _k === void 0 ? void 0 : _k.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write;
});
if (writeCharacteristic) {
return {
service,
write: writeCharacteristic,
};
}
}
}
}
}
exports.OhosBluetoothLEConnectedDevice = OhosBluetoothLEConnectedDevice;

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

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./connected"), exports);
__exportStar(require("./provider"), exports);
export * from './connected';
export * from './provider';

@@ -17,3 +17,4 @@ import { BasicMessage, BluetoothAdapterState, ConnectOptions, Jluetooth, JluetoothDevice, ProviderOptions } from "@psdk/device-bluetooth-traits";

private onBluetoothDeviceFound;
private findSCPair;
}
//# sourceMappingURL=provider.d.ts.map

@@ -1,14 +0,13 @@

"use strict";
// @ts-nocheck
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OhosBluetoothLe = void 0;
const device_bluetooth_traits_1 = require("@psdk/device-bluetooth-traits");
const connected_1 = require("./connected");
const frame_father_1 = require("@psdk/frame-father");
const _ohos_bluetooth_ble_1 = __importDefault(require("@ohos.bluetooth.ble"));
const _ohos_bluetooth_access_1 = __importDefault(require("@ohos.bluetooth.access"));
class OhosBluetoothLe extends device_bluetooth_traits_1.Jluetooth {
import { Jluetooth, JluetoothDevice, TBluetoothHelpers, } from "@psdk/device-bluetooth-traits";
import { OhosBluetoothLEConnectedDevice } from "./connected";
import { ConnectionState } from "@psdk/frame-father";
import ble from '@ohos.bluetooth.ble';
import access from '@ohos.bluetooth.access';
import constant from '@ohos.bluetooth.constant';
export class OhosBluetoothLe extends Jluetooth {
options;
connectedDevice;
isDiscovering;
discoveredCallback;
constructor(options) {

@@ -21,17 +20,40 @@ super();

bluetoothAdapterState() {
const state = _ohos_bluetooth_access_1.default.getState();
const state = access.getState();
return {
discovering: this.isDiscovering,
available: state === _ohos_bluetooth_access_1.default.BluetoothState.STATE_ON || state === _ohos_bluetooth_access_1.default.BluetoothState.STATE_BLE_ON,
available: state === access.BluetoothState.STATE_ON || state === access.BluetoothState.STATE_BLE_ON,
};
}
async connect(device, options) {
const bleDevice = _ohos_bluetooth_ble_1.default.createGattClientDevice(device.deviceId);
bleDevice.connect();
this.connectedDevice = new connected_1.OhosBluetoothLEConnectedDevice({
...this.options,
device: bleDevice,
const bleDevice = ble.createGattClientDevice(device.deviceId);
return new Promise(async (resolve, reject) => {
bleDevice.on('BLEConnectionStateChange', async (state) => {
const connectState = state.state;
switch (connectState) {
case constant.ProfileConnectionState.STATE_DISCONNECTED: {
reject(null);
break;
}
case constant.ProfileConnectionState.STATE_CONNECTED: {
const deviceName = await bleDevice.getDeviceName();
let services = await bleDevice.getServices();
const scpair = await this.findSCPair({ services });
if (!scpair) {
return reject(`Can not get write characteristic by device: ${deviceName}`);
}
this.connectedDevice = new OhosBluetoothLEConnectedDevice({
...this.options,
device: bleDevice,
deviceName: deviceName,
scpair: scpair
});
bleDevice.off('BLEConnectionStateChange');
resolve(this.connectedDevice);
break;
}
}
});
bleDevice.connect();
await this.stopDiscovery();
});
await this.stopDiscovery();
return Promise.resolve(this.connectedDevice);
}

@@ -44,3 +66,3 @@ discovered(callback) {

return false;
return this.connectedDevice.connectionState() === frame_father_1.ConnectionState.CONNECTED;
return this.connectedDevice.connectionState() === ConnectionState.CONNECTED;
}

@@ -51,9 +73,10 @@ startDiscovery() {

interval: 500,
dutyMode: _ohos_bluetooth_ble_1.default.ScanDuty.SCAN_MODE_LOW_POWER,
matchMode: _ohos_bluetooth_ble_1.default.MatchMode.MATCH_MODE_AGGRESSIVE,
dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER,
matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE,
};
_ohos_bluetooth_ble_1.default.startBLEScan(null, scanOptions);
ble.startBLEScan(null, scanOptions);
return Promise.resolve();
}
stopDiscovery() {
_ohos_bluetooth_ble_1.default.stopBLEScan();
ble.stopBLEScan();
this.isDiscovering = false;

@@ -63,3 +86,3 @@ return Promise.resolve({});

onBluetoothDeviceFound() {
_ohos_bluetooth_ble_1.default.on("BLEDeviceFind", async (results) => {
ble.on("BLEDeviceFind", async (results) => {
if (!results.length) {

@@ -70,9 +93,8 @@ return;

.filter(item => {
var _a, _b;
const name = item.deviceName || item.name || item.localName;
if (name)
return true;
return (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.allowNoName) !== null && _b !== void 0 ? _b : true;
return this.options?.allowNoName ?? true;
})
.map(item => new device_bluetooth_traits_1.JluetoothDevice({
.map(item => new JluetoothDevice({
origin: item,

@@ -90,3 +112,102 @@ name: item.deviceName || item.name || item.localName,

}
async findSCPair(options) {
// fill data
const _characteristicsMap = new Map();
for (const service of options.services) {
let characteristics = service.characteristics;
_characteristicsMap.set(service.serviceUuid, characteristics);
}
// use special first
if (this.options?.allowedWriteCharacteristic && this.options?.allowedReadCharacteristic) {
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.serviceUuid, this.options?.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
let writeCharacteristic = null;
let readCharacteristic = null;
for (const characteristic of characteristics) {
if (this.options?.allowedWriteCharacteristic.toLowerCase() == characteristic.characteristicUuid.toLowerCase()) {
writeCharacteristic = characteristic;
}
}
for (const characteristic of characteristics) {
if (this.options?.allowedReadCharacteristic.toLowerCase() == characteristic.characteristicUuid.toLowerCase()) {
readCharacteristic = characteristic;
}
}
if (writeCharacteristic != null && readCharacteristic != null) {
return {
service,
write: writeCharacteristic,
read: readCharacteristic,
};
}
}
}
// choose best characteristic
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.serviceUuid, this.options?.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
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 (this.options?.allowDetectDifferentCharacteristic ?? true) {
// choose different characteristic with write and read properties
for (const service of options.services) {
if (!TBluetoothHelpers.isAllowServices(service.serviceUuid, this.options?.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
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.serviceUuid, this.options?.allowServices)) {
continue;
}
const characteristics = _characteristicsMap.get(service.serviceUuid);
const writeCharacteristic = characteristics.find(characteristic => {
const properties = characteristic.properties;
// @ts-ignore
return properties.write;
});
if (writeCharacteristic) {
return {
service,
write: writeCharacteristic,
};
}
}
}
}
}
exports.OhosBluetoothLe = OhosBluetoothLe;
{
"name": "@psdk/ohos-bluetooth-le",
"version": "0.5.17",
"version": "0.5.18",
"description": "psdk - ohos bluetooth le device support",

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

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

@@ -37,3 +37,3 @@ },

],
"gitHead": "46943a9d832ca94079de06670bc167c18feddf60"
"gitHead": "c1d3ad454ff82f138a744d59b38bb6b933edc389"
}

@@ -15,7 +15,14 @@ @psdk/ohos-bluetooth-le

bluetooth.discovered(async (devices) => {
// ...
//connect bluetooth
const connectDevice = await bluetooth.connect(device[0]);
//init device
PrinterUtil.getInstance().init(connectDevice);
//send data
const report = await PrinterUtil.getInstance().cpcl()
.raw(Raw.binary(Uint8Array.from([0x10, 0xff, 0xbf])))
.write();
});
//discovery
await bluetooth.startDiscovery();
```

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