vue3 vueUse 连接蓝牙

本文介绍了如何在Vue3项目中利用vueUse库简化蓝牙设备的连接过程,包括调用蓝牙API、扫描设备、选择设备配对以及接收蓝牙数据(待测试)。作者还展示了如何使用webBluetoothAPI进行数据交互的示例代码。
摘要由CSDN通过智能技术生成

目录

vueuse安装:

useBluetooth: 调用蓝牙API

扫描周期设备

选择设备配对 

连接成功

接收蓝牙数据(待测试)

通过web bluttooth读写数据参考


vue3的网页项目连接电脑或者手机上的蓝牙设备,使用vueUse库,可以快速检查连接蓝牙设备。

vueUse库使用参考:

VueUse工具库 常用api-CSDN博客

vueuse安装:

npm install -D @vueuse/core

或者
pnpm add @vueuse/core

使用关于window相关的api 

useBluetooth: 调用蓝牙API

在需要用vue文件导入:
import {useBluetooth, useDevicesList} from "@vueuse/core";

检查当前设备是否支持蓝牙 xx.vue

<template>
  <div class="about flex flex-col">
    <h1> 蓝牙连接功能测试</h1>
    <div>
      <el-button size="default" class="mr-3">打开蓝牙</el-button>
    </div>
    <div>
      <el-text type="primary" size="large" >蓝牙可用状态:{{ isSupported ? '当前设备支持蓝牙' : '当前设备不支持蓝牙' }}</el-text>
    </div>
  </div>
</template>

<script setup lang="ts">

import {useBluetooth, useDevicesList} from "@vueuse/core";

const {
  isSupported,// check if bluetooth is supported
  isConnected, // check if connected, reactive
  device, // device object, reactive
  requestDevice, // function to request device, returns a promise
  server, // handle services, reactive
  error // error helper, reactive
} = useBluetooth({
  acceptAllDevices: true,
});
console.log(device)
</script>

<style>
/*@media (max-width: 1024px) {
  .about{
    margin-top:300px;
  }
}*/

@media (min-width: 1024px) {
  .about {
    min-height: 100vh;
    display: flex;
    align-items: center;
  }
}
</style>

我的电脑确实是有蓝牙模块

扫描周期设备

还是上面useBluetooth对象,调用requestDevice()自动弹窗一个扫描窗。

<script setup lang="ts">

import {useBluetooth} from "@vueuse/core";

const {
  isConnected,
  isSupported,
  device,
  requestDevice,
  error,
} = useBluetooth({
  acceptAllDevices: true,
})
</script>

<template>
  <div class="grid grid-cols-1 gap-x-4 gap-y-4">
    <div>{{ isSupported ? 'Bluetooth Web API Supported' : 'Your browser does not support the Bluetooth Web API' }}</div>

    <div v-if="isSupported">
      <button @click="requestDevice()">
       扫描周边蓝牙设备
      </button>
    </div>

    <div v-if="device">
      <p>已连接蓝牙设备名: {{ device.name }}</p>
    </div>

    <div v-if="isConnected" class="bg-green-500 text-white p-3 rounded-md">
      <p>已连接</p>
    </div>

    <div v-if="!isConnected" class="bg-orange-800 text-white p-3 rounded-md">
      <p>未连接</p>
    </div>

    <div v-if="error">
      <div>出错:</div>
      <pre>
      <code class="block p-5 whitespace-pre">{{ error }}</code>
    </pre>
    </div>
  </div>
</template>

选择设备配对 

点击需要连接设备,点击:配对

连接成功

接收蓝牙数据(待测试)

以下是官方的接收电池服务消息测试案例

<template>
  <button @click="requestDevice()">
    Request Bluetooth Device
  </button>
</template>



import { pausableWatch, useBluetooth } from '@vueuse/core'

const {
  isSupported,
  isConnected,
  device,
  requestDevice,
  server,
} = useBluetooth({
  acceptAllDevices: true,
  optionalServices: [
    'battery_service',
  ],
})

const batteryPercent = ref<undefined | number>()

const isGettingBatteryLevels = ref(false)

const getBatteryLevels = async () => {
  isGettingBatteryLevels.value = true

  // Get the battery service:
  const batteryService = await server.getPrimaryService('battery_service')

  // Get the current battery level
  const batteryLevelCharacteristic = await batteryService.getCharacteristic(
    'battery_level',
  )

  // Listen to when characteristic value changes on `characteristicvaluechanged` event:
  batteryLevelCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
    batteryPercent.value = event.target.value.getUint8(0)
  })

  // Convert received buffer to number:
  const batteryLevel = await batteryLevelCharacteristic.readValue()

  batteryPercent.value = await batteryLevel.getUint8(0)
}

const { stop } = pausableWatch(isConnected, (newIsConnected) => {
  if (!newIsConnected || !server.value || isGettingBatteryLevels.value)
    return
  // Attempt to get the battery levels of the device:
  getBatteryLevels()
  // We only want to run this on the initial connection, as we will use a event listener to handle updates:
  stop()
})

原理:vueUse其实是调用了谷歌的web Bluetooth

蓝牙通信功能实现参考:

useBluetooth | VueUse

Web Bluetooth Samples

https://github.com/WebBluetoothCG/demos

通过web bluttooth读写数据参考

通过 Web 控制蓝牙设备:WebBluetooth入门_navigator.bluetooth.requestdevice-CSDN博客

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3是一种用于构建用户界面的现代化JavaScript框架,它具有响应式数据绑定和组件化的特性。Element UI是Vue.js的一套基于Vue组件的UI库,它提供了丰富的UI组件和交互效果。VueUse是一组适用于Vue框架的常用功能的集合,它包含了许多有用的自定义hook和工具函数。 在使用Vue3和Element UI时,我们可以结合使用VueUse的拖拽功能来实现拖拽效果。VueUse中有一个拖拽功能的自定义hook,它可以帮助我们实现元素的拖拽功能。我们需要在Vue组件中引入这个拖拽hook,并使用它提供的函数来控制元素的拖拽行为。 首先,我们需要安装VueUse库,并在Vue组件中导入拖拽hook。然后在组件的模板中,我们可以使用Element UI提供的组件来创建需要拖拽的元素。 接着,在Vue组件的逻辑部分,我们可以使用拖拽hook的函数来设置元素的拖拽行为。其中,我们可以使用v-draggable指令来绑定元素的拖拽事件和状态。通过这个指令,我们可以控制元素的拖拽范围、拖拽过程中的样式变化以及最终的拖拽结果。 最后,我们可以根据需要在拖拽的过程中执行特定的操作,比如更新元素的位置、改变元素的样式等。通过Vue3、Element UI和VueUse的组合,我们可以实现灵活且高效的拖拽功能,提升用户体验和界面交互效果。 综上所述,Vue3、Element UI和VueUse的结合可以实现拖拽功能,让我们的应用更加动态和丰富。通过利用它们提供的特性和功能,我们可以轻松地实现拖拽效果,并提供良好的用户交互体验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值