更新记录

1.0.0(2025-09-10)

鸿蒙蓝牙


平台兼容性

uni-app(4.76)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
× × - - × ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
× × × × × × × × × × ×

uni-app x(4.76)

Chrome Safari Android iOS 鸿蒙 微信小程序
× × × × -

其他

多语言 暗黑模式 宽屏模式

an-flashlight

开发文档

UTS 语法 UTS API插件 UTS uni-app兼容模式组件 UTS 标准模式组件 Hello UTS

插件说明

该插件只支持鸿蒙,之后有时间可以ios、安卓环境; 连接发送数据/接受数据会在之后支持

插件方法说明

方法 文件说明 返回说明
setPermissUser 获取蓝牙权限 返回number格式,0表示已经授权 -1表示没有授权
getPermissUser 检查当前是否授权 返回number格式,0表示已经授权 -1表示没有授权
getState 获取蓝牙是否打开 返回number格式,0 是关闭 2是打开
openBluetooth 打开蓝牙
closeBluetooth 关闭蓝牙
getBluetooth(query: OnResultType) 扫描周围的蓝牙 返回类型见 OnResultType
setBluetoothStop 停止扫描
setPairDevice(query: PairDeviceType) 连接设备 返回扫描列表返回类型是 PairedDevices[]
offPairDevice 将已经连接并且订阅的取消订阅
getPairedDevices(query : DeviceMACType) 获取已配对设备信息 返回扫描列表返回类型是 PairedDevices[]
getRemoteDeviceName(query : DeviceNameType) 获取设备名称 返回string类型,设备名称
onBluetoothA2DP(query : ProfileDeviceType) 创建A2DP订阅 返回见 ProfileDeviceType
offBluetoothA2DP 取消A2DP订阅
onBluetoothHFP(query : ProfileDeviceType) 创建HFP订阅 返回见 ProfileDeviceType
offBluetoothHFP 取消HFP订阅
onBluetoothHID(query : ProfileDeviceType) 创建HID订阅 返回见 ProfileDeviceType
offBluetoothHID 取消HID订阅

类型说明

// 扫描设备类型
export type OnResultType = {
    count : number, // 扫描时间(鸿蒙默认扫描在14秒左右,倒计时最后会自动停止扫描)
    isheavy ?: boolean, // 是否去重(扫描的结果会有deviceName为空的情况。默认清除和去重)
    onResult : (res : UTSJSONObject[]) => void, // 订阅回调
    offResult ?: (res : UTSJSONObject[]) => void, // 取消回调
}
// 连接设备类型
export type PairDeviceType = {
    device : string, //设备地址
    success ?: () => void, // 成功回调
    fail ?: (res : any) => void, // 失败回调
    onResult ?: (res : connection.BondStateParam) => void, // 订阅回调
    offResult ?: (res : UTSJSONObject[]) => void, // 取消回调
}
// A2DP/HFP/HID 连接方式类型
export type ProfileDeviceType = {
    device : string, //设备地址
    success ?: () => void, // 成功回调
    fail ?: (res : any) => void, // 失败回调
    onResult ?: (res : baseProfile.StateChangeParam) => void, // 订阅回调
    offResult ?: (res : any) => void, // 失败回调
}
// 设备地址编码类型
export type DeviceMACType = {
    alias ?: boolean, // 表示是否获取对端蓝牙设备别名。 true表示获取对端蓝牙设备别名,false表示获取对端蓝牙设备原始名称。
}
// 设备地址信息返回 类型
export type PairedDevices = {
    deviceName : string, //地址名称
    deviceId : string, // 设备物理地址
}
// 设备名称类型
export type DeviceNameType = {
    device : string, //设备地址
    alias ?: boolean, // 表示是否获取对端蓝牙设备别名。 true表示获取对端蓝牙设备别名,false表示获取对端蓝牙设备原始名称。
}

使用说明

