1.0.3 • Published 7 months ago

cordova-plugin-dzble v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

cordova-plugin-dzble

安装插件

cordova plugin cordova-plugin-dzble

使用插件

安装完插件后,直接通过全局变量 dzBle 即可调用插件所提供的 BLE 相关方法。

// eg: 搜索打印机列表
dzBle.startBleDeviceDiscovery((resp) => {
  if (resp.status === 0) {
    console.log("搜索BLE设备启动成功!");
  } else if (resp.status === 2) {
    console.log("BLE设备搜索完毕!");
  } else if (resp.status > 2) {
    console.log("搜索BLE设备启动失败!");
  }
});

插件接口介绍

export interface BleDevice {
    /**
     * 设备名称
     */
    name: string;
    /**
     * 设备ID
     */
    deviceId: string;
    /**
     * 设备信号强度
     */
    rssi: number;
}
export interface IBleConnectResult {
    /**
     * mac地址;
     */
    deviceId: string;
    /**
     * 链接状态,
     * 0:连接成功;
     * 1:链接失败;
     * 2:链接断开;
     */
    status: number;
}
export interface IBleDiscoveryResult {
    /**
     * 搜索到的蓝牙设备列表。
     */
    devices?: BleDevice[];
    /**
     * 0 start scan;
     * 1 scanning;
     * 2 scanFinish,
     * 3 failed
     */
    status: number;
}
export interface IBleGattCharacter {
    /**
     * 特征值读写特性。
     */
    properties: string;
    /**
     * 特征值UUID。
     */
    uuid: string;
}
export interface IReadBleResults {
    deviceId: string;
    serviceId: string;
    characteristicId: string;
    /**
     * 读取结果状态码:
     * 0:读取成功;
     * 1:读取失败;
     */
    status: number;
    /**
     * 读取到的十六进制字符串;
     */
    value: string;
}
export interface IWriteBleResults {
    deviceId: string;
    serviceId: string;
    characteristicId: string;
    /**
     * 写入状态:
     * 0:写入成功;
     * 其他:写入失败;
     */
    status: number;
}
export interface INotifyBleResults {
    deviceId: string;
    serviceId: string;
    characteristicId: string;
    /**
     * notify通道侦听状态:
     * 0:启用成功;
     * 1:启用失败;
     * 2:侦测到数据;
     */
    status: number;
    /**
     * 侦测到的十六进制字符串数据;
     */
    value: string;
}
export declare class BLEAdapter {
    private static _instance?;
    static getInstance(): BLEAdapter;
    constructor();
    /**
     * 请求蓝牙设备权限
     *
     * @param {(resp: {status: number, message?: string}) => void} callback 请求回调函数。
     *
     * @param {number} callback.resp.status  响应状态码:
     *          0:请求成功;
     *          1:请求失败;
     * @param {string|undefined} callback.resp.message 如果请求失败,则表示失败的权限名称;
     */
    requestBluetoothAuth(callback: (resp: {
        status: number;
        message?: string;
    }) => void): void;
    /**
     * 搜索蓝牙设备列表
     *
     * @param {(resp: IBleDiscoveryResult) => void} callback 搜索结果回调函数;
     *
     * @param {IBleDiscoveryResult} callback.resp 回调参数;
     * @param {number} callback.resp.status 响应状态码:
     *      0:蓝牙搜索开启成功,开始搜索蓝牙设备
     *      1:搜索到蓝牙设备;
     *      2:蓝牙搜索结束;
     *      3:蓝牙搜索开启失败;
     * @param {BleDevice} callback.resp.devices 搜索到的蓝牙设备列表;
     */
    startBleDeviceDiscovery(callback: (resp: IBleDiscoveryResult) => void): void;
    /**
     * 停止蓝牙搜索操作。
     *
     * @param {(success: boolean) => void} callback 停止搜索回调接口。
     * @param {boolean} callback.success 停止成功与否。
     */
    stopBleDeviceDiscovery(callback: (success: boolean) => void): void;
    /**
     * 链接目标蓝牙设备。
     *
     * @param {{
     *      deviceId: string;
     *      success?: (resp: IBleConnectResult) => void;
     *      fail?: (resp: IBleConnectResult) => void;
     *      complete?: (resp: IBleConnectResult) => void;
     * }} options 创建蓝牙链接。
     *
     * @param {string} options.deviceId 蓝牙设备ID;
     * @param {IBleConnectResult} options.success 链接成功回调函数;
     * @param {IBleConnectResult} options.fail 链接失败回调函数;
     * @param {IBleConnectResult} options.complete 链接成功或失败时的回调函数;
     *
     * 回调参数:
     * @param {string} resp.deviceId 响应设备ID;
     * @param {number} resp:status 蓝牙链接响应状态码:
     *      0:连接成功;
     *      1:链接失败;
     *      2:链接断开;
     */
    createBLEConnection(options: {
        deviceId: string;
        success?: (resp: IBleConnectResult) => void;
        fail?: (resp: IBleConnectResult) => void;
        complete?: (resp: IBleConnectResult) => void;
    }): void;
    /**
     * 断开蓝牙链接。
     *
     * @param {
     *      deviceId: string;
     * } options 断开蓝牙链接请求参数;
     * @param {string} options.deviceId 目标蓝牙设备ID;
     */
    closeBLEConnection(options: {
        deviceId: string;
    }): void;
    /**
     * 获取蓝牙服务列表。
     *
     * @param {{
     *      deviceId: string;
     *      fail?: () => void;
     *      success?: (resp: string[]) => void;
     *      complete?: (resp: string[]) => void;
     * }} options 获取蓝牙服务列表。
     *
     * @param {string} options.deviceId 请求的设备ID;
     * @param {() => void} options.fail 请求失败回调函数;
     * @param {(resp: string[]) => void} options.success 请求成功回调函数;
     * @param {(resp: string[]) => void} options.complete 请求成功或失败回调函数;
     */
    getBLEDeviceServices(options: {
        deviceId: string;
        fail?: () => void;
        success?: (resp: string[]) => void;
        complete?: (resp: string[]) => void;
    }): void;
    /**
     * 获取蓝牙特征值列表。
     *
     * @param {{
     *      deviceId: string;
     *      serviceId: string;
     *      fail?: () => void;
     *      success?: (resp: IBleGattCharacter[]) => void;
     *      complete?: (resp: IBleGattCharacter[]) => void;
     * }} options 特征值请求参数。
     * @param {string} options.deviceId 请求设备ID;
     * @param {string} options.serviceId 请求的服务ID;
     * @param {() => void} options.fail 请求成功回调函数;
     * @param {(resp: IBleGattCharacter[]) => void} options.success 请求成功回调函数;
     * @param {(resp: IBleGattCharacter[]) => void} options.complete 请求成功或失败的回调函数;
     */
    getCharacteristicList(options: {
        deviceId: string;
        serviceId: string;
        fail?: () => void;
        success?: (resp: IBleGattCharacter[]) => void;
        complete?: (resp: IBleGattCharacter[]) => void;
    }): void;
    /**
     * 读取蓝牙设备数据。
     *
     * @param {{
     *      deviceId: string;
     *      serviceId: string;
     *      characteristicId: string;
     *      fail?: (resp: IReadBleResults) => void;
     *      success?: (resp: IReadBleResults) => void;
     *      complete?: (resp: IReadBleResults) => void;
     * }} options 蓝牙数据读取配置信息。
     * @param {string} options.deviceId 目标蓝牙设备ID;
     * @param {string} options.serviceId 目标蓝牙服务ID;
     * @param {string} options.characteristicId 目标蓝牙特征值ID;
     * @param {(resp: IReadBleResults) => void} options.fail 数据读取失败回调函数;
     * @param {(resp: IReadBleResults) => void} options.success 数据读取失败回调函数;
     * @param {(resp: IReadBleResults) => void} options.complete 数据读取失败回调函数;
     *
     * 蓝牙读取响应参数:
     * @param {number} resp.status 响应状态码:
     *      0:蓝牙数据读取成功;
     *      1:蓝牙数据读取失败;
     * @param {string} resp.value 读取到的蓝牙数据十六进制字符串,eg: ffffffff;
     */
    readBle(options: {
        deviceId: string;
        serviceId: string;
        characteristicId: string;
        fail?: (resp: IReadBleResults) => void;
        success?: (resp: IReadBleResults) => void;
        complete?: (resp: IReadBleResults) => void;
    }): void;
    /**
     * 写入数据到蓝牙设备
     *
     * @param {*} options 写入蓝牙配置信息;
     * @param {string} options.deviceId 目标设备ID;
     * @param {string} options.serviceId 目标服务ID;
     * @param {string} options.characteristicId 目标特征值ID;
     * @param {string} options.value 写入数据的十六进制字符串;
     * @param {boolean} options.withResponse
     * @param {(resp: IWriteBleResults) => void} options.fail 数据写入失败时的回调函数;
     * @param {(resp: IWriteBleResults) => void} options.success 数据写入成功时的回调函数;
     * @param {(resp: IWriteBleResults) => void} options.complete 数据写入成功或者失败是的回调眼熟;
     *
     * 回调参数介绍:
     * @param {number} resp.status 写入响应状态信息:
     *      0:表示成功;
     *      其他:表示失败;
     */
    writeBle(options: {
        deviceId: string;
        serviceId: string;
        characteristicId: string;
        value: string;
        withResponse?: boolean;
        success?: (resp: IWriteBleResults) => void;
        fail?: (resp: IWriteBleResults) => void;
        complete?: (resp: IWriteBleResults) => void;
    }): void;
    /**
     * 启动蓝牙侦听模式
     * @param {*} options 蓝牙通知通道数据侦听相关配置参数。
     * @param {string} options.deviceId 目标设备ID;
     * @param {string} options.serviceId 目标服务ID;
     * @param {string} options.characteristicId 目标特征值ID;
     * @param {(resp: INotifyBleResults) => void} options.success 蓝牙侦听成功回调函数;
     * @param {(resp: INotifyBleResults) => void} options.fail 蓝牙侦听失败回调函数;
     * @param {(resp: INotifyBleResults) => void} options.complete 蓝牙侦听成功或失败时的回调函数;
     *
     * 回调参数介绍:
     * @param {number} resp.status 回调状态:
     *      0:表示启动成功;
     *      1:表示侦听失败;
     *      2:表示检测到数据变化,可以通过 value 来获取数据;
     * @param {string} resp.value 接收到的十六进制字符串数据;
     */
    notifyBle(options: {
        deviceId: string;
        serviceId: string;
        characteristicId: string;
        success?: (data: INotifyBleResults) => void;
        fail?: (data: INotifyBleResults) => void;
        complete?: (data: INotifyBleResults) => void;
    }): void;
    /**
     * 协商MTU大小。
     *
     * @param options 协商MTU相关配置参数。
     * @param {string} options.deviceId 目标设备ID;
     * @param {number} options.mtu 待协商的目标MTU大小;
     * @param {(resp) => void} options.success 请求成功回调函数;
     * @param {(resp) => void} options.fail 请求成功回调函数;
     * @param {(resp) => void} options.complete 请求成功或失败时的回调函数;
     *
     * 请求回调参数介绍:
     * @param {number} resp.status 响应状态码:
     *      0:协商成功;
     *      1:协商失败;
     * @param {number} resp.mtu 协商后的MTU大小。
     */
    setMtu(options: {
        deviceId: string;
        mtu: number;
        success?: (resp: {
            status: number;
            mtu: number;
        }) => void;
        fail?: (resp: {
            status: number;
            mtu?: number;
        }) => void;
        complete?: (resp: {
            status: number;
            mtu?: number;
        }) => void;
    }): void;
}
export declare const instance: BLEAdapter;
//# sourceMappingURL=BLEAdapter.d.ts.map
1.0.3

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago