📄 useBluetooth¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 4 |
| 📦 Imports | 11 |
| ⚡ Async/Await Patterns | 3 |
| 🟢 Vue Composition API | 1 |
| 📐 Interfaces | 3 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useBluetooth/index.ts
📦 Imports¶
| Name | Source |
|---|---|
ShallowRef |
vue |
ConfigurableNavigator |
../_configurable |
Supportable |
../types |
tryOnMounted |
@vueuse/shared |
tryOnScopeDispose |
@vueuse/shared |
shallowReadonly |
vue |
shallowRef |
vue |
watch |
vue |
defaultNavigator |
../_configurable |
useEventListener |
../useEventListener |
useSupported |
../useSupported |
Async/Await Patterns¶
| Type | Function | Await Expressions | Promise Chains |
|---|---|---|---|
| await-expression | useBluetooth |
navigator?.bluetooth.requestDevice({ acceptAllDevices, filters, optionalServi... | none |
| async-function | requestDevice |
navigator?.bluetooth.requestDevice({ acceptAllDevices, filters, optionalServi... | none |
| async-function | connectToBluetoothGATTServer |
device.value.gatt.connect() | none |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
watch |
watch | none | none |
Functions¶
useBluetooth(options: UseBluetoothOptions): UseBluetoothReturn¶
Parameters:
optionsUseBluetoothOptions
Returns: UseBluetoothReturn
Calls:
useSupported (from ../useSupported)shallowRef (from vue)watch (from vue)connectToBluetoothGATTServernavigator?.bluetooth.requestDevicedevice.value.gatt.connecttryOnMounted (from @vueuse/shared)useEventListener (from ../useEventListener)tryOnScopeDispose (from @vueuse/shared)device.value.gatt?.disconnectshallowReadonly (from vue)
Internal Comments:
// This is the function can only be called if Bluetooth API is supported:
// Reset any errors we currently have: (x8)
// If filters specified, we need to ensure we don't accept all devices:
// Connect to the device: (x4)
// Device: (x2)
// Server: (x2)
// Errors: (x2)
Code
export function useBluetooth(options?: UseBluetoothOptions): UseBluetoothReturn {
let {
acceptAllDevices = false,
} = options || {}
const {
filters = undefined,
optionalServices = undefined,
navigator = defaultNavigator,
} = options || {}
const isSupported = useSupported(() => navigator && 'bluetooth' in navigator)
const device = shallowRef<undefined | BluetoothDevice>()
const error = shallowRef<unknown | null>(null)
watch(device, () => {
void connectToBluetoothGATTServer()
})
async function requestDevice(): Promise<void> {
// This is the function can only be called if Bluetooth API is supported:
if (!isSupported.value)
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 = shallowRef<undefined | BluetoothRemoteGATTServer>()
const isConnected = shallowRef(false)
function reset() {
isConnected.value = false
device.value = undefined
server.value = undefined
}
async function connectToBluetoothGATTServer() {
// Reset any errors we currently have:
error.value = null
if (device.value && device.value.gatt) {
try {
// Connect to the device:
server.value = await device.value.gatt.connect()
isConnected.value = server.value.connected
}
catch (err) {
error.value = err
}
}
}
tryOnMounted(() => {
useEventListener(device, 'gattserverdisconnected', reset, { passive: true, once: true })
void connectToBluetoothGATTServer()
})
tryOnScopeDispose(() => {
if (device.value)
device.value.gatt?.disconnect()
})
return {
isSupported,
isConnected: shallowReadonly(isConnected),
// Device:
device,
requestDevice,
// Server:
server,
// Errors:
error,
}
}
Internal helpers¶
Declared inside another function in this file.
requestDevice(): Promise<void>¶
Returns: Promise<void>
Calls:
navigator?.bluetooth.requestDevice
Internal Comments:
// This is the function can only be called if Bluetooth API is supported:
// Reset any errors we currently have: (x4)
// If filters specified, we need to ensure we don't accept all devices:
Code
async function requestDevice(): Promise<void> {
// This is the function can only be called if Bluetooth API is supported:
if (!isSupported.value)
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
}
}
reset(): void¶
Returns: void
Code
connectToBluetoothGATTServer(): Promise<void>¶
Returns: Promise<void>
Calls:
device.value.gatt.connect
Internal Comments:
Code
async function connectToBluetoothGATTServer() {
// Reset any errors we currently have:
error.value = null
if (device.value && device.value.gatt) {
try {
// Connect to the device:
server.value = await device.value.gatt.connect()
isConnected.value = server.value.connected
}
catch (err) {
error.value = err
}
}
}
Interfaces¶
UseBluetoothRequestDeviceOptions¶
Interface Code
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
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
filters |
BluetoothLEScanFilter[] \| undefined |
✓ | not shown |
optionalServices |
BluetoothServiceUUID[] \| undefined |
✓ | not shown |
UseBluetoothOptions¶
Interface Code
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
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
acceptAllDevices |
boolean |
✓ | not shown |
UseBluetoothReturn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
isConnected |
Readonly<ShallowRef<boolean>> |
✗ | not shown |
device |
ShallowRef<BluetoothDevice \| undefined> |
✗ | not shown |
requestDevice |
() => Promise<void> |
✗ | not shown |
server |
ShallowRef<BluetoothRemoteGATTServer \| undefined> |
✗ | not shown |
error |
ShallowRef<unknown \| null> |
✗ | not shown |
Generated by Syntax Scribe