<template>
    <scroll-view style="flex: 1;">
        <view class="fy-head-flex">
            <text class="fy-head-text">传统蓝牙</text>
            <button @click="setPermiss">获取设备权限</button>
            <button @click="getPermiss">获取当前是否授权</button>
            <button @click="getState">获取蓝牙是否打开</button>
            <button @click="open">打开蓝牙</button>
            <button @click="close">关闭蓝牙</button>
            <button @click="getBlue">获取附近蓝牙设备</button>
            <button @click="setStop">停止扫描附近蓝牙设备</button>
            <button @click="openBlue">打开蓝牙列表</button>
            <button @click="offDevice">将已经连接并且订阅的取消订阅</button>
            <button @click="getDevices">获取已配对设备信息</button>
            <button @click="onProfile">订阅A2DP/HFP/HID</button>
            <button @click="offProfile">取消订阅A2DP/HFP/HID</button>
            <button @click="setUUID">随机生成UUID</button>
        </view>
        <view class="fy-head-flex">
            <text class="fy-head-text">低功耗蓝牙</text>
            低功耗过于复杂用另外用一个插件写
        </view>
    </scroll-view>
    <!-- 组件 -->
    <view v-show="isOpen" class="fy-popup-back" @click="isOpen = false"></view>
    <view class="fy-popup-view" v-show="isOpen">
        <scroll-view class="fy-scroll-view">
            <view class="fy-bule-list">
                <view class="fy-bile-item" v-for="item in blueList">
                    <view class="fy-bult-code">
                        <text>设备名称:{{ item.deviceName }}</text>
                        <text>设备编码: {{ item.deviceId }}</text>
                    </view>
                    <view @click="setDevice(item)">
                        <text class="fy-blue-pair">连接</text>
                    </view>
                </view>
                <view style="height: 300rpx;"></view>
            </view>
        </scroll-view>
    </view>
</template>

