国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序?qū)崿F(xiàn)藍(lán)牙開鎖、開門、開關(guān)、指令發(fā)送成功,但藍(lán)牙設(shè)備毫無反應(yīng)、坑

這篇具有很好參考價值的文章主要介紹了微信小程序?qū)崿F(xiàn)藍(lán)牙開鎖、開門、開關(guān)、指令發(fā)送成功,但藍(lán)牙設(shè)備毫無反應(yīng)、坑。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。


開源

wx聯(lián)系本人獲取源碼(開源): MJ682517


html

<view>
	<view class="p_l_36 p_r_36">
		<input class="w_100_ h_80 lh_80 ta_c b_2s_eee radius_20" value="{{instructVal}}" type="text" placeholder="請輸入開門指令" bindinput="bindinput" />
	</view>
	
	<view class="m_t_36 w_50_ h_90 lh_90 m_l_a m_r_a bc_409eff radius_10 color_fff ta_c" catchtap="openBluetoothAdapter">藍(lán)牙開鎖</view>
</view>

JavaScript

需要從下往上閱讀,使用函數(shù)自調(diào)的方式解決API不能及時獲取數(shù)據(jù)的問題,替換方案是使用定時器,但是個人覺得定時器不好,所以使用了函數(shù)自調(diào)的方式實(shí)現(xiàn)獲取不到數(shù)據(jù)的問題。

getBluetoothDevices方法中獲取deviceId;getBLEDeviceServices方法中獲取到serviceId,獲取到的是數(shù)組,取數(shù)組中的uuid作為serviceId值,具體用哪個值,需要與藍(lán)牙設(shè)備文檔比對;getBLEDeviceCharacteristics方法中獲取characteristicId,獲取到的是數(shù)組,取數(shù)組中的uuid作為characteristicId值。

一般情況的對應(yīng)的值如下
serviceId: 0000FFB0-0000-1000-8000-00805F9B34FB
notifyId: 0000FFB2-0000-1000-8000-00805F9B34FB
writeId: 0000FFB1-0000-1000-8000-00805F9B34FB
注意觀察第一個橫杠前面最后兩位的值,不難發(fā)現(xiàn)規(guī)律。
寫入的指令一般是以十六進(jìn)制(EE03E30100)字符串的方式,最后轉(zhuǎn)為二進(jìn)制發(fā)送給藍(lán)牙。

坑: 在不確定藍(lán)牙文檔是否正確的情況下,寫入成功,但是藍(lán)牙設(shè)備毫無反應(yīng),那么大概率就是寫入的指令有問題,所以需要校對一下指令是否正確。

藍(lán)牙文檔需要與廠商獲取。

獲取藍(lán)牙服務(wù)值方法getBLEDeviceServices。
獲取藍(lán)牙特征值方法getBLEDeviceCharacteristics。特征值一般包括兩個,一個代表寫入值,一個代表讀取值,意思是說寫入的是需要把寫入的特征值帶上,讀取的時候把讀取的特征值帶上。

notifyBLECharacteristicValueChange方法啟用藍(lán)牙低功耗設(shè)備特征值變化時的notify功能,訂閱特征。此時傳入的是讀取的特征值。文章來源地址http://www.zghlxwxcb.cn/news/detail-611592.html

// index.js
let timeout = undefined;

