wx.closeBLEConnection(Object object)

基础库 1.1.0 开始支持,低版本需做兼容处理。

断开与低功耗蓝牙设备的连接。

参数

Object object

属性 类型 默认值 必填 说明
deviceId string   用于区分设备的 id
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

添加的代码

  lanyatest12(event){var that = this;wx.closeBLEConnection({deviceId: that.data.connectedDeviceId,success: function (res) {that.setData({connectedDeviceId: "",})console.log('断开蓝牙设备成功:' + res.errMsg)},fail:function(res){console.log('断开蓝牙设备失败:' + res.errMsg)}})}

完整代码

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获取所有service</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="section"><input placeholder='请输入要发送的信息' bindinput='getmsg'/></view><button type="primary" class="button" bindtap="lanyatest11">11微信发送消息</button><button type="primary" class="button" bindtap="lanyatest12">12断开蓝牙设备</button></view>

lanyatest.js代码:

// pages/lanyatest/lanyatest.js
Page({/*** 页面的初始数据*/data: {info:"未初始化蓝牙适配器",connectedDeviceId:"",deviceId:"",services:"",servicesUUID:"0000FF00-0000-1000-8000-00805F9B34FB",serviceId:"",notifyCharacteristicsId:"",writeCharacteristicsId: "",sendmsg:"",res:"",},lanyatest1(event){var that = this;wx.openBluetoothAdapter({success: function (res) {console.log('初始化蓝牙适配器成功')//页面日志显示that.setData({info: '初始化蓝牙适配器成功'})},fail: function (res) {console.log('请打开蓝牙和定位功能')that.setData({info: '请打开蓝牙和定位功能'})}})},lanyatest2(event){var that = this;wx.getBluetoothAdapterState({success: function (res) {//打印相关信息console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);that.setData({info: JSON.stringify(res.errMsg) +"\n蓝牙是否可用:" + res.available})},fail: function (res) {//打印相关信息console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);that.setData({info: JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available})}})},lanyatest3(event){var that = this;wx.startBluetoothDevicesDiscovery({//services: ['FEE7'], //如果填写了此UUID,那么只会搜索出含有这个UUID的设备,建议一开始先不填写或者注释掉这一句services: ['FF00'], //如果填写了此UUID,那么只会搜索出含有这个UUID的设备,建议一开始先不填写或者注释掉这一句success: function (res) {that.setData({info: "搜索设备" + JSON.stringify(res),})console.log('搜索设备返回' + JSON.stringify(res))}})},lanyatest4(event){var that = this;wx.getBluetoothDevices({success: function (res) {that.setData({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;wx.createBLEConnection({deviceId: event.currentTarget.id,success: function (res) {console.log('调试信息:' + res.errMsg);that.setData({connectedDeviceId: event.currentTarget.id,info: "MAC地址:" + event.currentTarget.id  + '  调试信息:' + res.errMsg,})},fail: function () {console.log("连接失败");},})},lanyatest6(event){var that = this;wx.stopBluetoothDevicesDiscovery({success: function (res) {console.log("停止搜索" + JSON.stringify(res.errMsg));that.setData({info: "停止搜索"  + JSON.stringify(res.errMsg),})}})},lanyatest7(event){var that = this;wx.getBLEDeviceServices({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,success: function (res) {console.log('services UUID:\n', JSON.stringify(res.services));for (var i = 0; i < res.services.length; i++) {console.log("第"+(i+1) + "个UUID:" + res.services[i].uuid+"\n")}that.setData({services: res.services,info: JSON.stringify(res.services),})}})},lanyatest8(event){var that = this;var myUUID = that.data.servicesUUID;//具有写、通知属性的服务uuidconsole.log('UUID:' + myUUID)wx.getBLEDeviceCharacteristics({// 这里的 deviceId 需要在上面的接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 接口中获取serviceId: 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].properties.notify) {console.log("notifyServicweId:", myUUID);console.log("notifyCharacteristicsId:", res.characteristics[i].uuid);that.setData({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.setData({writeServicweId: myUUID,//writeCharacteristicsId: res.characteristics[i].uuid,writeCharacteristicsId: "0000FF02-0000-1000-8000-00805F9B34FB",//手动设置writeCharacteristicsId为这个UUID,为了方便写死在这里})}}console.log('device getBLEDeviceCharacteristics:', res.characteristics);that.setData({msg: JSON.stringify(res.characteristics),})},fail: function (res) {console.log(res);},})},//必须先启用notifyCharacteristicsId才能监听characteristicValueChange事件lanyatest9(event){var that = this;var notifyServicweId = that.data.servicesUUID;  //具有写、通知属性的服务uuidvar notifyCharacteristicsId = that.data.notifyCharacteristicsId;console.log("启用notify的serviceId", notifyServicweId);console.log("启用notify的notifyCharacteristicsId", notifyCharacteristicsId);wx.notifyBLECharacteristicValueChange({state: true, // 启用 notify 功能deviceId: that.data.connectedDeviceId,// 这里的 serviceId 就是that.data.servicesUUIDserviceId: notifyServicweId,characteristicId: that.data.notifyCharacteristicsId,success: function (res) {//console.log('notifyBLECharacteristicValueChange success', res.errMsg)//var msg = '启动notify:' + res.errMsgthat.setData({info: "开启notify成功"})},fail: function (res) {console.log('启动notify:' + res.errMsg);},})},lanyatest10(event){var that = this;console.log("开始接收数据");wx.onBLECharacteristicValueChange(function (res) {console.log("characteristicId:" + res.characteristicId)console.log("serviceId:" + res.serviceId)console.log("deviceId" + res.deviceId)console.log("Length:" + res.value.byteLength)console.log("hexvalue:" + ab2hex(res.value))that.setData({info: that.data.info + ab2hex(res.value)})})},lanyatest11(event){var that = thisvar hex = that.data.sendmsg  //要发送的信息console.log('要发送的信息是:'+hex)var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {return parseInt(h, 16)}))console.log(typedArray)var buffer1 = typedArray.bufferwx.writeBLECharacteristicValue({deviceId: that.data.connectedDeviceId,serviceId: that.data.servicesUUID,characteristicId: that.data.writeCharacteristicsId,// 这里的value是ArrayBuffer类型value: buffer1,success: function (res) {console.log('写入成功', res.errMsg)},fail(res){console.log('写入失败', res.errMsg)}})},//获取输入框的数据getmsg(event){this.setData({sendmsg:event.detail.value})},lanyatest12(event){var that = this;wx.closeBLEConnection({deviceId: that.data.connectedDeviceId,success: function (res) {that.setData({connectedDeviceId: "",})console.log('断开蓝牙设备成功:' + res.errMsg)},fail:function(res){console.log('断开蓝牙设备失败:' + res.errMsg)}})}//我删除了自动生成的生命周期函数})// 微信官方给的ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {var hexArr = Array.prototype.map.call(new Uint8Array(buffer),function (bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join(',');
}

lanyatest.wxss代码:

/* pages/lanyatest/lanyatest.wxss */
.vertical{display: flex;flex-direction: column;
}/**index.wxss**/
.horizontal{display: flex;flex-direction: row;
}.btinfo{height:100px;
}.contentview {
margin: 0 10px;
}.button {
margin: 5px;
}.myview{height:200px;word-break:break-all;/* 自动换行 */
}

真机调试结果:

13、断开蓝牙设备连接(closeBLEConnection)相关推荐

  1. NB驱动建立MQTT连接和断开MQTT连接的代码实现

    文章目录 1 SIM7020建立MQTT连接和断开MQTT连接的AT指令 2 SIM7020连接阿里云的流程 3 代码实现 1 SIM7020建立MQTT连接和断开MQTT连接的AT指令 首先看下相关 ...

  2. 微信小程序对接蓝牙设备连接

    使用小程序的蓝牙功能与硬件设备进行连接相互传送数据指令,联调过程中发现一些问题,于是想着记录下来,方便以后查看! 一般使用蓝牙功能肯定是想连接某一个蓝牙设备,所以需要知道这个蓝牙设备的名称,一般来说都 ...

  3. android检测蓝牙设备连接不上,Android检查设备连接状态

    遇到一个检查蓝牙设备连接状态问题,困扰许久.在网上查询良久,尝试了多种方案,也没有很好的解决,最终经"高人指点",小有所获,在此小记. 网上查询的主要方法为以下两种: 1.getP ...

  4. C语言网络编程:close或者shutdown断开通信连接

    文章目录 前言 close函数介绍 shutdown函数介绍 前言 这里在主要通过实例进行描述close函数在网络编程中的使用 TCP编程模型中客户端或者服务器只要主动通过close发起断开连接的请求 ...

  5. mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案

    mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 参考文章: (1)mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 (2)https://www.cnbl ...

  6. Windows10熄屏自动断开WiFi连接解决方法

    遇到了好多次这样的情况,电脑只要是熄屏了,过不大会无线网络就会断开连接,那么有没有方法不让电脑熄屏或休眠时断网呢? 当然是有,出现这种问题的原因是Win10为了省电,所以在无人使用电脑时会自动断开网络 ...

  7. 客户端连接mysql 自动断开_MySql连接空闲8小时自动断开的原因及连接池配置方法...

    数据库连接超时时间查询 非交互式超时时间,如 JDBC 程序 show global variables like 'wait_timeout'; 交互式超时时间,如数据库工具 show global ...

  8. 如何减少mysql的连接时间_mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案...

    mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 更新时间:2012年11月29日 20:27:52   作者: MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,My ...

  9. mysql自动断开该连接解决方案

    mysql自动断开该连接解决方案 参考文章: (1)mysql自动断开该连接解决方案 (2)https://www.cnblogs.com/xxsl/p/7146292.html 备忘一下.

最新文章

  1. 如何一眼就分辨出本科、硕士和博士?
  2. helloworld 1 2結合版 (有意思)
  3. android SQL 语句
  4. linux源码编译安装lamp环境搭建,linux下源码包编译安装LAMP环境
  5. leetcode--数组(Medium2)
  6. android 浮动文字提示,怎么在Android中实现一个自由拖动并显示文字的悬浮框
  7. struts2 s:selects:radio 标签用法
  8. 【软件测试】负载测试与压力测试的区别
  9. Asp.Net中自以为是的Encode
  10. 使用 GetOleDbSchemaTable
  11. 高职对计算机课程要求,高职计算机课程对学生编程能力培养.doc
  12. 树莓派 HC-SRO4超声波测距模块的使用
  13. QT5 + MSVC + OpenCV4 配置
  14. 软件架构思想和系统架构图
  15. Java多线程系列--“JUC集合”04之 ConcurrentHashMap
  16. 【做小游戏在Godot中遇到的问题第一篇】
  17. Bug、Defect(缺陷)、Error、Fault、Exception的区别
  18. void main java_详细讲解Java中的main()方法
  19. 站点(e.g. Hexo Blog)提交百度搜索引擎收录实现SEO
  20. c++——简单计算器

热门文章

  1. Python实现json串比对并输出差异结果
  2. 野火多功能调试助手】-摄像头调试助手通讯协议
  3. 623.三个数的最大乘积
  4. 【计算机毕业设计】医生在线问诊答疑微信小程序
  5. java多线程累加计数
  6. 如何运行Python程序?
  7. 如何正确的理解“光速不变”
  8. Latex中如何输入特殊字符
  9. 为什么手机网速太慢_为什么手机网速太慢
  10. 桂电计算机网络老师,刘忆宁-桂电网络空间安全博士点获批,且行且珍惜