<script setup>
    import { 
        getBluetooth, 
        getPermissUser, 
        getBluetoothState, 
        openBluetooth, 
        setPermissUser, 
        closeBluetooth, 
        setBluetoothStop, 
        setPairDevice, 
        offPairDevice, 
        getPairedDevices, 
        onBluetoothA2DP, 
        onBluetoothHFP, 
        onBluetoothHID, 
        setConnectAllowedProfiles,
        offBluetoothA2DP,
        offBluetoothHFP,
        offBluetoothHID,
        getEnerateRandomUUID,
        onStartListen,
    } from '@/uni_modules/an-bluetooth'

    const blueList = ref<UTSJSONObject[]>([{}]); // 蓝牙列表
    const isOpen = ref<boolean>(false); // 打开蓝牙列表
    // 获取蓝牙权限
    const setPermiss = async () => {
        let number = await setPermissUser();
        // number.authResults: [0],
        // 0表示已经授权 -1表示没有授权
        console.log(number);
    }
    // 检查当前是否授权
    const getPermiss = async () => {
        let number = await getPermissUser();
        console.log(number);
    }
    // 获取蓝牙是否打开
    const getState = () => {
        // 0 是关闭 2是打开
        let state = getBluetoothState();
        console.log(state);
    }
    // 打开蓝牙
    const open = () => {
        openBluetooth()
    }
    // 关闭蓝牙
    const close = () => {
        closeBluetooth()
    }
    // 扫描周围的蓝牙
    const getBlue = () => {
        getBluetooth({
            count: 12,
            onResult: (res) => {
                console.log(res);
            },
            offResult: (res) => {
                console.log(res.length);
                blueList.value = res;
            }
        })
    }
    // 停止扫描
    const setStop = () => {
        let list = setBluetoothStop();
        blueList.value = list;
    }
    // 打开蓝牙列表
    const openBlue = () => {
        if (blueList.value.length <= 0) {
            uni.showToast({
                title: '没有数据,先扫描看看'
            })
            return;
        }
        isOpen.value = true;
    }
    // 连接
    const setDevice = (item : UTSJSONObject) => {
        setPairDevice({
            device: item.deviceId,
            success: () => {
                console.log('32312321');
            },
            onResult: (res) => {
                console.log(res);
            }
        })
    }
    // 将已经连接并且订阅的取消订阅
    const offDevice = () => {
        offPairDevice();
    }
    // 获取已配对设备信息
    const getDevices = () => {
        let list = getPairedDevices({});
        console.log(list);
    }

    // 订阅A2DP/HFP/HID
    // 先 打开蓝牙列表 连接设备在 订阅
    const onProfile = () => {
        let list = getPairedDevices({});
        console.log(list);
        if (list.length <= 0) return;
        // https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/bluetooth-overview-V13
        // a2dp模块(高级音频分发配置文件):A2DP是Advanced Audio Distribution Profile的缩写,即高级音频分发配置文件。它是一种蓝牙协议,允许无线传输高品质音频流,例如音乐或语音通话,同时支持双向通信,因此可以用于耳机、扬声器、汽车音响等设备。
        // ble模块(低功耗蓝牙):BLE是Bluetooth Low Energy的缩写,意为“低功耗蓝牙”。它是一种能够在低功耗情况下进行通信的蓝牙技术,与传统蓝牙相比,BLE的功耗更低,适用于需要长时间运行的低功耗设备,如智能手表、健康监测设备、智能家居等。
        // hfp模块(免提模式):HFP模块是指蓝牙耳机或车载蓝牙设备中的Hands-Free Profile,即“免提模式”。HFP允许用户通过蓝牙连接手机或其他蓝牙设备,实现免提通话和语音控制等功能。
        // hid模块(人机接口设备):HID是Human Interface Device的缩写,即人机接口设备。在蓝牙中,HID模块是一种允许用户通过蓝牙连接键盘、鼠标、游戏手柄等人机接口设备的模块。用户可以通过HID模块将这些设备连接到蓝牙主机上,实现无线控制和输入。

        // 创建A2DP订阅
        // 连接之前最好使用 onBluetoothA2DP,并且在success里面
        // 因为有可能设备不支持A2DP、HFP、HID,直接连接可能会报错
        // 三个订阅方式最好连接一次,连接多次可能报错

        let deviceId = list[0].deviceId;

        // onBluetoothA2DP({
        //  device: deviceId,
        //  success: () => {
        //      console.log('订阅成功');
        //      // 连接
        //      setConnectAllowedProfiles({
        //          device: deviceId,
        //          success: () => {
        //              console.log('连接成功');
        //          },
        //          fail: (err) => {
        //              console.log('连接失败',err);
        //          }
        //      })
        //  },
        //  fail: (err) => {
        //      console.log('订阅失败',err);
        //  }
        // })
        onBluetoothHFP({
            device: deviceId,
            onResult: (res) => {
                console.log(res);
            },
            success: () => {
                console.log('订阅成功');
                // 连接
                setConnectAllowedProfiles({
                    device: deviceId,
                    success: () => {
                        console.log('连接成功');
                    },
                    fail: (err) => {
                        console.log('连接失败', err);
                    }
                })
            },
            fail: (err) => {
                console.log('订阅失败', err);
            }
        })
        // onBluetoothHID({
        //  device: deviceId,
        //  success: () => {
        //      console.log('订阅成功');
        //      // 连接
        //      setConnectAllowedProfiles({
        //          device: deviceId,
        //          success: () => {
        //              console.log('连接成功');
        //          },
        //          fail: (err) => {
        //              console.log('连接失败',err);
        //          }
        //      })
        //  },
        //  fail: (err) => {
        //      console.log('订阅失败',err);
        //  }
        // })
    }
    // 取消订阅A2DP/HFP/HID
    const offProfile = () => {
        try {
            // 取消A2DP订阅
            offBluetoothA2DP();
            // 取消HFP订阅
            offBluetoothHFP();
            // 取消HID订阅
            offBluetoothHID();
        } catch (err) {

        }
    }
    // 随机生成UUID
    const setUUID = () => {
        let uuid = getEnerateRandomUUID();
        console.log(uuid);
    }
</script>

<style lang="scss">
    .fy-popup-back {
        position: absolute;
        top: 0;
        width: 100%;
        height: 100%;
        z-index: 90;
        background-color: #00000038;
    }
    .fy-popup-view {
        position: absolute;
        bottom: 0;
        width: 100%;
        z-index: 100;
    }
    .fy-scroll-view {
        height: 1000rpx;
        background-color: #fff;
        border-radius: 10px 10px 0 0;
        padding: 20px 10px;
    }

    .fy-bile-item {
        padding: 10px 0;
        border-bottom: 1px solid #000;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
    }

    .fy-blue-pair {
        text-align: center;
        width: 150rpx;
        padding: 5px 10px;
        background-color: #008AFF;
        color: #fff;
        border-radius: 5px;
    }

    .fy-bult-code {
        width: 70%;
    }
    .fy-head-flex {
        border: 1px solid #555;
        border-radius: 5px;
        padding: 10px;
        margin: 10px;
    }
    .fy-head-text {
        color: #008AFF;
        font-size: 20px;
        font-weight: bold;
        margin-bottom: 10px;
    }
</style>

隐私、权限声明

1. 本插件需要申请的系统权限列表:

ohos.permission.ACCESS_BLUETOOTH

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。