代码拉取完成,页面将自动刷新
// 理论上,开发者可以自己封装一个uniappx层专用的蓝牙封装库,然后按需 import 为 UTSBLE 这个别名,然后去随意调用其中的实现
// 这样子的话,如果以后开发者需要做出任意的逻辑变动,都不会影响到原本的业务代码,甚至可以办到更换其他的BLE库而无需重新更改所有涉及到蓝牙的页面
// 可以非常放心的是,BLE.js的实现和interface.uts暴露出去的默认通用规范实现的API,都是可用的
// 本例程由于时间安排相关原因,暂时只提供简单的搜索例程,由于UniAppX的强类型要求原因,导致部分开发者可能会遇到一些与原生平台语言有关的问题,此时请优先阅读官方文档寻求帮助
// 且由于UniAppx目前处于发展阶段的原因,可能有一些特性并不支持,有一些写法有隐藏BUG,未知隐患,这都是可能的,遇到问题实在无法解决请联系官方处理,可能并不是你的问题
// Android下是真正的编译为kotlin,没有js引擎,因此我们需要直接引入uts原生依赖
// #ifdef APP-ANDROID
import * as UTSBLE from '@/uni_modules/xl-uts-bluetooth';
// #endif
// iOS下导入js依赖库,注意,这种写法只能能UniAppX iOS js逻辑层使用,如果后期出了swift层,插件开发者需要进行适配,
// 因为swift逻辑层会引发整个uts插件生态的兼容性问题,并不能简单的直接去使用,比如最经典的问题:swift闭包函数无法比较导致回调函数重复注册且无法移除而内存泄漏的问题
// #ifdef APP-IOS
import * as UTSBLE from '@/uni_modules/xl-uts-bluetooth/jssdk/BLE.js';
// #endif
// 暂不支持web层,或许后期可以通过某些库,比如plus库或者其他的蓝牙库去间接实现通信,但是本插件暂时无法办到,可能需要开发者自己去想办法 适配 UTSBLE 的实现
// #ifdef WEB
import * as UTSBLE from '@/uni_modules/xl-uts-bluetooth/jssdk/BLE.js';
throw new Error("不支持的平台");
// #endif
// 导入必须的定义,API需要依赖此定义进行暴露的强类型的函数的声明
import {
OpenBluetoothAdapterOptions, CloseBluetoothAdapterOptions, GetBluetoothAdapterStateOptions,
OnBluetoothAdapterStateChangeCallback, OnBluetoothDeviceFoundCallback, StartBluetoothDevicesDiscoveryOptions,
GetBluetoothDevicesOptions, StopBluetoothDevicesDiscoveryOptions, OnBLEConnectionStateChangeCallback, CreateBLEConnectionOptions,
CloseBLEConnectionOptions, OnBLEMTUChangeCallback, SetBLEMTUOptions, GetBLEMTUOptions, GetBLEDeviceServicesOptions,
GetBLEDeviceCharacteristicsOptions, ReadBLECharacteristicValueOptions, WriteBLECharacteristicValueOptions,
BatchWriteBLECharacteristicValueOptions, NotifyBLECharacteristicValueChangeOptions, OnBLECharacteristicValueChangeCallback,
ApiCommonResult, GetBluetoothAdapterStateSuccessCallbackResult, GetBluetoothDevicesSuccessCallbackResult, GetBLEMTUSuccessCallbackResult,
GetBLEDeviceServicesSuccessCallbackResult, GetBLEDeviceCharacteristicsSuccessCallbackResult,
} from '@/uni_modules/xl-uts-bluetooth';
// 导出所有BLE库支持的API,注意,此为TS写法,仅仅支持UniappX工程,在Android上会被编译为KT,在IOS js逻辑层上会保留为uts(ts),所以我们 BLE.js 的导入才能正常工作,
// 而如果后期出了UniappX swift逻辑层,这个封装库可能会出现编译时的兼容性问题,但是目前还不担心,UniappX swift逻辑层还未上线,
// 我们可以在这个封装的非uni_modules文件夹内的而是工程内的uts文件中,实现调用真正的BLE UTS库的逻辑,不会有啥问题
// --------------------------------------------- BLE操作的异步接口 ---------------------------------------------
// Promise风格适配,iOS用的是BLE.js,其内部实现了js层的Promise适配,因此可以直接用
// #ifdef APP-IOS
export function openBluetoothAdapter (options: OpenBluetoothAdapterOptions) : Promise<ApiCommonResult> | null {
return UTSBLE.openBluetoothAdapter(options);
}
export function closeBluetoothAdapter (options: CloseBluetoothAdapterOptions): Promise<ApiCommonResult> | null {
return UTSBLE.closeBluetoothAdapter(options);
}
export function getBluetoothAdapterState (options: GetBluetoothAdapterStateOptions): Promise<GetBluetoothAdapterStateSuccessCallbackResult> | null {
return UTSBLE.getBluetoothAdapterState(options);
}
export function startBluetoothDevicesDiscovery (options: StartBluetoothDevicesDiscoveryOptions): Promise<ApiCommonResult> | null {
return UTSBLE.startBluetoothDevicesDiscovery(options);
}
export function getBluetoothDevices (options: GetBluetoothDevicesOptions): Promise<GetBluetoothDevicesSuccessCallbackResult> | null {
return UTSBLE.getBluetoothDevices(options);
}
export function stopBluetoothDevicesDiscovery (options: StopBluetoothDevicesDiscoveryOptions): Promise<ApiCommonResult> | null {
return UTSBLE.stopBluetoothDevicesDiscovery(options);
}
export function createBLEConnection (options: CreateBLEConnectionOptions): Promise<ApiCommonResult> | null {
return UTSBLE.createBLEConnection(options);
}
export function closeBLEConnection (options: CloseBLEConnectionOptions): Promise<ApiCommonResult> | null {
return UTSBLE.closeBLEConnection(options);
}
export function setBLEMTU (options: SetBLEMTUOptions): Promise<ApiCommonResult> | null {
return UTSBLE.setBLEMTU(options);
}
export function getBLEMTU (options: GetBLEMTUOptions): Promise<GetBLEMTUSuccessCallbackResult> | null {
return UTSBLE.getBLEMTU(options);
}
export function getBLEDeviceServices (options: GetBLEDeviceServicesOptions): Promise<GetBLEDeviceServicesSuccessCallbackResult> | null {
return UTSBLE.getBLEDeviceServices(options);
}
export function getBLEDeviceCharacteristics (options: GetBLEDeviceCharacteristicsOptions): Promise<GetBLEDeviceCharacteristicsSuccessCallbackResult> | null {
return UTSBLE.getBLEDeviceCharacteristics(options);
}
export function readBLECharacteristicValue (options: ReadBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
return UTSBLE.readBLECharacteristicValue(options);
}
export function writeBLECharacteristicValue (options: WriteBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
return UTSBLE.writeBLECharacteristicValue(options);
}
export function batchWriteBLECharacteristicValue (options: BatchWriteBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
return UTSBLE.batchWriteBLECharacteristicValue(options);
}
export function notifyBLECharacteristicValueChange (options: NotifyBLECharacteristicValueChangeOptions): Promise<ApiCommonResult> | null {
return UTSBLE.notifyBLECharacteristicValueChange(options);
}
// #endif
// Promise风格适配,Android端最终是编译到kotlin的,我们需要对其进行一层封装适配,适配之后,在条件符合的情况下,异步接口可返回Promise
// 关于Android端的Promise适配,可参考官方对Promise的支持文档:https://doc.dcloud.net.cn/uni-app-x/uts/buildin-object-api/promise.html
// 关于UTS使用异步函数极其相关事项,可参考官方支持文档:https://doc.dcloud.net.cn/uni-app-x/uts/function.html#async
// #ifdef APP-ANDROID
export function openBluetoothAdapter (options: OpenBluetoothAdapterOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.openBluetoothAdapter(options);
});
} else {
UTSBLE.openBluetoothAdapter(options);
return null;
}
}
export function closeBluetoothAdapter (options: CloseBluetoothAdapterOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.closeBluetoothAdapter(options);
});
} else {
UTSBLE.closeBluetoothAdapter(options);
return null;
}
}
export function getBluetoothAdapterState (options: GetBluetoothAdapterStateOptions): Promise<GetBluetoothAdapterStateSuccessCallbackResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<GetBluetoothAdapterStateSuccessCallbackResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.getBluetoothAdapterState(options);
});
} else {
UTSBLE.getBluetoothAdapterState(options);
return null;
}
}
export function startBluetoothDevicesDiscovery (options: StartBluetoothDevicesDiscoveryOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.startBluetoothDevicesDiscovery(options);
});
} else {
UTSBLE.startBluetoothDevicesDiscovery(options);
return null;
}
}
export function getBluetoothDevices (options: GetBluetoothDevicesOptions): Promise<GetBluetoothDevicesSuccessCallbackResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<GetBluetoothDevicesSuccessCallbackResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.getBluetoothDevices(options);
});
} else {
UTSBLE.getBluetoothDevices(options);
return null;
}
}
export function stopBluetoothDevicesDiscovery (options: StopBluetoothDevicesDiscoveryOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.stopBluetoothDevicesDiscovery(options);
});
} else {
UTSBLE.stopBluetoothDevicesDiscovery(options);
return null;
}
}
export function createBLEConnection (options: CreateBLEConnectionOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.createBLEConnection(options);
});
} else {
UTSBLE.createBLEConnection(options);
return null;
}
}
export function closeBLEConnection (options: CloseBLEConnectionOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.closeBLEConnection(options);
});
} else {
UTSBLE.closeBLEConnection(options);
return null;
}
}
export function setBLEMTU (options: SetBLEMTUOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.setBLEMTU(options);
});
} else {
UTSBLE.setBLEMTU(options);
return null;
}
}
export function getBLEMTU (options: GetBLEMTUOptions): Promise<GetBLEMTUSuccessCallbackResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<GetBLEMTUSuccessCallbackResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.getBLEMTU(options);
});
} else {
UTSBLE.getBLEMTU(options);
return null;
}
}
export function getBLEDeviceServices (options: GetBLEDeviceServicesOptions): Promise<GetBLEDeviceServicesSuccessCallbackResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<GetBLEDeviceServicesSuccessCallbackResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.getBLEDeviceServices(options);
});
} else {
UTSBLE.getBLEDeviceServices(options);
return null;
}
}
export function getBLEDeviceCharacteristics (options: GetBLEDeviceCharacteristicsOptions): Promise<GetBLEDeviceCharacteristicsSuccessCallbackResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<GetBLEDeviceCharacteristicsSuccessCallbackResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.getBLEDeviceCharacteristics(options);
});
} else {
UTSBLE.getBLEDeviceCharacteristics(options);
return null;
}
}
export function readBLECharacteristicValue (options: ReadBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.readBLECharacteristicValue(options);
});
} else {
UTSBLE.readBLECharacteristicValue(options);
return null;
}
}
export function writeBLECharacteristicValue (options: WriteBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.writeBLECharacteristicValue(options);
});
} else {
UTSBLE.writeBLECharacteristicValue(options);
return null;
}
}
export function batchWriteBLECharacteristicValue (options: BatchWriteBLECharacteristicValueOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.batchWriteBLECharacteristicValue(options);
});
} else {
UTSBLE.batchWriteBLECharacteristicValue(options);
return null;
}
}
export function notifyBLECharacteristicValueChange (options: NotifyBLECharacteristicValueChangeOptions): Promise<ApiCommonResult> | null {
if (options.success == null && options.fail == null && options.complete == null) {
return new Promise<ApiCommonResult>((resolve, reject) => {
// 拦截两个重要的回调函数,处理成功和失败两种情况
options.success = (res) => {
resolve(res);
};
options.fail = (err) => {
reject(err);
}
UTSBLE.notifyBLECharacteristicValueChange(options);
});
} else {
UTSBLE.notifyBLECharacteristicValueChange(options);
return null;
}
}
// #endif
// --------------------------------------------- 回调注册的同步接口 ---------------------------------------------
export function onBluetoothAdapterStateChange (callback: OnBluetoothAdapterStateChangeCallback) { UTSBLE.onBluetoothAdapterStateChange(callback); }
export function offBluetoothAdapterStateChange () { UTSBLE.offBluetoothAdapterStateChange(); }
export function onBluetoothDeviceFound (callback: OnBluetoothDeviceFoundCallback) { UTSBLE.onBluetoothDeviceFound(callback); }
export function offBluetoothDeviceFound () { UTSBLE.offBluetoothDeviceFound(); }
export function onBLEConnectionStateChange (callback: OnBLEConnectionStateChangeCallback) { UTSBLE.onBLEConnectionStateChange(callback); }
export function offBLEConnectionStateChange (callback?: OnBLEConnectionStateChangeCallback) { UTSBLE.offBLEConnectionStateChange(callback); }
export function onBLEMTUChange (callback: OnBLEMTUChangeCallback) { UTSBLE.onBLEMTUChange(callback); }
export function offBLEMTUChange (callback?: OnBLEMTUChangeCallback) { UTSBLE.offBLEMTUChange(callback); }
export function onBLECharacteristicValueChange (callback: OnBLECharacteristicValueChangeCallback) { UTSBLE.onBLECharacteristicValueChange(callback); }
export function offBLECharacteristicValueChange () { UTSBLE.offBLECharacteristicValueChange(); }
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。