test.vue 648 B

12345678910111213141516171819202122232425262728
  1. <template>
  2. <div>
  3. <button @click="scanBluetooth">扫描蓝牙设备</button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. async scanBluetooth() {
  10. if (!navigator.bluetooth) {
  11. console.log('此浏览器不支持 Web Bluetooth API。:');
  12. // alert("此浏览器不支持 Web Bluetooth API。");
  13. return;
  14. }
  15. try {
  16. const device = await navigator.bluetooth.requestDevice({
  17. filters: [{services: ['battery_service']}]
  18. });
  19. console.log('选择的设备:', device);
  20. } catch (error) {
  21. console.error('错误:', error);
  22. }
  23. }
  24. }
  25. }
  26. </script>