当前位置:网站首页>Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)

Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)

2022-04-23 14:05:00 one billion twenty-nine million one hundred and seventy-nine th

Preface :
Wechat applet searches for nearby Bluetooth devices , Bluetooth initialization must be performed first , initialization ok Search for nearby Bluetooth devices . The specified Bluetooth connection requires deviceId( Hardware through at Commands can be viewed deviceId)
preparation :
Software : Wechat applet
Hardware :
Bluetooth devices :hc-09
Single chip microcomputer :stm32
One : hardware component
see hc-09deviceId

Hardware :
Two : Software part
Software
wxml

<button class="to-clock" hover-class="hover-to-clock" bindtap="toClock"> Bluetooth initialization </button>

js

 // Bluetooth initialization 
  wx.openBluetoothAdapter({
    success: function (res) {
      console.log(" Initialize Bluetooth adapter ");
      /*getBluetoothAdapterState()  Get the status of the native Bluetooth adapter , Determine if it is available ,available by false Because the user does not turn on the system Bluetooth */
      wx.getBluetoothAdapterState({
        success:function (res) {
          // Print related information 
          console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
          // res.available==true Adapter available  res.available==false Adapter not available 
          if(res.available){
            // Search for Bluetooth devices 
            wx.startBluetoothDevicesDiscovery({
              services: [],
              allowDuplicatesKey: false,
              success: function (res) {
                console.log(" Search for Bluetooth devices :"+res)
                // Get Bluetooth device output information list 
                wx.getBluetoothDevices({
                  success: function (res) {
                    console.log(' Number of search devices :' + res.devices.length)
                    console.log(' Equipment information :\n' + JSON.stringify(res.devices)+"\n")
                    // Wechat applet connects to Bluetooth  hc-09
                    wx.createBLEConnection({
                      deviceId:"2C:AB:33:33:94:08",
                      success: function (res) {
                        console.log(' Connection success output information :' + res)
                        wx.hideLoading()
                        wx.showModal({
                          title: ' reminder ',
                          content: ' Bluetooth connection successful '
                        })

                      },
                      fail: function () {
                        wx.hideLoading()
                        wx.showModal({
                          title: ' reminder ',
                          content: ' Bluetooth connection failed '
                        })
                      },
                 
                    })
                  }
                })
              },
              fail: function (err) {
                wx.hideLoading()
                console.log(err);
                wx.showModal({
                  title: ' reminder ',
                  content: ' Bluetooth search failed '
                })
              }
            });
          }else{
            wx.hideLoading()
            wx.showModal({
              title: ' reminder ',
              content: ' Bluetooth device not available '
            })
          }
         
        },
        fail: function (res) {
          // Print related information 
          console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
          wx.hideLoading()
          wx.showModal({
            title: ' reminder ',
            content: ' Bluetooth device not available '
          })
        }
      })
    },
    fail: function (err) {
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: ' Bluetooth initialization failed ',
        icon: 'success',
        duration: 2000
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    }
  });

More knowledge of WeChat applet and Bluetooth communication knowledge, and the following official account.
 Insert picture description here

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401297802.html