Page({
  data: {
    devices: [],
    deviceId: '',
    services: [],
    serviceId: '',
    characteristics: [],
    characteristicId: '',
    instructVal: 'EE03E30100'
  },
  // 指令輸入
  bindinput({
    detail: {
      value
    }
  }) {
    this.setData({
      instructVal: value
    });
  },

  // 失敗時的統(tǒng)一提示
  failShowToast(title = '開鎖失敗') {
    clearTimeout(timeout);
    timeout = undefined;
    wx.hideLoading();
    wx.showToast({
      icon: 'none',
      title
    });
  },

  /**
   * 根據(jù)不同方法名調(diào)用方法,統(tǒng)一定時器的觸發(fā)判斷,
   * 當(dāng)定時器觸發(fā)時,無法確定當(dāng)前調(diào)用的方法是哪個
   * @param {String} fnName 
   */
  methodExecution(fnName = '') {
    if (timeout) this[fnName]();
  },

  // 關(guān)閉藍(lán)牙模塊
  closeBluetoothAdapter() {
    let that = this;

    // 關(guān)閉藍(lán)牙模塊
    wx.closeBluetoothAdapter({
      success() {},
      fail() {
        that.methodExecution('closeBluetoothAdapter');
      }
    });
  },

  // 斷開與藍(lán)牙低功耗設(shè)備的連接
  closeBLEConnection() {
    let that = this;

    // 斷開與藍(lán)牙低功耗設(shè)備的連接
    wx.closeBLEConnection({
      deviceId: that.data.deviceId,
      success() {
        that.closeBluetoothAdapter();
      },
      fail() {
        that.methodExecution('closeBLEConnection');
      }
    });
  },

  // 向藍(lán)牙低功耗設(shè)備特征值中寫入二進(jìn)制數(shù)據(jù)
  writeBLECharacteristicValue() {
    let that = this;

    let str = that.data.instructVal;

    if (!str) return wx.showToast({
      title: '請輸入指令',
      icon: 'none'
    });

    /* 將數(shù)值轉(zhuǎn)為ArrayBuffer類型數(shù)據(jù) */
    let typedArray = new Uint8Array(str.match(/[\da-f]{2}/gi).map((h) => parseInt(h, 16))),
      buffer = typedArray.buffer;

    // 向藍(lán)牙低功耗設(shè)備特征值中寫入二進(jìn)制數(shù)據(jù)
    wx.writeBLECharacteristicValue({
      deviceId: that.data.deviceId,
      serviceId: that.data.serviceId,
      characteristicId: '0000FFB1-0000-1000-8000-00805F9B34FB',
      value: buffer,
      success() {
        clearTimeout(timeout);
        timeout = undefined;
        wx.hideLoading();
        wx.showToast({
          icon: 'none',
          title: '開鎖成功'
        });
        // that.closeBLEConnection();
      },
      fail() {
        that.methodExecution('writeBLECharacteristicValue');
      }
    });
  },

  // 監(jiān)聽藍(lán)牙低功耗設(shè)備的特征值變化事件
  onBLECharacteristicValueChange() {
    let that = this;

    // 監(jiān)聽藍(lán)牙低功耗設(shè)備的特征值變化事件
    wx.onBLECharacteristicValueChange((res) => {
      if (res.value) {
        that.writeBLECharacteristicValue();
      } else {
        that.methodExecution('onBLECharacteristicValueChange');
      }
    });
  },

  // 啟用藍(lán)牙低功耗設(shè)備特征值變化時的 notify 功能,訂閱特征
  notifyBLECharacteristicValueChange() {
    let that = this;

    // 啟用藍(lán)牙低功耗設(shè)備特征值變化時的 notify 功能,訂閱特征
    wx.notifyBLECharacteristicValueChange({
      state: true,
      deviceId: that.data.deviceId,
      serviceId: that.data.serviceId,
      characteristicId: that.data.characteristicId,
      success() {
        that.onBLECharacteristicValueChange();
      },
      fail() {
        that.methodExecution('notifyBLECharacteristicValueChange');
      }
    });
  },

  // 獲取藍(lán)牙低功耗設(shè)備某個服務(wù)中所有特征 (characteristic)
  getBLEDeviceCharacteristics() {
    let that = this;

    // 獲取藍(lán)牙低功耗設(shè)備某個服務(wù)中所有特征 (characteristic)
    wx.getBLEDeviceCharacteristics({
      deviceId: that.data.deviceId,
      serviceId: that.data.serviceId,
      success({
        characteristics
      }) {
        that.setData({
          characteristics,
          characteristicId: '0000FFB2-0000-1000-8000-00805F9B34FB'
        }, () => that.notifyBLECharacteristicValueChange());
      },
      fail() {
        that.methodExecution('getBLEDeviceCharacteristics');
      }
    });
  },

  // 獲取藍(lán)牙低功耗設(shè)備所有服務(wù) (service)
  getBLEDeviceServices() {
    let that = this;

    // 獲取藍(lán)牙低功耗設(shè)備所有服務(wù) (service)
    wx.getBLEDeviceServices({
      deviceId: that.data.deviceId,
      success({
        services
      }) {
        that.setData({
          services,
          serviceId: '0000FFB0-0000-1000-8000-00805F9B34FB'
        }, () => that.getBLEDeviceCharacteristics());
      },
      fail() {
        that.methodExecution('getBLEDeviceServices');
      }
    });
  },

  // 停止搜尋附近的藍(lán)牙外圍設(shè)備
  stopBluetoothDevicesDiscovery() {
    let that = this;

    // 停止搜尋附近的藍(lán)牙外圍設(shè)備
    wx.stopBluetoothDevicesDiscovery({
      success() {
        that.getBLEDeviceServices();
      },
      fail() {
        that.methodExecution('stopBluetoothDevicesDiscovery');
      }
    });
  },

  // 連接藍(lán)牙低功耗設(shè)備
  createBLEConnection() {
    let that = this;

    // 連接藍(lán)牙低功耗設(shè)備
    wx.createBLEConnection({
      deviceId: that.data.deviceId,
      success() {
        that.stopBluetoothDevicesDiscovery();
      },
      fail() {
        that.methodExecution('createBLEConnection');
      }
    });
  },

  // 獲取在藍(lán)牙模塊生效期間所有搜索到的藍(lán)牙設(shè)備
  getBluetoothDevices() {
    let that = this;

    // 獲取在藍(lán)牙模塊生效期間所有搜索到的藍(lán)牙設(shè)備
    wx.getBluetoothDevices({
      success({
        devices
      }) {
        for (let i = 0; i < devices.length; i++) {
          const item = devices[i];
          if (item.name === 'YX_0A45320C78C6') {
            item.ASUUID = item.advertisServiceUUIDs[0];

            that.setData({
              devices: item,
              deviceId: item.deviceId
            }, () => that.createBLEConnection());

            break;
          }
        }

        that.methodExecution('getBluetoothDevices');
      },
      fail() {
        that.methodExecution('getBluetoothDevices');
      }
    });
  },

  // 開始搜尋附近的藍(lán)牙外圍設(shè)備
  startBluetoothDevicesDiscovery() {
    let that = this;

    // 開始搜尋附近的藍(lán)牙外圍設(shè)備
    wx.startBluetoothDevicesDiscovery({
      // 此字段會導(dǎo)致startBluetoothDevicesDiscovery不執(zhí)行
      // services: ['YX'],
      success() {
        that.getBluetoothDevices();
      },
      fail() {
        that.methodExecution('startBluetoothDevicesDiscovery');
      }
    });
  },

  // 藍(lán)牙開鎖
  openBluetoothAdapter() {
    let that = this,
      thatData = that.data;

    if (!that.data.instructVal) return wx.showToast({
      title: '請輸入指令',
      icon: 'none'
    });

    if (timeout) return wx.showToast({
      title: '加載中',
      icon: 'none'
    });

    wx.showLoading({
      title: '加載中',
      mask: true
    });

    timeout = setTimeout(() => {
      that.failShowToast('開鎖失敗');
    }, 1000 * 26);

    if (thatData.deviceId && thatData.serviceId && thatData.characteristicId) return that.writeBLECharacteristicValue();

    // 初始化藍(lán)牙模塊
    wx.openBluetoothAdapter({
      success() {
        that.setData({
          devices: [],
          deviceId: '',
          services: [],
          serviceId: '',
          characteristics: [],
          characteristicId: ''
        }, () => that.startBluetoothDevicesDiscovery());
      },
      fail() {
        that.failShowToast('查看手機(jī)藍(lán)牙是否打開');
      }
    });
  },
  
  onLoad() {
  
  }
})

