1. 在 manifest.json 文件中添加蓝牙权限:
{
  "mp-weixin": {
    "permission": {
      "scope.userLocation": {
        "desc": "获取用户地理位置信息"
      },
      "scope.bluetooth": {
        "desc": "蓝牙"
      },
      "scope.record": {
        "desc": "录音功能"
      }
    }
  }
}
  1. 在页面中引入 uni-ble 组件:
<uni-ble id="ble"></uni-ble>
  1. 在页面的 js 文件中初始化 uni-ble 组件:
import uniBle from '@/components/uni-ble/uni-ble.vue'

export default {
  components: {
    uniBle
  },
  data() {
    return {
      ble: null,
      deviceId: '', // 设备 ID
      serviceId: '', // 服务 ID
      characteristicId: '', // 特征值 ID
      writeValue: '', // 待写入的数据
      notifyValue: '', // 待订阅的数据
      notifyState: false, // 订阅状态
      receiveData: '', // 接收到的数据
      receiveDataArray: [] // 接收到的数据数组
    }
  },
  mounted() {
    this.ble = this.$refs.ble
    this.ble.init()
  },
  methods: {
    // 扫描设备
    scan() {
      this.ble.startScan({
        success: (res) => {
          console.log('扫描设备成功:', res)
        },
        fail: (err) => {
          console.log('扫描设备失败:', err)
        }
      })
    },
    // 停止扫描
    stopScan() {
      this.ble.stopScan({
        success: (res) => {
          console.log('停止扫描成功:', res)
        },
        fail: (err) => {
          console.log('停止扫描失败:', err)
        }
      })
    },
    // 连接设备
    connect() {
      this.ble.connect({
        deviceId: this.deviceId,
        success: (res) => {
          console.log('连接设备成功:', res)
        },
        fail: (err) => {
          console.log('连接设备失败:', err)
        }
      })
    },
    // 断开设备连接
    disconnect() {
      this.ble.disconnect({
        deviceId: this.deviceId,
        success: (res) => {
          console.log('断开设备连接成功:', res)
        },
        fail: (err) => {
          console.log('断开设备连接失败:', err)
        }
      })
    },
    // 获取服务列表
    getServices() {
      this.ble.getServices({
        deviceId: this.deviceId,
        success: (res) => {
          console.log('获取服务列表成功:', res)
        },
        fail: (err) => {
          console.log('获取服务列表失败:', err)
        }
      })
    },
    // 获取特征值列表
    getCharacteristics() {
      this.ble.getCharacteristics({
        deviceId: this.deviceId,
        serviceId: this.serviceId,
        success: (res) => {
          console.log('获取特征值列表成功:', res)
        },
        fail: (err) => {
          console.log('获取特征值列表失败:', err)
        }
      })
    },
    // 写入数据
    write() {
      this.ble.write({
        deviceId: this.deviceId,
        serviceId: this.serviceId,
        characteristicId: this.characteristicId,
        value: this.writeValue,
        success: (res) => {
          console.log('写入数据成功:', res)
        },
        fail: (err) => {
          console.log('写入数据失败:', err)
        }
      })
    },
    // 订阅数据
    subscribe() {
      this.ble.subscribe({
        deviceId: this.deviceId,
        serviceId: this.serviceId,
        characteristicId: this.characteristicId,
        success: (res) => {
          console.log('订阅数据成功:', res)
          this.notifyState = true
        },
        fail: (err) => {
          console.log('订阅数据失败:', err)
          this.notifyState = false
        }
      })
    },
    // 取消订阅数据
    unsubscribe() {
      this.ble.unsubscribe({
        deviceId: this.deviceId,
        serviceId: this.serviceId,
        characteristicId: this.characteristicId,
        success: (res) => {
          console.log('取消订阅数据成功:', res)
          this.notifyState = false
        },
        fail: (err) => {
          console.log('取消订阅数据失败:', err)
        }
      })
    },
    // 监听接收到的数据
    onReceive() {
      this.ble.onReceive({
        deviceId: this.deviceId,
        serviceId: this.serviceId,
        characteristicId: this.characteristicId,
        success: (res) => {
          console.log('监听接收到的数据成功:', res)
        },
        fail: (err) => {
          console.log('监听接收到的数据失败:', err)
        }
      })
      this.ble.on('receive', (res) => {
        console.log('接收到的数据:', res)
        this.receiveData = res.value
        this.receiveDataArray.push(res.value)
      })
    }
  }
}
  1. 可以根据需要调用对应的方法完成蓝牙连接和数据交互。例如:
  • 扫描设备:调用 scan 方法;
  • 连接设备:调用 connect 方法;
  • 获取服务列表:调用 getServices 方法;
  • 获取特征值列表:调用 getCharacteristics 方法;
  • 写入数据:调用 write 方法;
  • 订阅数据:调用 subscribe 方法;
  • 取消订阅数据:调用 unsubscribe 方法;
  • 监听接收到的数据:调用 onReceive 方法。

注意:在进行蓝牙连接和数据交互时,需要传入设备 ID、服务 ID、特征值 ID 等相关信息。可以在获取到服务列表和特征值列表后,将对应的 ID 保存起来,便于后续调用。

UNIAPP 蓝牙连接配对

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

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