uniapp蓝牙发送不过去怎么回事

1. 背景介绍

UniApp是一个基于Vue.js框架的跨平台开发工具,可以同时开发iOS、Android、H5、小程序等多平台应用。而蓝牙模块作为UniApp的重要组成部分,可以实现手机与其他蓝牙设备的数据交换。但在实际开发过程中,开发者可能会遇到一些蓝牙发送不过去的问题,本文将着重介绍这种问题的解决方法。

2. 原因分析

蓝牙发送不过去的原因大体可以归纳为以下几种:

2.1 连接问题

由于蓝牙设备可能处于未连接状态,开发者需要在发送数据之前确认设备已经成功连接。此外,当连接成功后,若未执行释放连接的相关代码,可能会造成二次连接或通讯出错。

2.2 数据格式问题

在UniApp中蓝牙通信采用的是二进制数据,而在进行具体的数据发送之前,需要将实际数据格式转化为二进制。如果转化不正确,就会导致数据发送失败。

2.3 字符编码问题

蓝牙设备支持的字符编码格式可能与开发者所使用的编码格式不同。开发者需要在发送数据前确认好字符编码格式,以免造成通讯错误。

3. 解决方案

3.1 连接问题

为避免连接错误,开发者需要在进行数据发送前首先判断设备是否已连接,如下:

//判断设备当前是否已连接

if (!this.connectedDeviceId) {

uni.showToast({

title: '设备未连接',

icon: 'none'

})

return

}

而当已连接为true时,则可继续进行数据发送操作:

//已连接蓝牙设备,开始通讯

uni.writeBLECharacteristicValue({

deviceId: this.connectedDeviceId,

serviceId: this.UUID_service,

characteristicId: this.UUID_write,

value: buffer,

success: res => {

//操作成功

console.log('writeBLECharacteristicValue success: ', res)

},

fail: e => {

//操作失败

console.log('writeBLECharacteristicValue fail: ', e)

}

})

需要注意的是,操作完成后一定要执行释放连接的操作,防止出现二次连接的情况:

//释放蓝牙设备的连接

uni.closeBLEConnection({

deviceId: this.connectedDeviceId, //要断开设备的id

success: res => {

//断开成功

console.log('closeBLEConnection success: ', res)

},

fail: e => {

//断开失败

console.log('closeBLEConnection fail: ', e)

}

})

3.2 数据格式问题

在进行数据格式转化前,需要先确认数据格式。若数据为ArrayBuffer类型,则可直接将其传递给value字段:

let buffer = new Uint8Array([0x12, 0x34, 0x56]).buffer

uni.writeBLECharacteristicValue({

deviceId: this.connectedDeviceId,

serviceId: this.UUID_service,

characteristicId: this.UUID_write,

value: buffer,

success: res => {

//操作成功

console.log('writeBLECharacteristicValue success: ', res)

},

fail: e => {

//操作失败

console.log('writeBLECharacteristicValue fail: ', e)

}

})

而如果数据为字符串,则需要将其转化为二进制格式:

let str = 'hello world'

//将字符串转化为ArrayBuffer格式

let buffer = new ArrayBuffer(str.length * 2)

let dataView = new DataView(buffer)

for (let i = 0; i < str.length; i++) {

dataView.setUint16(i * 2, str.charCodeAt(i), true)

}

uni.writeBLECharacteristicValue({

deviceId: this.connectedDeviceId,

serviceId: this.UUID_service,

characteristicId: this.UUID_write,

value: buffer,

success: res => {

//操作成功

console.log('writeBLECharacteristicValue success: ', res)

},

fail: e => {

//操作失败

console.log('writeBLECharacteristicValue fail: ', e)

}

})

3.3 字符编码问题

在进行数据发送操作前,开发者可以先通过uni.getSystemInfoSync()方法获取当前设备的字符编码格式:

//获取设备支持的字符编码格式

let systemInfo = uni.getSystemInfoSync()

let encoding = systemInfo.language

然后可以通过iconv-lite等工具对需要发送的字符串进行转化:

let iconv = require('iconv-lite')

let str = 'hello world'

//将字符串转化为Uint8Array类型

let data = iconv.encode(str, encoding)

//对数组类型数据进行数据发送

let buffer = new Uint8Array(data)

uni.writeBLECharacteristicValue({

deviceId: this.connectedDeviceId,

serviceId: this.UUID_service,

characteristicId: this.UUID_write,

value: buffer.buffer,

success: res => {

//操作成功

console.log('writeBLECharacteristicValue success: ', res)

},

fail: e => {

//操作失败

console.log('writeBLECharacteristicValue fail: ', e)

}

})

4. 总结

在进行UniApp蓝牙开发时,开发者需要注意不同设备的连接情况、数据格式、字符编码等问题。本文针对蓝牙发送不过去的问题,从连接问题、数据格式问题、字符编码问题等方面进行了详细阐述,并提供了相关的解决方案,希望有所帮助。

免责声明:本文来自互联网,本站所有信息(包括但不限于文字、视频、音频、数据及图表),不保证该信息的准确性、真实性、完整性、有效性、及时性、原创性等,版权归属于原作者,如无意侵犯媒体或个人知识产权,请来电或致函告之,本站将在第一时间处理。猿码集站发布此文目的在于促进信息交流,此文观点与本站立场无关,不承担任何责任。