到了這里,關(guān)于微信小程序?qū)崿F(xiàn)藍(lán)牙開鎖、開門、開關(guān)、指令發(fā)送成功,但藍(lán)牙設(shè)備毫無反應(yīng)、坑的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序——實(shí)現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    微信小程序——實(shí)現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    ?作者簡介:2022年 博客新星 第八 。熱愛國學(xué)的Java后端開發(fā)者,修心和技術(shù)同步精進(jìn)。 ??個人主頁:Java Fans的博客 ??個人信條:不遷怒,不貳過。小知識,大智慧。 ??當(dāng)前專欄:微信小程序?qū)W習(xí)分享 ?特色專欄:國學(xué)周更-心性養(yǎng)成之路 ??本文內(nèi)容:微信小程序——實(shí)

    2024年02月08日
    瀏覽(26)
  • 微信小程序switch開關(guān)組件修改樣式(大小,顏色)

    微信小程序switch開關(guān)組件修改樣式(大小,顏色)

    以上尺寸根據(jù)你的具體情況來調(diào)整

    2024年02月11日
    瀏覽(95)
  • 微信小程序 - 藍(lán)牙連接

    微信小程序 - 藍(lán)牙連接

    官網(wǎng)?藍(lán)牙 (Bluetooth) | 微信開放文檔 ???????藍(lán)牙低功耗是從藍(lán)牙?4.0?起支持的協(xié)議,與經(jīng)典藍(lán)牙相比,功耗極低、傳輸速度更快,但傳輸數(shù)據(jù)量較小。常用在對續(xù)航要求較高且只需小數(shù)據(jù)量傳輸?shù)母鞣N智能電子產(chǎn)品中,比如智能穿戴設(shè)備、智能家電、傳感器等,應(yīng)用場景

    2024年02月05日
    瀏覽(39)
  • 微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備

    微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備

    ?藍(lán)牙列表期待效果 ?代碼 ?js里面注意getBLEDeviceCharacteristics獲取特征值的時候,極個別設(shè)備參數(shù)write,read,notify是亂來的,需要自己打單獨(dú)處理,通過對應(yīng)write,read,notify 為true的時候拿到對應(yīng)的uuid,

    2024年02月04日
    瀏覽(40)
  • 微信小程序獲取藍(lán)牙權(quán)限

    要獲取微信小程序中的藍(lán)牙權(quán)限,您可以按照以下步驟進(jìn)行操作: 1. 在 app.json 文件中添加以下代碼: ? ?``` ? ?\\\"permissions\\\": { ? ? ?\\\"scope.userLocation\\\": { ? ? ? ?\\\"desc\\\": \\\"需要獲取您的地理位置授權(quán)以搜索附近的藍(lán)牙設(shè)備。\\\" ? ? ?}, ? ? ?\\\"scope.bluetooth\\\": { ? ? ? ?\\\"desc\\\": \\\"需要獲取您

    2024年02月11日
    瀏覽(29)
  • 微信小程序藍(lán)牙流程及代碼
  • 微信小程序:BLE藍(lán)牙開發(fā)

    一、添加藍(lán)牙權(quán)限: 1.添加藍(lán)牙權(quán)限(工程/app.json): 二、實(shí)現(xiàn)掃描/連接/接收BLE設(shè)備數(shù)據(jù): 1.實(shí)現(xiàn)BLE藍(lán)牙設(shè)備掃描: 2.實(shí)現(xiàn)連接設(shè)備/接收數(shù)據(jù): 3.調(diào)用例子:

    2024年02月12日
    瀏覽(106)
  • 微信小程序藍(lán)牙授權(quán)完整流程

    微信小程序藍(lán)牙授權(quán)完整流程

    ? ? ? ? 1.1 authorize: ????????????????提前向用戶發(fā)起授權(quán)請求。調(diào)用后會立刻彈窗詢問用戶是否同意授權(quán)小程序使用某項(xiàng)功能或獲取用戶的某些數(shù)據(jù),但不會實(shí)際調(diào)用對應(yīng)接口。如果用戶之前已經(jīng)同意授權(quán),則不會出現(xiàn)彈窗,直接返回成功。更多用法詳見 用戶授權(quán)。

    2024年04月27日
    瀏覽(103)
  • 微信小程序掃描藍(lán)牙及操作(低功耗)藍(lán)牙數(shù)據(jù)(全套流程)

    微信小程序掃描藍(lán)牙及操作(低功耗)藍(lán)牙數(shù)據(jù)(全套流程)

    掃描藍(lán)牙首先需要打開手機(jī)上的藍(lán)牙。iOS只需打開藍(lán)牙即可開始掃描藍(lán)牙;Android需要打開藍(lán)牙及位置信息才可以開始掃描藍(lán)牙。下面的代碼可以在掃描之前使用,用來檢查是否已經(jīng)很打開藍(lán)牙及位置信息 官方地址https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth/wx.stopBl

    2024年02月09日
    瀏覽(92)
  • 微信小程序-MQTT-ESP8266操作SG90開關(guān)燈

    微信小程序-MQTT-ESP8266操作SG90開關(guān)燈

    本例僅供參考,不進(jìn)行更新完善。 困難:微信小程序域名限制; ESP8266連接MQTT可參考:HTML Echarts圖形統(tǒng)計實(shí)時顯示DHT11溫度(四)_我也不清楚的博客-CSDN博客_vue echarts溫度計動態(tài)顯示溫度 ESP8266控制SG90可參考:NodeMcu(ESP8266)控制SG90_我也不清楚的博客-CSDN博客 ESP8266 DNS WEB動態(tài)配

    2024年02月12日
    瀏覽(35)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包