设备是小米,系统是:Xiaomi HyperOS(1.0.23.0.UOPCNXM),相同代码在苹果和华为手机没问题,我正常逻辑是先调用notifyBLECharacteristicValueChange启用监听,然后调writeBLECharacteristicValue写入数据,写入后监听特诊值蓝牙会返回信息,但是在这个手机上,我不调用notifyBLECharacteristicValueChange还能写入成功,但是监听不到蓝牙的返回值,调用了notifyBLECharacteristicValueChange直接写入都无反应,就是writeBLECharacteristicValue的sucess和fail都没有任何反应。
再补充一点,如果连接的设备是无密码的,那都是正常的,如果是需要输入密码的就会出现这个问题,不知道到底是哪里出了问题。
请问这是什么问题,已经困扰一天了?????!!!!!
看下是不是设备特征服务那个顺序两个手机是不是不一样导致你赋值启用没回调
openBluetoothAdapter() { // this.scanState = true // return this.text = '' // 初始化蓝牙模块 uni.openBluetoothAdapter({ mode: 'central', success: (res) => { console.log('蓝牙初始化成功', res) this.connected = true this.onBluetoothAdapterStateChange() this.startBluetoothDevicesDiscovery() }, fail: (err) => { console.log('蓝牙初始化失败', err) this.scanState = false uni.showToast({ icon: 'none', title: '请检查手机蓝牙是否打开' }) } }) }, //开始搜索附近的蓝牙外围设备 startBluetoothDevicesDiscovery() { uni.startBluetoothDevicesDiscovery({ allowDuplicatesKey: true, success: (res) => { console.log('开始搜索附近的蓝牙设备', res) this.scanState = true //开始监听扫描新设备事件 this.onBluetoothDeviceFound() }, fail: (err) => { console.log('搜索蓝牙设备失败', err) } }) }, //搜索过程中检查蓝牙状态(监听) onBluetoothAdapterStateChange() { var that = this //搜索过程中检查蓝牙状态 uni.onBluetoothAdapterStateChange(function(res) { if (res.available) { that.connected = true console.log('蓝牙适配器状态变为可用') } else { that.connected = false console.log('蓝牙适配器不可用,可能是蓝牙被关闭') that.cancelScan() } }) }, //搜索蓝牙设备(监听) onBluetoothDeviceFound() { //Sleep(500) var that = this //蓝牙搜索中关闭,则停止搜索 if (!that.connected) { return } //监听搜索到新设备的事件 uni.onBluetoothDeviceFound((res) => { if (!that.scanState) { return } //更新蓝牙设备列表数据 res.devices.forEach((device) => { // 名字前两位 != 'HJ'开头的设备排除 if (device.name.slice(0, 2) != 'HJ') { return } console.log('监听扫描到的设备', res.devices) var index = that.devicesList.findIndex(r => r.deviceId == device.deviceId) if (index >= 0) { that.devicesList[index].name = device.name that.devicesList[index].RSSI = device.RSSI that.devicesList[index].connectable = device.connectable that.devicesList[index].lostCount = 0 } else { that.devicesList.push({ ...device, lostCount: 0 }) } }) // 标记未扫描到的设备为丢失,并增加 lostCount that.devicesList.forEach((device, index) => { const found = res.devices.some(d => d.deviceId === device.deviceId); if (!found) { that.devicesList[index].lostCount += 1; if (that.devicesList[index].lostCount >= that.lossCountThreshold) { that.devicesList.splice(index, 1) //从列表中移除设备 } } }) }) }, createBLEConnection(deviceId) { uni.createBLEConnection({ deviceId: deviceId, success: (res) => { uni.showToast({ icon: 'none', title: '连接成功' }) buletoothCommand.DeviceId = deviceId this.connectDeviceId = deviceId console.log('连接成功', res) //安卓设备需要调整MTU值 //最大传输单元。设置范围为 (22,512) 区间内,单位 bytes if (IsAndroidPlat()) { this.setBLEMTU() } uni.stopBluetoothDevicesDiscovery() this.devicesList = [] //发现蓝牙设备上的服务 this.getBLEDeviceServices() }, fail: (res) => { uni.showToast({ icon: 'none', title: '连接失败' }) console.log('连接失败') } }) }, //获取蓝牙低功耗设备所有服务 getBLEDeviceServices() { var that = this uni.getBLEDeviceServices({ deviceId: that.connectDeviceId, // 搜索到设备的 deviceId success: (res) => { for (let i = 0; i < res.services.length; i++) { console.log('获取蓝牙低功耗设备所有服务成功', res.services) // 可根据具体业务需要,选择一个主服务进行通信(透传服务的UUID为0xFFF0) if (res.services[i].uuid.indexOf('0000FFF0') >= 0) { buletoothCommand.ServiceId = res.services[i].uuid that.connectDevice = res.services[i] that.getBLEDeviceCharacteristics() return } } }, fail: (err) => { console.log('获取蓝牙低功耗设备所有服务失败', err) } }) }, //获取蓝牙低功耗设备某个服务中所有特征 (characteristic) getBLEDeviceCharacteristics() { uni.getBLEDeviceCharacteristics({ deviceId: this.connectDeviceId, serviceId: this.connectDevice.uuid, success: (res) => { console.log('获取特征成功', res.characteristics) this.characteristics = res.characteristics for (var i = 0; i < res.characteristics.length; i++) { var item = res.characteristics[i] if (item.uuid.includes('FFF2')) { this.characteristicId_2 = item.uuid buletoothCommand.CharacteristicId = item.uuid } else if (item.uuid.includes('FFF3')) { this.characteristicId_3 = item.uuid //this.setValue() //this.setValueModem() } } //启用蓝牙低功耗设备特征值变化时的 notify 功能,订阅特征 this.notifyBLECharacteristicValueChange() //读取蓝牙低功耗设备特征值的二进制数据 //this.readBLECharacteristicValue() }, fail: (err) => { console.log('获取特征失败', err) } }) }, //启用蓝牙低功耗设备特征值变化时的 notify 功能,订阅特征 notifyBLECharacteristicValueChange() { var that = this for (var i = 0; i < that.characteristics.length; i++) { if (that.characteristics[i].properties.notify || that.characteristics[i].properties .indicate) { console.log('启用notification', that.characteristics[i]) uni.notifyBLECharacteristicValueChange({ deviceId: that.connectDeviceId, serviceId: that.connectDevice.uuid, characteristicId: that.characteristics[i].uuid, state: true, type: 'notification', success: (res) => { //连接成功 console.log('启用notify成功', res) }, fail: (err) => { //连接失败 console.log('启用notify失败', err) } }) } } //跳转到通信界面 uni.navigateTo(...) },上面就是全部的代码段,大神帮忙看看到底啥问题....