zoukankan      html  css  js  c++  java
  • 快应用---使用蓝牙

    1、初始化蓝牙模块

          为了使用蓝牙功能,需要先初始化蓝牙模块。除了设置状态监听以外,所有的蓝牙接口都需要在初始化完成以后才能正常使用;

          //初始化蓝牙模块

          bluetooth.openAdapter({

               //是否打开系统蓝牙开关,默认false

               operateAdapter: true,

               success:function(){

                     console.log('success');

               },

               fail:function(data,code){

                    console.log('handling fail,code = ${code}');

                    if(code===10001) {

                        //蓝牙未打开,提示用户打开蓝牙

                    }

               },

               complete: function(){

                    console.log('complete');

               }

          })

    2、搜索周围设备

          你可以通过搜索操作,发现周围的低功耗蓝牙设备。在进行扫描前,你需要注册设备发现回调以及时接收搜索结果

           //在扫描之前先注册设备发现回调

           bluetooth.ondevicefound = function(data){

                console.log('new device list has founded');

                data.devices.forEach(device=>{

                        //发现所需设备后停止扫描

                        bluetooth.stopDevicesDiscovery();

                        _this.deviceId = device.deviceId;

                        console.log('handling finding new device: ${JSON.stringify(device)}');

                        console.log('handling advertisData = ${_this.ab2hex(device.advertisData)}');

                        for(let key in device.serviceData){

                              console.log('handling serviceData:uuid = ${key},serviceData=${_this.ab2hex(device.serviceData[key])}')

                        }

                })

           };

           //开始扫描

            bluetooth.startDevicesDiscovery({

                 //指定设备uuid,支持16-bit,32-bit,128-bit uuid,不填则扫描周围所有设备

                 services:['1105'],

                  //是否允许重复设备上报,如果不需要监听广播包数据,建议不设置此项,默认值为false

                  allowDuplicatesKey: false,

                  //上报间隔,单位毫秒,为0即立即上报,默认值为0

                  interval:1000,

                  success:function(){

                        console.log('success');

                  }

            })

          3、连接

           在操作设备前,需要先连接设备,连接操作可以通过deviceId直接进行,扫描并不是必要的操作。在连接之前,你可以注册设置连接状态接口来监听设备断连状态;

           //注册设备连接状态监听

           bluetooth.onbleconnectionstatechange = function(data){

                  console.log('handling device state change:deviceId = ${data.deviceId},connected =${data.connected}');

                  //更新设备连接状态

                   _this.connected = data.connected;

                  if(data.connected){

                       //目标设备连接后,获取服务列表

                        _this.getServices();

                  }else{

                       //做断开连接的相关操作,比如重新连接,或者重新扫描

                  }

           }

           //连接设备

            bluetooth.createBLEConnection({

                  deviceId: _this.deviceId,

                  success:function(){

                   },

                  fail:function(data,code){

                  },

                  complete:function(){}

           })

           4、获取设备服务信息

            bluetooth.getBLEDeviceServices({

                  deviceId: _this.deviceId,

                  success: function(data){

                      data.services.forEach(service = >{ 

                             //获取需要的服务,可以根据设备定义的uuid筛选

                            if(service.isPrimary){

                                 _this.serviceId = service.uuid;

                                 //获取特征值列表

                                 _this.getCharacteristices();

                            }

                      })

                  },

                 fail:function(data,code){},

                 complete(){}

            })

  • 相关阅读:
    [转]MySql 5.7关键字和保留字-附表
    [转]超链接标签简单的几个样式属性
    layui table 前台数字格式保留两位小数,不足补0(mysql 数据库)
    [转]Object.keys()和for in的排序问题
    [转]对form:input标签中的数字进行格式化
    [转]bootstrap table 动态列数
    [转]bootstrap的table插件动态加载表头
    [转]【MyBatis】Decimal映射到实体类出现科学计数法问题
    [转]MySQL函数大全 及用法示例
    [转]mysql update case when和where之间的注意事项
  • 原文地址:https://www.cnblogs.com/sunqq/p/11237179.html
Copyright © 2011-2022 走看看