forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
133 lines (112 loc) · 3.14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { computed, ref, watch } from 'vue-demi'
import { tryOnMounted, tryOnScopeDispose } from '@vueuse/shared'
import type { ConfigurableNavigator } from '../_configurable'
import { defaultNavigator } from '../_configurable'
export interface UseBluetoothRequestDeviceOptions {
/**
*
* An array of BluetoothScanFilters. This filter consists of an array
* of BluetoothServiceUUIDs, a name parameter, and a namePrefix parameter.
*
*/
filters?: BluetoothLEScanFilter[] | undefined
/**
*
* An array of BluetoothServiceUUIDs.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService/uuid
*
*/
optionalServices?: BluetoothServiceUUID[] | undefined
}
export interface UseBluetoothOptions extends UseBluetoothRequestDeviceOptions, ConfigurableNavigator {
/**
*
* A boolean value indicating that the requesting script can accept all Bluetooth
* devices. The default is false.
*
* !! This may result in a bunch of unrelated devices being shown
* in the chooser and energy being wasted as there are no filters.
*
*
* Use it with caution.
*
* @default false
*
*/
acceptAllDevices?: boolean
}
export function useBluetooth(options?: UseBluetoothOptions) {
let {
acceptAllDevices = false,
} = options || {}
const {
filters = undefined,
optionalServices = undefined,
navigator = defaultNavigator,
} = options || {}
const isSupported = navigator && 'bluetooth' in navigator
const device = ref<undefined | BluetoothDevice>(undefined)
const error = ref<unknown | null>(null)
watch(device, () => {
connectToBluetoothGATTServer()
})
async function requestDevice(): Promise<void> {
// This is the function can only be called if Bluetooth API is supported:
if (!isSupported)
return
// Reset any errors we currently have:
error.value = null
// If filters specified, we need to ensure we don't accept all devices:
if (filters && filters.length > 0)
acceptAllDevices = false
try {
device.value = await navigator?.bluetooth.requestDevice({
acceptAllDevices,
filters,
optionalServices,
})
}
catch (err) {
error.value = err
}
}
const server = ref<undefined | BluetoothRemoteGATTServer>()
const isConnected = computed((): boolean => {
return server.value?.connected || false
})
async function connectToBluetoothGATTServer() {
// Reset any errors we currently have:
error.value = null
if (device.value && device.value.gatt) {
// Add callback to gattserverdisconnected event:
device.value.addEventListener('gattserverdisconnected', () => {})
try {
// Connect to the device:
server.value = await device.value.gatt.connect()
}
catch (err) {
error.value = err
}
}
}
tryOnMounted(() => {
if (device.value)
device.value.gatt?.connect()
})
tryOnScopeDispose(() => {
if (device.value)
device.value.gatt?.disconnect()
})
return {
isSupported,
isConnected,
// Device:
device,
requestDevice,
// Server:
server,
// Errors:
error,
}
}