ESP32的SDK开发之blufi一键配网微信小程序端开发

        上一篇文章简单介绍了blufi配网协议,现在来讲讲再微信小程序端实现配网的控制,小程序开发是基于官方原生API开发。本人的微信小程序开发也是初学的,由于之前没接触过前端开发,软件写的很渣渣。
       蓝牙BLE的开发涉及到很多数据转换,这些算法都是网上搜罗的,就不一一讲解了,这里先列出常用的几种转换算法:

1、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('');
}

2、ArrayBuffer转为字符串

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}

3、16进制格式字符串转为ArrayBuffer

function strhex2ab(str) {
  var out = new ArrayBuffer(str.length/2);
  var u8a= new Uint8Array(out);
  for(var i =0 ; i

4、字符串转为ArrayBuffer

function str2ab(str){
  var out = new ArrayBuffer(str.length*2);
  var u16a= new Uint16Array(out);
  var strs = str.split("");
  for(var i =0 ; i

根据上一篇博文讲解的协议,这里软件实现一下:

  //ESP32的蓝牙配网命令接口
  bleSendCMD(CMD, subCMD, frameControl, payload){
    var ab =new ArrayBuffer(payload.length + 6)
    var u8array = new Uint8Array(ab);
    var LSB_Type = ((subCMD & 0x3f) << 2) | (CMD & 0x03);
    u8array[0] = LSB_Type;
    u8array[1] = frameControl;
    u8array[2] = this.data.bleSendSequence;
    u8array[3] = payload.length;
    for(let i=0; i

示例:发送连接wifi路由的密码到设备,其他的命令发送可以以此类推

  bindtap_buttom1(){
    let password = '1234567'
    let u8buffer = new Uint8Array(password.length)
    u8buffer[0] = password.charCodeAt(0)
    u8buffer[1] = password.charCodeAt(1)
    u8buffer[2] = password.charCodeAt(2)
    u8buffer[3] = password.charCodeAt(3)
    u8buffer[4] = password.charCodeAt(4)
    u8buffer[5] = password.charCodeAt(5)
    u8buffer[6] = password.charCodeAt(6)
    this.bleSendCMD(app.dataEnum.PACKAGE_VALUE,
                    app.dataEnum.SUBTYPE_SOFTAP_WIFI_PASSWORD,
                    0,
                    u8buffer
                    )
  },

官方例程涉及到的所有命令:

  dataEnum: {
    PACKAGE_VALUE: 0x01,
    SUBTYPE_NEG: 0x00,
    SUBTYPE_STA_WIFI_BSSID: 0x01,
    SUBTYPE_STA_WIFI_SSID: 0x02,
    SUBTYPE_STA_WIFI_PASSWORD: 0x03,
    SUBTYPE_SOFTAP_WIFI_SSID: 0x04,
    SUBTYPE_SOFTAP_WIFI_PASSWORD: 0x05,
    SUBTYPE_SOFTAP_MAX_CONNECTION_COUNT: 0x06,
    SUBTYPE_SOFTAP_AUTH_MODE: 0x07,
    SUBTYPE_SOFTAP_CHANNEL: 0x08,
    SUBTYPE_USERNAME: 0x09,
    SUBTYPE_CA_CERTIFICATION: 0x0a,
    SUBTYPE_CLIENT_CERTIFICATION: 0x0b,
    SUBTYPE_SERVER_CERTIFICATION: 0x0c,
    SUBTYPE_CLIENT_PRIVATE_KEY: 0x0d,
    SUBTYPE_SERVER_PRIVATE_KEY: 0x0e,
    SUBTYPE_WIFI_CONNECTION_STATE: 0x0f,
    SUBTYPE_VERSION: 0x10,
    SUBTYPE_WIFI_LIST: 0x11,
    SUBTYPE_ERROR: 0x12,
    SUBTYPE_CUSTOM_DATA: 0x13
},
controllEnum: {
    PACKAGE_VALUE: 0x00,
    SUBTYPE_ACK: 0x00,
    SUBTYPE_SET_SEC_MODE: 0x01,
    SUBTYPE_SET_OP_MODE: 0x02,
    SUBTYPE_CONNECT_WIFI: 0x03,
    SUBTYPE_DISCONNECT_WIFI: 0x04,
    SUBTYPE_GET_WIFI_STATUS: 0x05,
    SUBTYPE_DEAUTHENTICATE: 0x06,
    SUBTYPE_GET_VERSION: 0x07,
    SUBTYPE_CLOSE_CONNECTION: 0x08,
    SUBTYPE_GET_WIFI_LIST: 0x09
}

 

你可能感兴趣的:(ESP32)