import Taro , { getBLEDeviceServices } from '@tarojs/taro' ; import _ from 'lodash' ; import { IBLEServices } from '@/interfaces/common' ; /**
 * 获取蓝牙设备所有服务(service)
 */ export default function getBELSeviceId ( deviceId ) { return Taro . getBLEDeviceServices ( { deviceId } ) . then ( res => { console . log ( '获取蓝牙设备所有 service:' , res ) ; return getCharacteristics ( deviceId , res . services ) ; } ) ; } param services async function getCharacteristics ( deviceId , services ) { const findChar = { read : false , write : false , notify : false } ; const BLEServices = { writeCharaterId : '' , writeServiceId : '' , notifyCharaterId : '' , notifyServiceId : '' , readCharaterId : '' , readServiceId : '' } ; for ( const service of services ) { const { characteristics } = await Taro . getBLEDeviceCharacteristics ( { deviceId , serviceId : service . uuid } ) ; console . log ( `获取蓝牙设备服务(${ service . uuid })中的所有特征值: ` , characteristics ) ; characteristics . forEach ( item => { if ( ! findChar . notify && item . properties . notify ) { findChar . notify = true ; BLEServices . notifyCharaterId = item . uuid ; BLEServices . notifyServiceId = service . uuid ; } if ( ! findChar . read && item . properties . read ) { findChar . read = true ; BLEServices . readCharaterId = item . uuid ; BLEServices . readServiceId = service . uuid ; } if ( ! findChar . write && item . properties . write ) { findChar . write = true ; BLEServices . writeCharaterId = item . uuid ; BLEServices . writeServiceId = service . uuid ; } } ) ; if ( _ . every ( findChar , v => v ) ) { break ; } } console . log ( '获得蓝牙权限：' , findChar ) ; console . log ( '服务ID' , BLEServices ) ; if ( ! _ . every ( findChar , v => v ) ) { return Promise . reject ( '找不到该蓝牙设备读写的特征值' ) ; } else { return Promise . resolve ( BLEServices ) ; } }