小程序 wx.writeBLECharacteristicValue 向蓝牙写入数据报错 10004 noservice

文章目录

    使用微信小程序官方提供的操作蓝牙设备示例代码

    https://developers.weixin.qq.com/miniprogram/dev/framework/device/bluetooth.html

    向低功耗蓝牙设备写入数据时,发现蓝牙设备没有收到数据。

    为 wx.writeBLECharacteristicValue 增加了成功失败回调之后

    wx.writeBLECharacteristicValue({
    	deviceId: this._deviceId,
    	serviceId: this._deviceId,
    	characteristicId: this._characteristicId,
    	value: buffer,
    	success: (e) => {
    		console.log("success ...");
    		console.log(e);
    	},
    	fail: (e) => {
    		console.log("fail ...");
    		console.log(e);
    	},
    })
    

    发现,实际上是写入失败。错误码 10004,错误信息 noservice。

    从错误码信息里看

    https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-ble/wx.writeBLECharacteristicValue.html

    10004 no service 代表没有找到指定服务。

    仔细看了一下官方提供的代码,发现

    serviceId: this._deviceId,
    

    serviceId 被赋值成了 deviceId … 差评!

    修改之后:

    wx.writeBLECharacteristicValue({
    	deviceId: this._deviceId,
    	//serviceId: this._deviceId,
    	serviceId: this._serviceId,
    	characteristicId: this._characteristicId,
    	value: buffer,
    	success: (e) => {
    		console.log("success ...");
    		console.log(e);
    	},
    	fail: (e) => {
    		console.log("fail ...");
    		console.log(e);
    	},
    })
    

    再次尝试向蓝牙设备写入数据,成功。

    所以,必须每次操作都通过错误码来判断是否成功。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式