123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import errToString from "./error";
- import {logs} from '../util'
- import {promisify} from '../uniFunction'
- import {ab2hex} from "@/utils/ble/hex";
- export const _onBluetoothAdapterStateChange = (callback) => {
- /* #ifndef H5 */
- uni.onBluetoothAdapterStateChange(res => {
- // 处理蓝牙开启与关闭问题
- callback(res)
- })
- /* #endif */
- }
- export const _onBLEConnectionStateChange = (callback) => {
- /* #ifndef H5 */
- uni.onBLEConnectionStateChange(res => {
- // 该方法回调中可以用于处理连接意外断开等异常情况
- callback(res)
- })
- /* #endif */
- }
- export const _onBLECharacteristicValueChange = (callback) => {
- /* #ifndef H5 */
- uni.onBLECharacteristicValueChange(res => {
- res.str = ab2hex(res.value)
- callback(res)
- })
- /* #endif */
- }
- export const _openAdapter = () => {
- logs(`准备初始化蓝牙适配器...`);
- return promisify(uni.openBluetoothAdapter).then((res) => {
- logs(`✔ 适配器初始化成功!`, res);
- return res;
- }, (err) => {
- throw `初始化失败!${errToString(err)}`
- })
- }
- /**
- * @param data
- */
- export const _startSearch = (data = {}) => {
- logs(`准备搜寻附近的蓝牙外围设备...`);
- /* #ifndef H5 */
- return promisify(uni.startBluetoothDevicesDiscovery, Object.assign({
- interval: 10
- }, data)).then(
- (res) => {
- logs(`✔ 搜索成功!`, res);
- return res
- },
- (err) => {
- throw `搜索蓝牙失败!${errToString(err)}`
- }
- );
- /* #endif */
- }
- /**
- * 停止蓝牙搜索
- * @returns {PromiseLike<[null, any] | [(string|string), null]> | Promise<[null, any] | [(string|string), null]>}
- * @private
- */
- export const _stopSearchBluetooth = () => {
- logs(`停止查找新设备...`);
- /* #ifdef H5 */
- throw `停止查询失败!H5不能操作`
- /* #endif */
- return uni.stopBluetoothDevicesDiscovery().then(
- (res) => {
- logs(`✔ 停止查找设备成功!`);
- return res
- },
- (err) => {
- throw `停止查询失败!${errToString(err)}`
- }
- );
- }
- /**
- * 连接设备
- * @param deviceId
- * @returns {Promise<[null, unknown] | [null, any] | [(string|string), null]>}
- * @private
- */
- export const _connectBlue = (deviceId) => {
- logs(`准备连接设备...`);
- /* #ifndef H5 */
- return promisify(uni.createBLEConnection, {
- deviceId,
- }).then(
- (res) => {
- logs(`✔ 连接蓝牙成功!`);
- return true
- },
- (err) => {
- if (err.errCode == 10010) {
- logs(`✔ 连接蓝牙成功!`);
- return true
- } else {
- throw `连接蓝牙失败! ${errToString(err)}`
- }
- }
- );
- /* #endif */
- }
- /**
- * 关闭设备蓝牙
- * @param deviceId
- * @returns {Promise<[null, unknown] | [(string|string), null]>}
- * @private
- */
- export const _closeBLEConnection = (deviceId) => {
- logs(`断开蓝牙连接...`)
- /* #ifndef H5 */
- return promisify(uni.closeBLEConnection, {
- deviceId,
- }).then(
- (res) => {
- logs(`✔ 断开蓝牙成功!`);
- return true
- },
- (err) => {
- throw `断开蓝牙连接失败! ${errToString(err)}`
- }
- );
- /* #endif */
- }
- /**
- * 关闭蓝牙
- * @returns {Promise<[null, unknown] | [(string|string), null]>}
- * @private
- */
- export const _closeBLEAdapter = () => {
- logs(`释放蓝牙适配器...`)
- return promisify(uni.closeBluetoothAdapter).then(
- (res) => {
- logs(`✔ 释放适配器成功!`)
- return res
- },
- (err) => {
- throw `释放适配器失败! ${errToString(err)}`
- }
- );
- }
- /**
- * 获取设备服务列表
- * @param deviceId
- * @returns {Promise<[null, unknown] | [(string|string), null]>}
- * @private
- */
- export const _getBLEServices = (deviceId) => {
- logs(`获取蓝牙设备所有服务...`)
- return promisify(uni.getBLEDeviceServices, {
- deviceId
- }).then(res => {
- logs(`✔ 获取service成功!`)
- return res
- }, err => {
- throw `获取service失败! ${errToString(err)}`
- })
- }
- /**
- * 获取服务通道
- * @param deviceId
- * @param serviceId
- * @returns {Promise<[null, unknown] | [(string|string), null]>}
- * @private
- */
- export const _getCharacteristics = (deviceId, serviceId) => {
- let msg = null
- if (!deviceId) msg = `服务通道错误!deviceId为空`
- if (!serviceId) msg = `服务通道错误!serviceId为空`
- if (msg != null) {
- console.error(`获取特征失败,${msg}`)
- throw msg
- }
- logs(`开始获取特征值...`);
- return promisify(uni.getBLEDeviceCharacteristics, {
- deviceId,
- serviceId,
- }).then(
- (res) => {
- logs(`✔ 获取特征值成功!`);
- return res
- },
- (err) => {
- throw `获取特征值失败! ${errToString(err)}`
- }
- );
- }
- // 订阅特征值
- export const _notifyBLECharacteristicValueChange = (deviceId, serviceId, characteristicId) => {
- return promisify(uni.notifyBLECharacteristicValueChange, {
- deviceId,
- serviceId,
- characteristicId,
- state: true
- }).then(res => {
- logs(`✔ 订阅notify成功!`)
- return true
- }, err => {
- throw `订阅notify失败! ${errToString(err)}`
- })
- }
- export const _writeBLECharacteristicValue = (deviceId, serviceId, characteristicId, buffer) => {
- return promisify(uni.writeBLECharacteristicValue, {
- deviceId,
- serviceId,
- characteristicId,
- value: buffer,
- }).then(res => {
- return res
- }, err => {
- throw {
- code: err.errCode,
- msg: `写入数据失败! ${errToString(err)}`
- }
- })
- }
|