网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

微信小程序怎么使用蓝牙小插件-创新互联

今天小编给大家分享一下微信小程序怎么使用蓝牙小插件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

成都创新互联公司是由多位在大型网络公司、广告设计公司的优秀设计人员和策划人员组成的一个具有丰富经验的团队,其中包括网站策划、网页美工、网站程序员、网页设计师、平面广告设计师、网络营销人员及形象策划。承接:成都网站建设、网站建设、网站改版、网页设计制作、网站建设与维护、网络推广、数据库开发,以高性价比制作企业网站、行业门户平台等全方位的服务。

bluetooth.js

function BLE(options) {
 this.options = options || { locator: {} };
}

function ab2hex(buffer) {
 const hexArr = Array.prototype.map.call(
  new Uint8Array(buffer),
  function (bit) {
   return ('00' + bit.toString(16)).slice(-2)
  }
 )
 return hexArr.join('-')
};

BLE.prototype = {
 open: function (callback) {//打开适配器 成功 调用callback();
  wx.openBluetoothAdapter({
   success(res) {
    if (typeof callback != "undefined") {
     callback();
    }
   },
   fail(res) {
    console.log(res)
   }
  })
 },
 scan: function (callback) {//扫描设备 成功 调用callback(ls);
  wx.startBluetoothDevicesDiscovery({
   success(res) {
    wx.getBluetoothDevices({
     success(res) {
      console.log(res);
      var ls = [];
      for (var i = 0; i < res.devices.length; i++) {
       console.log(res.devices[i].name);
       ls[i] = {
        deviceId: res.devices[i].deviceId,
        name: res.devices[i].name
       };
      }
      if (typeof callback!= "undefined"){
       callback(ls);
      }
      
     }
    })
   },
   fail(res) {
    console.log(res)
    return 0;
   }
  })
 },
 link: function (deviceId, callback) {//连接设备 成功 调用callback();
  wx.createBLEConnection({
   // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
   deviceId: deviceId,
   success(res) {
    console.log(res);
    wx.getBLEDeviceServices({
     // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
     deviceId: deviceId,
     success(res) {
      console.log(res);

      wx.getBLEDeviceCharacteristics({
       // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
       deviceId: deviceId,
       // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
       serviceId: ' ',
       success(res) {
        console.log(res)
        if (typeof callback != "undefined") {
         callback();
        }
       }
      })
     }
    })
   },
   fail(res) {
    console.log(res)
   }
  })
 },
 write: function (deviceId, value, callback) {//写入数据 成功 调用callback();
  const buffer = new ArrayBuffer(value.length);
  const dataView = new DataView(buffer);
  for (var i = 0; i < value.length; i++) {
   dataView.setUint8(i, value[i].charCodeAt());
  }
  console.log(buffer);
  console.log(buffer);
  wx.getBLEDeviceServices({
   // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
   deviceId: deviceId,
   success(res) {
    console.log(res);

    wx.getBLEDeviceCharacteristics({
     // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
     deviceId: deviceId,
     // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
     serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B',
     success(res) {
      wx.writeBLECharacteristicValue({
       deviceId: deviceId,
       serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B',
       characteristicId: 'BEB5483E-36E1-4688-B7F5-EA07361B26B8',
       value: buffer,
       success: function (res) {
        console.log(res);
        if (typeof callback != "undefined") {
         callback();
        }
       },
       fail: function (res) {
        console.log(res);
       }
      })
     }
    })
   }
  })

  
 },
 read: function (deviceId, callback) {//读取数据 成功 调用callback(xmlString);
  wx.onBLECharacteristicValueChange(function (characteristic) {
   var board = ab2hex(characteristic.value);
   var bigData = board.split('-');
   var result = [];
   for (var i = 0; i < bigData.length; i++) {
    result.push(String.fromCharCode('0X' + bigData[i]));
   }
   var xmlString = result.join('');
   if (typeof callback != "undefined") {
    callback(xmlString);
   }
  });
  wx.readBLECharacteristicValue({
   // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
   deviceId: deviceId,
   // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
   serviceId: '4FAFC201-1FB5-459E-8FCC-C5C9C331914B',
   // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
   characteristicId: 'BEB5483E-36E1-4688-B7F5-EA07361B26B8',
   success(res) {
    // console.log('readBLECharacteristicValue:', res.errCode);
    console.log(res);
   },
   fail(res) {
    console.log(res);
   }
  })
 },
 close: function (deviceId, callback) {//关闭蓝牙 成功 调用callback();
  wx.closeBLEConnection({
   deviceId: deviceId,
   success(res) {
    console.log(res);
    if (typeof callback != "undefined") {
     callback();
    }
   }
  })
  // wx.closeBluetoothAdapter({
  //  success(res) {
  //   console.log(res);
  //   if (typeof callback != "undefined"){
  //    callback();
  //   }
  //  }
  // }) 
 }
}

exports.BLE = BLE;

index.js

const Ble = require('../../lib/bluetooch/bluetooch');
var ble = new Ble.BLE();

以上就是“微信小程序怎么使用蓝牙小插件”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。


文章题目:微信小程序怎么使用蓝牙小插件-创新互联
标题URL:http://bjjierui.cn/article/godjj.html

其他资讯