uniapp低功耗蓝牙在移动端使用较为平常,本文相较于官方文档介绍一下低功耗蓝牙的操作案例,即取即用。低功耗蓝牙虽工作原理与经典蓝牙类似,但是有着独特的架构体系,所以LE独立出来成为一种蓝牙形态。不过LE和经典蓝牙使用相同的2.4G无线电频率,可以共享同一个天线,组成双模蓝牙。

扫描蓝牙设备

Search() {var that = this;console.log("search:", that.searching);if (!that.searching) {//关闭现有的蓝牙连接uni.closeBluetoothAdapter({complete: function (res) {console.log("关闭蓝牙模块-----------------------------------------");console.log(res);//打开蓝牙适配uni.openBluetoothAdapter({success: function (res) {console.log("初始化蓝牙模块----------------------------------------");console.log(res);uni.getBluetoothAdapterState({success: function (res) {console.log("获取本机蓝牙适配器状态----------------------------------------");console.log(res);},});//开始搜索蓝牙设备uni.startBluetoothDevicesDiscovery({allowDuplicatesKey: false,success: function (res) {console.log("搜索设备-----------------------------------------");that.onBluetoothDeviceFound();that.searching = true;that.devicesList = [];},});},fail: function (res) {console.log("############");console.log(res);setTimeout(() => {uni.showModal({title: "温馨提示",content: "蓝牙未打开,请打开后再连接",showCancel: false,confirmColor: "#008fd6",});that.isSearch = false;}, 1000);},});},});} else {uni.stopBluetoothDevicesDiscovery({success: function (res) {console.log("停止搜索设备-----------------------------------------");console.log(res);that.searching = false;},});}
}

连接设备

ConnectByID(item, index) {var that = this;// console.log(item);that.connectedDeviceId = item.deviceId;uni.setStorageSync('pageName', item.name);uni.setStorageSync('connectedDeviceId', that.connectedDeviceId)uni.showLoading({title: '正在连接设备...',})uni.createBLEConnection({deviceId: item.deviceId,success(res) {uni.hideLoading()uni.showToast({title: '连接成功',duration: 2000,icon: "none"});// that.open()console.log("已连接设备----------------------------------------");that.getBLEDeviceServices(); //获取特征值},fail(res) {console.log(res)uni.hideLoading()uni.showModal({title: '提示',content: '连接失败',showCancel: false})}})// }},

获取设备UUID

         //获取蓝牙设备的服务uuid    //服务uuid可能有多个getBLEDeviceServices() {var that = this;setTimeout(() => {//获取数据可能会有延迟uni.getBLEDeviceServices({deviceId: that.connectedDeviceId,success: function(res) {console.log("获取蓝牙设备的服务uuid:" + JSON.stringify(res.services));that.services = res.services;that.serviceId = res.services[2].uuid;that.deviceId = that.connectedDeviceId;uni.setStorageSync("serviceId", that.serviceId);that.getBLEDeviceCharacteristics();},fail(res) {console.log(res);},});}, 3000);},

获取蓝牙特征值开始监听

// 根据服务uuid获取蓝牙特征值开始监听写入和接收getBLEDeviceCharacteristics() {let that = this;uni.getBLEDeviceCharacteristics({deviceId: that.connectedDeviceId,serviceId: that.serviceId,success: function(res) {console.log("获取蓝牙设备的特征" + JSON.stringify(res.characteristics));for (var i = 0; i < res.characteristics.length; i++) {if (res.characteristics[i].properties.notify === true) {that.characteristics = res.characteristics[i];that.characteristicId = res.characteristics[i].uuid;}if (res.characteristics[i].properties.write === true) {that.characteristics = res.characteristics[i];that.writeId = res.characteristics[i].uuid;}}uni.setStorageSync("characteristicId", that.characteristicId);uni.setStorageSync("writeId", that.writeId);that.notifyBLECharacteristicValueChange(); //7.0,开始侦听数据},});},

开启蓝牙数据监听

notifyBLECharacteristicValueChange() {var that = this;uni.notifyBLECharacteristicValueChange({state: true,deviceId: that.deviceId,serviceId: that.serviceId,characteristicId: that.characteristicId,success: function(res) {console.log("启用notify成功");that.onBLECharacteristicValueChange();},fail: function(res) {console.log("启用notify失败");},});},

接收处理数据

         //设备返回数据的接收onBLECharacteristicValueChange() {var that = this;uni.onBLECharacteristicValueChange((res) => {// 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,所以需要通过这个方法转换成字符串var resHex = that.ab2hex(res.value);// that.dealWithData(resHex);console.log("接收数据:" + resHex);var hexArray = stickyBag(resHex);for (let s of hexArray) {//处理数据}});},

监听寻找新设备

         //监听寻找到新设备的事件onBluetoothDeviceFound() {var that = this;console.log("打开设备监听");uni.onBluetoothDeviceFound(function(devices) {var reg = new RegExp(/[^\s]+/g);if (devices.devices) {if (devices.devices[0].name.match(reg)) {console.log(devices.devices[0]);that.devicesList.push(devices.devices[0]);}}});},

监听蓝牙状态

onBLEConnectionStateChange() {var that = this;uni.onBLEConnectionStateChange(function(res) {that.connected = res.connected;if (!that.connected) {console.log("蓝牙已断开");uni.createBLEConnection({// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接deviceId: that.connectedDeviceId,success(res) {that.getBLEDeviceServices();//获取该设备地址console.log("蓝牙重新连接" + JSON.stringify(res));},fail(res) {console.log("蓝牙重新连接失败" + JSON.stringify(res));},});} else {console.log("蓝牙连接成功");}});},

低功耗蓝牙相较于传统蓝牙,连接速度更快,接收成功后会自动断开,下一次连接的时候再激活就可以了

Uniapp低功耗蓝牙操作实例相关推荐

  1. 【uni-app学习】uni-app低功耗蓝牙采坑记录

    一.低功耗蓝牙的基础知识 1.低功耗蓝牙简介 蓝牙4.0及更高版本被称为蓝牙低功耗,其中蓝牙4.0标准包括传统的蓝牙模块部分和蓝牙低功耗模块部分,这是双模式标准.一般上位机都会有相应的蓝牙API可用, ...

  2. 十大蓝牙操作实例,助你深入解析蓝牙技术应用

    电路城十大蓝牙方案实例助你深入解析蓝牙技术应用 蓝牙在项目设计中的应用已经十分广泛,从电脑手机音箱等传统使用手段到车载蓝牙.智能家居等应用,蓝牙发挥的作用越来越大,能实现的功能也愈渐复杂,网罗电路城上 ...

  3. uniapp 低功耗蓝牙项目相关 (蓝牙发光计步犬牌)

    概况:手机APP发送16进制指令至蓝牙犬牌,犬牌响应返回结果,功能为计步,犬牌电量,版本号,系统时间,灯光颜色等. 步骤 打开蓝牙 连接设备 扩大传输MTU 获取蓝牙服务 获取蓝牙设备某个服务中所有特 ...

  4. Android使用低功耗蓝牙BLE进行简单通信

    一.蓝牙硬件操作 Android操作蓝牙需要申请蓝牙权限.定位权限,部分手机还必须要打开GPS才能使用. 蓝牙的打开.关闭.搜索,这部分内容只是简单的调用API就能实现,这里不做说明. 但是从连接开始 ...

  5. uniapp微信小程序实现连接低功耗蓝牙打印功能

    微信小程序项目中有使用到蓝牙连接打印,参考官方文档做了一个参考笔记,这样使用的时候就按着步骤查看. uni-app蓝牙连接 蓝牙: 1.初始化蓝牙 uni.openBluetoothAdapter(O ...

  6. Uni-App使用低功耗蓝牙连接血压仪测量

    1.Uni-APP蓝牙使用流程 在阅读这篇内容之前,建议您详细浏览一遍uni-app官方关于蓝牙和低功耗蓝牙(BLE)使用教程文档 uni-app官方低功耗蓝牙接口,如果您有微信小程序方面的开发经验, ...

  7. 在uniAPP中使用使用低功耗蓝牙通讯

    在uniAPP中使用使用低功耗蓝牙通讯 1.初始化蓝牙监听器 onLoad(){//蓝牙是否在扫描设备uni.onBluetoothAdapterStateChange((res)=>{cons ...

  8. uni-app、微信小程序低功耗蓝牙开发及使用

    引导 今天在这里记录分享一下低功耗蓝牙的使用方法和需要注意的地方 如果使用的微信小程序原生开发,使用方法是一样的,只需要把所有uni换成wx就行 例 wx.openBluetoothAdapter({ ...

  9. 基于uni-app的BLE低功耗蓝牙测试工具

    基于uni-app的BLE低功耗蓝牙测试工具 前言 开发环境:HBuilder X2.8.11,运行环境:微信小程序 本项目是从网上收集他人的源代码,经过测试.修改后的版本,在微信小程序上表现比较稳定 ...

最新文章

  1. 谷歌新模型刷新ImageNet纪录,第一作者是上海交大毕业生谢其哲
  2. 【CyberSecurityLearning 77】DC系列之DC-8渗透测试(Drupal)
  3. aspnet还有人用吗_别盲目跟风!理性分析:超火的小香风外套真的适合你吗?
  4. 常用工具备忘(更新中)
  5. CubeMX 的使用实例详细(04.6)- STM32F103的 - 定时器设定 - callback调用 - 实现1S的定时更新LED灯
  6. c语言编译器uwp版,哔哩哔哩UWP最新版下载 - 哔哩哔哩UWP版免费版(32位64位win10)安装下载v1.3.10.0 - QT软件园...
  7. 太原冶金技师学院计算机系,山西冶金技师学院专业都有什么
  8. struts2上传 zip和rar文件类型
  9. 给table表格表头添加斜线
  10. 流媒体下载的几种方法
  11. div 和 img 标签引入图片制作背景的小问题
  12. python sklearn metrics,在Python中sklearn.metrics.mean_squared_error越大越好(否定)?
  13. 分享一下微信域名防封方案
  14. 用递归和非递归求斐波那契数列
  15. tp6实现商城后台登录功能
  16. UA OPTI570 量子力学20 量子谐振子模型中量子态的相干性
  17. Git Commit emoji Guide
  18. 【中科蓝讯AB532X】自定义按键处理函数的实现
  19. java tcp 断开检测_TCP连接网线断开时的情况测试
  20. 字节跳动 前端校招 一二三面+hr面(2020-03)

热门文章

  1. 【Day12-Stream流Map集合】
  2. 软著中写源代码60页快速实现方法
  3. select标签默认选项
  4. Codeforces 743 D Chloe and pleasant prizes
  5. C语言:数据文件操作
  6. 51nod 1113 矩阵快速幂
  7. DAO数据访问对象(Data Access Object)
  8. php读取文件使用redis的pipeline导入大批量数据
  9. word文档找不到smartart_word2003SmartArt在哪里
  10. Jordan标准形(番外篇)——Jordan块的最小多项式