以下是使用微信开发者工具编写蓝牙与 MCU 通讯程序的示例,实现发送和接收 8 字节数组数据:

// 初始化蓝牙适配器
wx.openBluetoothAdapter({
  success: function(res) {
    console.log('蓝牙适配器初始化成功');
    // 开始搜索附近的蓝牙设备
    wx.startBluetoothDevicesDiscovery({
      success: function(res) {
        console.log('开始搜索附近的蓝牙设备');
        // 监听蓝牙设备列表变化事件
        wx.onBluetoothDeviceFound(function(res) {
          var devices = res.devices;
          for (var i = 0; i < devices.length; i++) {
            var device = devices[i];
            console.log('发现蓝牙设备:' + device.name);
            // 判断是否是目标设备
            if (device.name === 'MCU设备') {
              // 停止搜索蓝牙设备
              wx.stopBluetoothDevicesDiscovery();
              // 连接目标设备
              wx.createBLEConnection({
                deviceId: device.deviceId,
                success: function(res) {
                  console.log('连接成功');
                  // 监听蓝牙连接状态变化事件
                  wx.onBLEConnectionStateChanged(function(res) {
                    if (!res.connected) {
                      console.log('连接已断开');
                    }
                  });
                  // 发送数据
                  var data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
                  wx.writeBLECharacteristicValue({
                    deviceId: device.deviceId,
                    serviceId: serviceId,
                    characteristicId: characteristicId,
                    value: data.buffer,
                    success: function(res) {
                      console.log('发送成功');
                    },
                    fail: function(res) {
                      console.log('发送失败');
                    }
                  });
                  // 监听蓝牙接收数据事件
                  wx.onBLECharacteristicValueChange(function(res) {
                    var data = new Uint8Array(res.value);
                    console.log('接收到数据:' + data);
                  });
                  // 开启接收通知
                  wx.notifyBLECharacteristicValueChange({
                    deviceId: device.deviceId,
                    serviceId: serviceId,
                    characteristicId: characteristicId,
                    state: true,
                    success: function(res) {
                      console.log('开启接收通知成功');
                    },
                    fail: function(res) {
                      console.log('开启接收通知失败');
                    }
                  });
                },
                fail: function(res) {
                  console.log('连接失败');
                }
              });
            }
          }
        });
      },
      fail: function(res) {
        console.log('搜索附近的蓝牙设备失败');
      }
    });
  },
  fail: function(res) {
    console.log('蓝牙适配器初始化失败');
  }
});

代码中使用 wx.openBluetoothAdapter 初始化蓝牙适配器,wx.startBluetoothDevicesDiscovery 搜索设备,wx.createBLEConnection 连接目标设备。

通过 wx.writeBLECharacteristicValue 发送 8 字节数组数据,并使用 wx.onBLECharacteristicValueChange 监听接收数据。

最后,使用 wx.notifyBLECharacteristicValueChange 开启接收通知,实现实时接收数据。

需要注意的是,serviceIdcharacteristicId 需要根据实际情况进行替换,可通过 wx.getBLEDeviceServiceswx.getBLEDeviceCharacteristics 获取。

微信开发者工具蓝牙通讯:8 字节数组与 MCU 交互

原文地址: https://www.cveoy.top/t/topic/qvjV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录