实例介绍
【实例简介】
【实例截图】
【核心代码】
// pages/bleble/ble.js var TAG = 'ble'; //监护正在运行的状态: 0,停止或初始状态 1,正在搜索蓝牙,2,正在接收数据 var mJianHuStatus = 0; //要显示的数据 var mStreee = new Array(); /////////////////////////////////////////////////////////////////////////// ////////////只需要在这里更改你的参数即可 //////////////////////////////////////////////////////////////////////////// //主服务或者 外设 var main_uuid = ''; // 开始命令 var start_code = '' //停止命令 var stop_code = '' //需要查找的服务 var write_service = '' //////////////////////////////////////////////////////////////// //可加我微信好友单聊 w857408545 ///////////////////////////////////////////////////////////// Page({ /** * 页面的初始数据 */ data: { startStopBtnText: '开始', showdata: [], toview: 't0' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { //观察蓝牙状态 this.watchBleStatus(); this.setShowData('123'); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { //打开蓝牙适配器 console.log("页面显示时打开蓝牙适配器"); this.openBleAdapter(); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, ///////////////////////////////////////////////////////////// ////////蓝牙各种操作 ///////////////////////// /** * 观察蓝牙各种状态 */ watchBleStatus: function () { var that = this; wx.onBluetoothAdapterStateChange(function (res) { //蓝牙已经关闭 if (res.available == false) { that.data.switchStatus = false; console.log(TAG, "手机蓝牙关闭") that.setShowData("手机蓝牙关闭"); } else { if (res.discovering == true) { console.log(TAG, "正在搜索设备..") that.setShowData("正在搜索设备.."); } else { console.log(TAG, "蓝牙已打开,未搜索设备"); that.setShowData("蓝牙已打开,未搜索设备"); that.data.switchStatus = true; } } }) wx.onBluetoothDeviceFound(function (devices) { that.data.devices = devices.devices[0]; that.data.deviceID = devices.devices[0].deviceId; //断开蓝牙搜索 that.stopBleSearch(0); //开始进厅设备连接情况 that.autoConnect(); }) }, /** * 搜索设备 */ searchDevice: function (e) { var that = this if (that.data.switchStatus) { //正在搜索蓝牙,还没有搜索到设备,则停止搜索 if (mJianHuStatus == 1) { that.stopBleSearch(1); //断开蓝牙连接 that.closeBleAdapter(); } else if (mJianHuStatus == 2)//正在接收数据,则断开蓝牙的链接 { //发送停止监护包 //停止命令 that.writeBleData(stop_code); // console.log("开始断开蓝牙的链接 mJianHuStatus==", mJianHuStatus); //断开蓝牙连接 wx.closeBLEConnection({ deviceId: that.data.deviceID, success: function (res) { mJianHuStatus = 0; that.setData( { startStopBtnText: '开始', mBJianHuFinished: true, } ) that.closeBleAdapter(); console.log("蓝牙断开成功", mJianHuStatus); that.setShowData("蓝牙断开成功"); }, }) } else if (mJianHuStatus == 0)//开始搜索设备 { //that.setShowData(''); that.clearbuffer(); wx.openBluetoothAdapter({ success: function (res) { console.log("打开蓝牙适配成功"); that.setShowData("打开蓝牙适配成功"); setTimeout(that.dontfindDevice, 30000); ///20s之后搜索不到设备,则停止搜索 wx.startBluetoothDevicesDiscovery({ services: [main_uuid], success: function (res) { mJianHuStatus = 1; that.setData ( { startStopBtnColor: '#ff0000', startStopBtnText: '停止' } ) }, fail: function (res) { mJianHuStatus = 0; that.setData ( { startStopBtnColor: '#ee6565', startStopBtnText: '开始' } ) } }) }, }) } } else { console.log('没有打开蓝牙适配器,请打开蓝牙适配器!'); that.setShowData('没有打开蓝牙适配器,请打开蓝牙适配器!'); } }, /** * @20s 之后如果没有找到设备,则停止搜索 */ dontfindDevice: function () { // console.log("触发定时器", mJianHuStatus); if (mJianHuStatus == 1) { this.stopBleSearch(1); console.log("没有找到设备,请重新搜索!", mJianHuStatus); that.setShowData('没有找到设备,请重新搜索!'); } }, /** * 向蓝牙通道里面写数据 */ writeBleData: function (data) { var that = this; var hex = data var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })) var buffer1 = typedArray.buffer console.log("发送的数据==" typedArray) that.setShowData("发送的数据==" typedArray); wx.writeBLECharacteristicValue({ deviceId: that.data.connectedDeviceId, serviceId: that.data.serviceId, characteristicId: that.data.writecharacId, value: buffer1, success: function (res) { console.log('writeBLECharacteristicValue success data==', res) }, fail: function (res) { console.log('writeBLECharacteristicValue failed data==', res) }, complete: function (res) { console.log('writeBLECharacteristicValue complete', res) } }) }, /** * 打开蓝牙适配器 */ openBleAdapter: function () { var that = this; wx.openBluetoothAdapter({ success: function (res) { that.data.switchStatus = true; console.log("打开蓝牙适配器成功"); that.setShowData("打开蓝牙适配器成功"); }, fail: function (res) { that.data.switchStatus = false; console.log("打开蓝牙适配器失败"); that.setShowData('打开蓝牙适配器失败'); } }) }, /** * 停止蓝牙搜索 * f:1手动停止搜索设备 0;找到设备后,主动停止搜索设备 */ stopBleSearch: function (f) { var that = this; wx.stopBluetoothDevicesDiscovery({ success: function (res) { if (f == 1) { mJianHuStatus = 0; that.setData( { startStopBtnColor: '#ee6565', //开始搜索和停止搜索文字 startStopBtnText: '开始' } ) } }, fail: function (res) { console.log('停止搜索设备失败', res) that.setShowData('停止搜索设备失败'); } }) }, /** * 关闭蓝牙适配器 */ closeBleAdapter: function () { var that = this; wx.closeBluetoothAdapter({ success: function (res) { console.log(TAG, "关闭蓝牙适配器成功"); wx.showToast({ title: '关闭蓝牙适配器成功', }) }, fail: function (res) { console.log(TAG, "关闭蓝牙适配器失败"); wx.showToast({ title: '关闭蓝牙适配器失败', }) } }) }, /** * 自动连接设备 * */ autoConnect: function () { var that = this wx.createBLEConnection({ deviceId: that.data.deviceID, success: function (res) { //设备连接成功后 连接服务器 that.setData({ connectedDeviceId: that.data.deviceID, }) //获取各种服务 that.getTargetService(); }, fail: function (res) { mJianHuStatus = 0; that.setData( { startStopBtnColor: '#ee6565', startStopBtnText: '开始' } ) that.closeBleAdapter(); wx.showToast({ title: '连接设备失败,请重新搜索!', }) } }) }, /** * 获取目标服务 * **/ getTargetService: function () { var that = this wx.getBLEDeviceServices({ deviceId: that.data.connectedDeviceId, success: function (res) { var count = res.services.length; for (var i = 0; i < count; i ) { //找到相关服务 然后退出(一般以一个服务为主) if (res.services[i].uuid.indexOf(write_service) >= 0) { that.getTargetCharacteric(res.services[i].uuid); break; } } } }) }, /** * 获取目标 特征 * **/ getTargetCharacteric: function (serviceid) { var that = this wx.getBLEDeviceCharacteristics({ deviceId: that.data.connectedDeviceId, serviceId: serviceid, success: function (res) { console.log('设置写监听:', res.characteristics[0].uuid) that.setData({ serviceId: serviceid, writecharacId: res.characteristics[0].uuid }) //写入开始命令 that.writeBleData(start_code); setTimeout(function () { wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 deviceId: that.data.connectedDeviceId, serviceId: serviceid, characteristicId: res.characteristics[0].uuid, success: function (res) { console.log('监听蓝牙notify服务成功 characteristicId') mJianHuStatus = 2; that.setData( { mBJianHuFinished: false, } ) that.recieveequ(); }, fail: function (res) { mJianHuStatus = 0; } }) }, 3000) } }) }, /** * 监听蓝牙获取的数据 * */ recieveequ: function () { var that = this wx.onBLECharacteristicValueChange(function (characteristic) { let buffer = characteristic.value // console.log("接收数据为:hex==" Array.prototype.map.call(new Uint8Array(buffer), x => ('00' x.toString(16)).slice(-2)).join('')) var tmpStrr = "接收数据为:hex==" Array.prototype.map.call(new Uint8Array(buffer), x => ('00' x.toString(16)).slice(-2)).join(''); that.setShowData(tmpStrr); }) }, /** * 设置要显示的数据 */ setShowData: function (tmpStr) { var that = this; var tmpddd = { txt: tmpStr, } mStreee.push(tmpddd); that.setData( { showdata: mStreee, toview: 't' (mStreee.length - 1), } ) }, /** * 清空数据 */ clearbuffer: function () { var that = this; mStreee = new Array(); that.setData( { showdata: mStreee, toview: 't0', } ) } })
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论