⼩程序蓝⽛通讯蓝⽛模块demo谷歌街景地图
公司项⽬⽤到蓝⽛和硬件通讯,APP正在开发,弄⼀个⼩程序蓝⽛通讯的demo,可能后期会有蓝⽛的项⽬,第⼀次搞,遇到2个坑:
1.安卓和苹果获取的硬件服务UUID顺序不同
2.⽬前⽤的这⼀版 “启⽤低功耗蓝⽛设备特征值变化时的 notify 功能”在安卓和苹果的测试机上都返回启动失败,其实是已经启动成功,在我同事安卓⼿机上返回的正常。
index.wxml
<!--index.wxml-->
<view class="content">
<text class="status">适配器状态:{{ status }}</text>
<text class="sousuo">是否搜索:{{ sousuo }}</text>
<text class="msg">消息:{{ msg }} </text>
<text class="msg1">消息:{{ msg1 }}</text>
<button type="primary" class="button" bindtap="lanya1">1初始化蓝⽛适配器</button>
<button type="primary" class="button" bindtap="lanya2">2本机蓝⽛适配状态</button>
<button type="primary" class="button" bindtap="lanya3">3搜索周边设备</button>
<button type="primary" class="button" bindtap="lanya4">4获取设备后在列表中连接</button>
<button type="primary" class="button" bindtap="lanya5">5停⽌搜索周边设备</button>
<button type="primary" class="button" bindtap="lanya6">6获取连接设备所有service</button>
<button type="primary" class="button" bindtap="lanya7">7获取连接设备所有特征值</button>
<button type="primary" class="button" bindtap="lanya8">8发送指定消息</button>
圣骑士怎么加点<button type="primary" class="button" bindtap="lanya9">9启⽤设备特征值变化时的notify</button>
<button type="primary" class="button" bindtap="lanya10">10接收消息</button>
<view class="section">
<text class="status">接收到消息:{{ jieshou }}</text>
</view>
<button type="primary" class="button" bindtap="lanya0">0断开蓝⽛连接</button>
</view>
<view class="venues_list">
<block wx:for="{{devices}}" wx:key="{{test}}">
<view class="venues_item">
<text class="status">设备名称:{{item.name}}</text>
<text class="status">设备ID:{{item.deviceId}}</text>
<text class="status">连接状态:{{connectedDeviceId == item.deviceId?"已连接":"未连接"}}</text>
<view class="section">
</view>
<view class="section">
<button type="warn" class="button" id="{{item.deviceId}}" bindtap="connectTO">连接</button>
</view>
</view>
</block>
</view>
index.js
//index.js
//获取应⽤实例
status: "",
sousuo: "",
connectedDeviceId: "", //已连接设备uuid
services: "", // 连接设备的服务
characteristics: "", // 连接设备的状态值
writeServicweId: "", // 可写服务uuid
writeCharacteristicsId: "",//可写特征值uuid
readServicweId: "", // 可读服务uuid
readCharacteristicsId: "",//可读特征值uuid
notifyServicweId: "", //通知服务UUid
notifyCharacteristicsId: "", //通知特征值UUID
inputValue: "",
characteristics1: "", // 连接设备的状态值
},
onLoad: function () {
if (wx.openBluetoothAdapter) {
wx.openBluetoothAdapter()
} else {
// 如果希望⽤户在最新版本的客户端上体验您的⼩程序,可以这样⼦提⽰
wx.showModal({
title: '提⽰',
content: '当前版本过低,⽆法使⽤该功能,请升级到最新版本后重试。' })
}
},
// 初始化蓝⽛适配器
lanya1: function () {
var that = this;
wx.openBluetoothAdapter({
success: function (res) {
that.setData({
msg: "初始化蓝⽛适配器成功!" + JSON.stringify(res),
})
酒井法子 蝴蝶//监听蓝⽛适配器状态
that.setData({
sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
})
}
})
},
// 本机蓝⽛适配器状态
lanya2: function () {
var that = this;
success: function (res) {
that.setData({
msg: "本机蓝⽛适配器状态" + "/" + JSON.Msg),
sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
//监听蓝⽛适配器状态
that.setData({
sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
})
}
lanya3: function () {
var that = this;
wx.startBluetoothDevicesDiscovery({
success: function (res) {
that.setData({
msg: "搜索设备" + JSON.stringify(res),
})
//监听蓝⽛适配器状态
that.setData({
sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
})
}
})
},
/
/ 获取所有已发现的设备
lanya4: function () {
var that = this;
success: function (res) {
//是否有已连接设备
success: function (res) {
console.log(JSON.stringify(res.devices));
that.setData({
connectedDeviceId: res.deviceId
})
}
})
that.setData({
msg: "搜索设备" + JSON.stringify(res.devices),
devices: res.devices,
})
//监听蓝⽛适配器状态
that.setData({
sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
})
}
})
},
//停⽌搜索周边设备
lanya5: function () {
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
that.setData({
msg: "停⽌搜索周边设备" + "/" + JSON.Msg), sousuo: res.discovering ? "在搜索。" : "未搜索。",
status: res.available ? "可⽤。" : "不可⽤。",
})
}
})
},
//连接设备
connectTO: function (e) {
var that = this;
deviceId: e.currentTarget.id,
success: function (res) {
console.Msg);
that.setData({
connectedDeviceId: e.currentTarget.id,
msg: "已连接" + e.currentTarget.id,
msg1: "",
})
},
fail: function () {
console.log("调⽤失败");
},
complete: function () {
console.log("调⽤结束");
}
})
console.log(tedDeviceId);
},
// 获取连接设备的service服务
lanya6: function () {
var that = this;高以翔身高
// 这⾥的 deviceId 需要在上⾯的 getBluetoothDevices 或 onBluetoothDeviceFound 接⼝中获取 deviceId: tedDeviceId,
success: function (res) {
console.log('device services:', JSON.stringify(res.services));
that.setData({
services: res.services,
msg: JSON.stringify(res.services),
})
}
})
},
//获取连接设备的所有特征值 for循环获取不到值
lanya7: function () {
var that = this;
// 这⾥的 deviceId 需要在上⾯的 getBluetoothDevices 或 onBluetoothDeviceFound 接⼝中获取 deviceId: tedDeviceId,
// 这⾥的 serviceId 需要在上⾯的 getBLEDeviceServices 接⼝中获取
serviceId: that.data.services[0].uuid,
success: function (res) {
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].ify) {
console.log("11111111", that.data.services[0].uuid);
console.log("22222222222222222", res.characteristics[i].uuid);
that.setData({
notifyServicweId: that.data.services[0].uuid,
notifyCharacteristicsId: res.characteristics[i].uuid,
})
品味女人}
if (res.characteristics[i].properties.write) {
that.setData({
writeServicweId: that.data.services[0].uuid,
writeCharacteristicsId: res.characteristics[i].uuid,
})
} else if (res.characteristics[i].ad) {
that.setData({
readServicweId: that.data.services[0].uuid,
readCharacteristicsId: res.characteristics[i].uuid,
})
}
console.log('device getBLEDeviceCharacteristics:', res.characteristics);
that.setData({
msg: JSON.stringify(res.characteristics),
})
},
fail: function () {
console.log("fail");
},
complete: function () {
console.log("complete");
}
})
// 这⾥的 deviceId 需要在上⾯的 getBluetoothDevices 或 onBluetoothDeviceFound 接⼝中获取 deviceId: tedDeviceId,
// 这⾥的 serviceId 需要在上⾯的 getBLEDeviceServices 接⼝中获取
serviceId: that.data.services[1].uuid,
success: function (res) {
for (var i = 0; i < res.characteristics.length; i++) {
张文慈图片
if (res.characteristics[i].ify) {
that.setData({
notifyServicweId: that.data.services[1].uuid,
notifyCharacteristicsId: res.characteristics[i].uuid,
})
}
if (res.characteristics[i].properties.write) {
that.setData({
writeServicweId: that.data.services[1].uuid,
writeCharacteristicsId: res.characteristics[i].uuid,
})
} else if (res.characteristics[i].ad) {
that.setData({
readServicweId: that.data.services[1].uuid,
readCharacteristicsId: res.characteristics[i].uuid,
})
}
}
console.log('device getBLEDeviceCharacteristics1:', res.characteristics);
that.setData({
msg1: JSON.stringify(res.characteristics),
})
},
fail: function () {
console.log("fail1");
},
complete: function () {
console.log("complete1");
}
})
},
//断开设备连接
lanya0: function () {
var that = this;
wx.closeBLEConnection({
deviceId: tedDeviceId,
success: function (res) {
that.setData({
connectedDeviceId: "",
发布评论