useBluetooth
蓝牙设备
何时使用
当需要使用蓝牙功能时
API
const [
{ devices, connectedDevices, adapter },
{
toggleAdapter,
getState,
getDevices,
getConnected,
makePair,
isPaired,
toggleDiscovery,
},
] = useBluetooth();
参数说明
无
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
devices | 蓝牙模块生效期间所有搜索到的蓝牙设备 | Taro.getBluetoothDevices.SuccessCallbackResultBlueToothDevice[] |
connectedDevices | 主服务UUID 获取已连接的蓝牙设备 | Taro.getConnectedBluetoothDevices.BluetoothDeviceInfo[] |
adapter | 蓝牙模块 | Taro.getBluetoothAdapterState.SuccessCallbackResult |
toggleAdapter | 初始化(关闭)蓝牙模块。iOS 上开启主机/从机(外围设备)模式时需分别调用一次,并指定对应的mode | PromiseOptionalAction<ExcludeOption<Taro.startBluetoothDevicesDiscovery.Option>, TaroGeneral.BluetoothError> |
getState | 获取本机蓝牙适配器状态 | PromiseWithoutOptionAction<AdapterState> |
getDevices | 获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备 | PromiseWithoutOptionAction<Taro.getBluetoothDevices.SuccessCallbackResult> |
getConnected | 根据主服务UUID 获取已连接的蓝牙设备 | PromiseWithoutOptionAction<Taro.getConnectedBluetoothDevices.SuccessCallbackResult> |
toggleDiscovery | 开始(停止)搜寻附近的蓝牙外围设备 | PromiseOptionalAction<ExcludeOption<Taro.startBluetoothDevicesDiscovery.Option>, TaroGeneral.BluetoothError> |
makePair | 通常情况下(需要指定 pin 码或者密码时)系统会接管配对流程,直接调用 wx.createBLEConnection 即可。该接口只应当在开发者不想让用户手动输入 pin 码且真机验证确认可以正常生效情况下用 | PromiseAction<ExcludeOption<Taro.makeBluetoothPair.Option>> |
isPaired | 查询蓝牙设备是否配对,仅安卓支持 | PromiseAction<string> |
代码演示
- React
- Vue
device/useBluetooth/index
import React from 'react';
import { useBluetooth } from 'taro-hooks';
import DemoContent from '@src/components/DemoContent';
import { Cell, Divider } from '@taroify/core';
export default () => {
const [{ devices, connectedDevices, adapter }] = useBluetooth();
return (
<DemoContent>
<Cell.Group title="适配器">
{Object.entries(adapter ?? {}).map(([key, value]) => (
<Cell key={key} title={key} brief={JSON.stringify(value)}></Cell>
))}
</Cell.Group>
<Divider>设备</Divider>
{devices.map((item, key) => (
<Cell.Group title={item.name} key={key}>
{Object.entries(item).map(([itemKey, value]) => (
<Cell
key={itemKey}
title={itemKey}
brief={JSON.stringify(value)}
></Cell>
))}
</Cell.Group>
))}
<Divider>连接设备</Divider>
{connectedDevices.map((item, key) => (
<Cell.Group title={item.name} key={key}>
{Object.entries(item).map(([itemKey, value]) => (
<Cell
key={itemKey}
title={itemKey}
brief={JSON.stringify(value)}
></Cell>
))}
</Cell.Group>
))}
</DemoContent>
);
};
device/useBluetooth/index
<template>
<demo-content>
<nut-cell-group title="适配器">
<nut-cell
v-for="(value, key) in adapter ?? {}"
:key="key"
:title="key"
:sub-title="$filters.stringify(value)"
></nut-cell>
</nut-cell-group>
<nut-divider>设备</nut-divider>
<nut-cell-group
v-for="(item, key) in devices"
:key="key"
:title="item.name"
>
<nut-cell
v-for="(value, itemKey) in item"
:key="itemKey"
:title="itemKey"
:sub-title="$filters.stringify(value)"
></nut-cell>
</nut-cell-group>
<nut-divider>连接设备</nut-divider>
<nut-cell-group
v-for="(item, key) in connectedDevices"
:key="key"
:title="item.name"
>
<nut-cell
v-for="(value, itemKey) in item"
:key="itemKey"
:title="itemKey"
:sub-title="$filters.stringify(value)"
></nut-cell>
</nut-cell-group>
</demo-content>
</template>
<script setup lang="ts">
import { useBluetooth } from 'taro-hooks';
const [{ devices, connectedDevices, adapter }] = useBluetooth();
</script>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ |