在uniAPP中使用使用低功耗蓝牙通讯
1、初始化蓝牙监听器
onLoad ( ) {
uni. onBluetoothAdapterStateChange ( ( res ) => {
console. log ( "蓝牙" + ( res. discovering ? "开启" : "关闭" ) + "搜索" )
this . discovering = res. discovering;
} )
uni. onBluetoothDeviceFound ( resd => {
const devices = resd. devices;
} )
uni. onBLEConnectionStateChange ( res => {
console. log ( ` 设备 ${ res. deviceId} ,connected: ${ res. connected} ` )
this . Connecting = res. connected;
this . deviceId = res. deviceId;
} )
}
2、初始化蓝牙适配器
openBluetoothAdapter ( ) {
uni. openBluetoothAdapter ( {
success : ( res ) => {
setTimeout ( ( ) => {
this . deviceList = [ ] ;
this . startBluetoothDeviceDiscovery ( ) ;
} , 100 ) ;
setTimeout ( ( ) => {
this . stopBluetoothDevicesDiscovery ( ) ;
} , 15 * 1000 ) ;
} ,
fail : err => {
uni. showToast ( {
title: '请确认手机蓝牙是否打开'
} )
}
} )
} ,
3、初始化成功设备后,开始搜索设备,搜索到的设备会在 onBluetoothDeviceFound 方法中回调。
startBluetoothDeviceDiscovery ( ) {
uni. startBluetoothDevicesDiscovery ( {
success : ( resd ) => {
console. log ( "打开成功" , resd) ;
} ,
fail : err => {
console. error ( "startBluetoothDevicesDiscoveryErr" , err) ;
this . Toast ( "请检查蓝牙状态" )
}
} )
} ,
4、连接蓝牙设备 createBLEConnection
createBLEConnection ( deviceId ) {
if ( this . discovering) {
this . stopBluetoothDevicesDiscovery ( )
}
if ( this . Connecting) {
this . Toast ( "设备已连接" ) ;
return
}
uni. showLoading ( {
title: "连接设备中..."
} )
uni. createBLEConnection ( {
deviceId,
success : res => {
uni. hideLoading ( ) ;
this . deviceId = deviceId;
setTimeout ( ( ) => {
this . getBLEDeviceServices ( deviceId) ;
} , 1500 )
} ,
fail : err => {
uni. hideLoading ( ) ;
this . Toast ( '设备连接失败,请检查重试' )
if ( err. errCode == - 1 ) {
this . closeBLEConnect ( deviceId)
}
}
} )
} ,
5、获取蓝牙服务 和服务中的特征值
getBLEDeviceServices ( deviceId ) {
const _this = this ;
uni. getBLEDeviceServices ( {
deviceId,
success : res => {
console. log ( res. services) ;
let services = res. services;
_this. serviceUUID = services[ 2 ] . uuid;
this . getBLEDeviceCharacteristics ( deviceId, _this. serviceUUID) . then ( res => {
console. log ( res) ;
_this. notifyUUid = res. characteristics[ 0 ] . uuid
_this. writeUUid = res. characteristics[ 1 ] . uuid
uni. notifyBLECharacteristicValueChange ( {
deviceId: deviceId,
serviceId: _this. serviceUUID,
characteristicId: _this. notifyUUid,
state: true ,
success : ( res ) => {
console. log ( "广播开启成功" )
_this. onBLECharacteristicValueChange ( ) ;
} ,
fail : ( err ) => {
console. error ( err)
}
} )
setTimeout ( ( ) => {
_this. writeBLECharacteristicValue ( [ 0x5A , 0x03 , 0xFF ] )
} , 2000 )
} )
} ,
fail : err => {
console. warn ( "设备服务Error" + err)
}
} )
} ,
getBLEDeviceCharacteristics ( deviceId, serviceId ) {
const _this = this ;
return new Promise ( ( resolve, reject ) => {
uni. getBLEDeviceCharacteristics ( {
deviceId,
serviceId,
success : res => {
resolve ( res) ;
} ,
fail : err => {
console. log ( "获取特征值失败" , err)
reject ( err) ;
}
} )
} )
} ,
onBLECharacteristicValueChange ( ) {
uni. onBLECharacteristicValueChange ( res => {
console. log ( ` 监听低功耗蓝牙设备的特征值变化事件 ${ res. characteristicId} has changed, now is ${ res. value} ` )
console. log ( this . ab2hex ( res. value) ) ;
} )
} ,
ab2hex ( buffer ) {
const hexArr = Array . prototype. map . call (
new Uint8Array ( buffer) ,
function ( bit ) {
return ( '00' + bit. toString ( 16 ) ) . slice ( - 2 )
}
)
return hexArr. join ( '' )
} ,
特征值解析
"characteristics" : [ {
"uuid" : "00002A00-0000-1000-8000-00805F9B34FB" ,
"properties" : {
"read" : false ,
"write" : true ,
"notify" : true ,
"indicate" : false
}
} ]
6、写入数据交互
writeBLECharacteristicValue ( value ) {
let buffer = new ArrayBuffer ( 1 )
let dataView = new DataView ( buffer)
dataView. setInt8 ( 0x01 )
uni. writeBLECharacteristicValue ( {
deviceId: this . deviceId,
serviceId: this . serviceUUID,
characteristicId: this . writeUUid,
value: buffer,
success : res => {
console. log ( "写入成功" , res)
} ,
fail : err => {
console. error ( "写入失败" , err)
}
} )
} ,
7、在卸载页面中记得断开连接
onUnload ( ) {
if ( this . discovering) {
this . stopBluetoothDevicesDiscovery ( ) ;
}
if ( this . Connecting) {
this . closeBLEConnect ( this . deviceId) ;
}
} ,
8、最后我们贴上完整代码
export default {
data ( ) {
return {
discovering: false ,
deviceList: [ ] ,
deviceId: '' ,
Connecting: false ,
serviceUUID: '' ,
writeUUid: '' ,
notifyUUid: ''
}
} ,
methods: {
seachDevice ( ) {
if ( ! this . discovering) {
this . openBluetoothAdapter ( ) ;
if ( this . Connecting) {
this . closeBLEConnect ( this . deviceId) ;
}
} else {
this . Toast ( "正在扫描中,请等待" ) ;
}
} ,
openBluetoothAdapter ( ) {
uni. openBluetoothAdapter ( {
success : ( res ) => {
setTimeout ( ( ) => {
this . deviceList = [ ] ;
this . startBluetoothDeviceDiscovery ( ) ;
} , 100 ) ;
setTimeout ( ( ) => {
this . stopBluetoothDevicesDiscovery ( ) ;
} , 15 * 1000 ) ;
} ,
fail : err => {
uni. showToast ( {
title: '请确认手机蓝牙是否打开'
} )
}
} )
} ,
startBluetoothDeviceDiscovery ( ) {
uni. startBluetoothDevicesDiscovery ( {
success : ( resd ) => {
console. log ( "打开成功" , resd) ;
} ,
fail : err => {
console. error ( "startBluetoothDevicesDiscoveryErr" , err) ;
this . Toast ( "请检查蓝牙状态" )
}
} )
} ,
stopBluetoothDevicesDiscovery ( ) {
uni. stopBluetoothDevicesDiscovery ( {
success : res => {
}
} )
} ,
onBLEConnectionStateChange ( ) {
uni. onBLEConnectionStateChange ( res => {
console. log ( ` 设备 ${ res. deviceId} ,connected: ${ res. connected} ` )
this . Connecting = res. connected;
this . deviceId = res. deviceId;
} )
} ,
createBLEConnection ( deviceId ) {
if ( this . discovering) {
this . stopBluetoothDevicesDiscovery ( )
}
if ( this . Connecting) {
this . Toast ( "设备已连接" ) ;
return
}
uni. showLoading ( {
title: "连接设备中..."
} )
uni. createBLEConnection ( {
deviceId,
success : res => {
uni. hideLoading ( ) ;
this . Toast ( '设备连接成功' )
this . deviceId = deviceId;
setTimeout ( ( ) => {
this . getBLEDeviceServices ( deviceId) ;
} , 1500 )
} ,
fail : err => {
uni. hideLoading ( ) ;
this . Toast ( '设备连接失败,请检查重试' )
if ( err. errCode == - 1 ) {
this . closeBLEConnect ( deviceId)
}
}
} )
} ,
getBLEDeviceServices ( deviceId ) {
const _this = this ;
uni. getBLEDeviceServices ( {
deviceId,
success : res => {
console. log ( res. services) ;
let services = res. services;
_this. serviceUUID = services[ 2 ] . uuid;
this . getBLEDeviceCharacteristics ( deviceId, _this. serviceUUID) . then ( res => {
console. log ( res) ;
_this. notifyUUid = res. characteristics[ 0 ] . uuid
_this. writeUUid = res. characteristics[ 1 ] . uuid
uni. notifyBLECharacteristicValueChange ( {
deviceId: deviceId,
serviceId: _this. serviceUUID,
characteristicId: _this. notifyUUid,
state: true ,
success : ( res ) => {
console. log ( "广播开启成功" )
_this. onBLECharacteristicValueChange ( ) ;
} ,
fail : ( err ) => {
console. error ( err)
}
} )
setTimeout ( ( ) => {
_this. writeBLECharacteristicValue ( [ 0x5A , 0x03 , 0xFF ] )
} , 2000 )
} )
} ,
fail : err => {
console. warn ( "设备服务Error" + err)
}
} )
} ,
writeBLECharacteristicValue ( value ) {
let buffer = new ArrayBuffer ( 1 )
let dataView = new DataView ( buffer)
dataView. setInt8 ( 0x01 )
uni. writeBLECharacteristicValue ( {
deviceId: this . deviceId,
serviceId: this . serviceUUID,
characteristicId: this . writeUUid,
value: buffer,
success : res => {
console. log ( "写入成功" , res)
} ,
fail : err => {
console. error ( "写入失败" , err)
}
} )
} ,
onBLECharacteristicValueChange ( ) {
uni. onBLECharacteristicValueChange ( res => {
console. log ( ` 监听低功耗蓝牙设备的特征值变化事件 ${ res. characteristicId} has changed, now is ${ res. value} ` )
console. log ( this . ab2hex ( res. value) ) ;
} )
} ,
ab2hex ( buffer ) {
const hexArr = Array . prototype. map . call (
new Uint8Array ( buffer) ,
function ( bit ) {
return ( '00' + bit. toString ( 16 ) ) . slice ( - 2 )
}
)
return hexArr. join ( '' )
} ,
getBLEDeviceCharacteristics ( deviceId, serviceId ) {
const _this = this ;
return new Promise ( ( resolve, reject ) => {
uni. getBLEDeviceCharacteristics ( {
deviceId,
serviceId,
success : res => {
resolve ( res) ;
} ,
fail : err => {
console. log ( "获取特征值失败" , err)
reject ( err) ;
}
} )
} )
} ,
closeBLEConnect ( deviceId ) {
uni. closeBLEConnection ( {
deviceId,
success : res => {
this . deviceId = "" ;
console. log ( "断开成功" , res) ;
} ,
fail : err => {
console. error ( "断开错误" , err) ;
}
} )
} ,
getBLEDeviceRssi ( deviceId ) {
uni. getBLEDeviceRSSI ( {
deviceId,
success : res => {
console. log ( "获取蓝牙设备强度成功" , res) ;
} ,
fail : err => {
console. error ( "获取蓝牙强度失败," , err) ;
}
} )
} ,
readBLEDeviceData ( deviceId, serviceId, characteristicId ) {
uni. readBLECharacteristicValue ( {
deviceId,
serviceId,
characteristicId,
success : res => {
console. log ( "读取设备值成功" , res) ;
} ,
fail : err => {
console. error ( "读取设备值err" , err)
}
} )
} ,
Toast ( title ) {
uni. showToast ( {
title,
position: 'bottom' ,
icon: 'none'
} )
} ,
} ,
onLoad ( ) {
uni. onBluetoothAdapterStateChange ( ( res ) => {
console. log ( "蓝牙" + ( res. discovering ? "开启" : "关闭" ) + "搜索" )
this . discovering = res. discovering;
} )
uni. onBluetoothDeviceFound ( resd => {
const devices = resd. devices;
} )
this . onBLEConnectionStateChange ( ) ;
this . seachDevice ( )
} ,
onUnload ( ) {
if ( this . discovering) {
this . stopBluetoothDevicesDiscovery ( ) ;
}
if ( this . Connecting) {
this . closeBLEConnect ( this . deviceId) ;
}
} ,
}