12、发送信息到蓝牙(writeBLECharacteristicValue)

更新时间:2023-07-01 19:24:06 阅读: 评论:0

12、发送信息到蓝⽛(writeBLECharacteristicValue)wx.writeBLECharacteristicValue(Object object)
基础库 1.1.0 开始⽀持,低版本需做。
向低功耗蓝⽛设备特征值中写⼊⼆进制数据。注意:必须设备的特征值⽀持 write 才可以成功调⽤。
参数
Object object
属性类型默认值必填说明
deviceId string是蓝⽛设备 id
rviceId string是蓝⽛特征值对应服务的 uuid
characteristicId string是蓝⽛特征值的 uuid
value ArrayBuffer是蓝⽛设备特征值对应的⼆进制值
success function否接⼝调⽤成功的回调函数
fail function否接⼝调⽤失败的回调函数
complete function否接⼝调⽤结束的回调函数(调⽤成功、失败都会执⾏)
lanyatest.wxml代码:
<!--pages/lanyatest/lanyatest.wxml-->
<view class="contentview">
<view  class='myview' >
{{info}}
</view>
<button type="primary" class="button" bindtap="lanyatest1">1初始化蓝⽛</button>
<button type="primary" class="button" bindtap="lanyatest2">2获取蓝⽛状态</button>
<button type="primary" class="button" bindtap="lanyatest3">3搜索周边设备</button>
搬家对联大全
<button type="primary" class="button" bindtap="lanyatest4">4获取所有设备</button>
<block wx:for="{{devices}}" wx:key="{{test}}">
<button type="primary" class="button" id="{{item.deviceId}}" style='background-color:red' bindtap="lanyaconnect">5连接{{item.name}}        </button>  </block>
<button type="primary" class="button" bindtap="lanyatest6">6停⽌搜索周边蓝⽛设备</button>
<button type="primary" class="button" bindtap="lanyatest7">7获取所有rvice</button>
<button type="primary" class="button" bindtap="lanyatest8">8获取所有特征值</button>
<button type="primary" class="button" bindtap="lanyatest9">9启⽤特征值变化notify</button>
<button type="primary" class="button" bindtap="lanyatest10">10接收蓝⽛返回消息</button>
<view class="ction">
<input placeholder='请输⼊要发送的信息' bindinput='getmsg'/>
</view>
水发鱿鱼
<button type="primary" class="button" bindtap="lanyatest11">11微信发送消息</button>
</view>
lanyatest.js代码:
// pages/lanyatest/lanyatest.js
Page({
/**
* 页⾯的初始数据
*/
data: {
info:"未初始化蓝⽛适配器",
connectedDeviceId:"",
deviceId:"",
rvices:"",
rvicesUUID:"0000ff00-0000-1000-8000-00805f9b34fb",
rviceId:"",
notifyCharacteristicsId:"",
writeCharacteristicsId: "",纨绔子弟的意思
ndmsg:"",
},
lanyatest1(event){
var that = this;
wx.openBluetoothAdapter({
success: function (res) {
console.log('初始化蓝⽛适配器成功')
//页⾯⽇志显⽰
that.tData({
info: '初始化蓝⽛适配器成功'
})
},
圆柱体的侧面积公式fail: function (res) {
console.log('请打开蓝⽛和定位功能')
console.log('请打开蓝⽛和定位功能')
that.tData({
info: '请打开蓝⽛和定位功能'包装网
})
}
})
},
lanyatest2(event){
var that = this;
success: function (res) {
//打印相关信息
console.log(JSON.Msg) + "\n蓝⽛是否可⽤:" + res.available);
that.tData({
info: JSON.Msg) +"\n蓝⽛是否可⽤:" + res.available
})
},
fail: function (res) {
//打印相关信息
console.log(JSON.Msg) + "\n蓝⽛是否可⽤:" + res.available);
that.tData({
info: JSON.Msg) + "\n蓝⽛是否可⽤:" + res.available
})
}
})
},
lanyatest3(event){
var that = this;
wx.startBluetoothDevicesDiscovery({
rvices: ['FEE7'], //如果填写了此UUID,那么只会搜索出含有这个UUID的设备,建议⼀开始先不填写或者注释掉这⼀句      success: function (res) {
that.tData({
info: "搜索设备" + JSON.stringify(res),
})
鸡蛋炒米饭
console.log('搜索设备返回' + JSON.stringify(res))
}
})
},
lanyatest4(event){
var that = this;
success: function (res) {
that.tData({
info: "设备列表\n" + JSON.stringify(res.devices),
info: "设备列表\n" + JSON.stringify(res.devices),
devices: res.devices
})
console.log('搜设备数⽬:' + res.devices.length)
console.log('设备信息:\n' + JSON.stringify(res.devices)+"\n")
}
})
},
lanyaconnect(event){
var that = this;
deviceId: event.currentTarget.id,
success: function (res) {
console.log('调试信息:' + Msg);
that.tData({
connectedDeviceId: event.currentTarget.id,
info: "MAC地址:" + event.currentTarget.id  + '  调试信息:' + Msg,
})
},
fail: function () {
console.log("连接失败");
},
})
},
lanyatest6(event){
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log("停⽌搜索" + JSON.Msg));
that.tData({
info: "停⽌搜索"  + JSON.Msg),
})
}
})
},
lanyatest7(event){
var that = this;
// 这⾥的 deviceId 需要在上⾯的 getBluetoothDevices 或 onBluetoothDeviceFound 接⼝中获取      deviceId: tedDeviceId,
success: function (res) {
console.log('rvices UUID:\n', JSON.stringify(res.rvices));
for (var i = 0; i < res.rvices.length; i++) {
console.log("第"+(i+1) + "个UUID:" + res.rvices[i].uuid+"\n")
}
that.tData({
rvices: res.rvices,
info: JSON.stringify(res.rvices),
})
}
})
})
},
lanyatest8(event){
var that = this;
奚谷var myUUID = that.data.rvicesUUID;//具有写、通知属性的服务uuid
console.log('UUID' + myUUID)
// 这⾥的 deviceId 需要在上⾯的接⼝中获取
deviceId: tedDeviceId,
// 这⾥的 rviceId 需要在上⾯的接⼝中获取
rviceId: myUUID,
success: function (res) {
console.log("%c getBLEDeviceCharacteristics", "color:red;");
for (var i = 0; i < res.characteristics.length; i++) {
console.log('特征值:' + res.characteristics[i].uuid)
if (res.characteristics[i].ify) {
console.log("notifyServicweId:", myUUID);
console.log("notifyCharacteristicsId:", res.characteristics[i].uuid);
that.tData({
notifyServicweId: myUUID,
notifyCharacteristicsId: "0000ff01-0000-1000-8000-00805f9b34fb",//⼿动设置notifyCharacteristicsId为这个UUID,为了⽅便写死在这⾥
})
}
if (res.characteristics[i].properties.write) {
console.log("writeServicweId:", myUUID);
console.log("writeCharacteristicsId:", res.characteristics[i].uuid);
that.tData({
writeServicweId: myUUID,
//writeCharacteristicsId: res.characteristics[i].uuid,
writeCharacteristicsId: "0000ff02-0000-1000-8000-00805f9b34fb",//⼿动设置writeCharacteristicsId为这个UUID,为了⽅便写死在这⾥            })
}
}
嗫嚅怎么读console.log('device getBLEDeviceCharacteristics:', res.characteristics);
that.tData({
msg: JSON.stringify(res.characteristics),
})
},
fail: function () {
console.log("fail");
},
})
},
lanyatest9(event){
var that = this;
var notifyServicweId = that.data.rvicesUUID;  //具有写、通知属性的服务uuid
var notifyCharacteristicsId = ifyCharacteristicsId;
console.log("启⽤notify的rviceId", notifyServicweId);
console.log("启⽤notify的notifyCharacteristicsId", notifyCharacteristicsId);

本文发布于:2023-07-01 19:24:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1063520.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设备   特征值   属性   搜索   定位   回调   函数   开始
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图