更新记录

1.1.5(2025-03-14)

1.1.4(2025-03-12)

UTSBluetoothListBle 蓝牙延迟搜索时候去除相同物理地址的过滤,显示所有搜索到的蓝牙列表

1.1.3(2025-03-11)

添加 UTSBluetoothListBle API 使用ble蓝牙低延迟方式获取蓝牙列表

查看更多

平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.6.8,Android:6.0,iOS:不支持,HarmonyNext:不确定 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序 鸿蒙元服务
× × × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

原生安卓Android ble低功耗蓝牙插件

需要打包自定调试包才能测试

搜索或者连接蓝牙前都需要先初始化蓝牙,初始化成功后才能使用

调用插件 :

import {
    UTSBluetoothManager,
    UTSBluetoothList,
    UTSBluetoothListBle,
    UTSBluetoothStop,
    UTSBluetoothConnect,
    UTSBluetoothGetUUID,
    UTSWriteCharacteristic,
    UTSSetCharacteristicNotification,
    UTSrequestMtu
} from "@/uni_modules/mushan-bluetooth";

完整vue3页面调用:

    <template>
    <view>
        <button @click="BluetoothManager">初始化蓝牙</button>
        <button @click="BluetoothList">获取蓝牙列表,点击获取的列表直接连接</button>
        <view style="padding: 30rpx 0;" v-for="(item,index) in bluetoothList">
            <text  @click="BluetoothGetUUID(item.address)">名称:{{item.name}} 别名:{{item.alias}} 地址:{{item.address}}</text>
        </view>
        <button @click="BluetoothListBle">ble低延时方式获取蓝牙列表,点击获取的列表直接连接</button>
        <view style="padding: 30rpx 0;" v-for="(item,index) in bluetoothListBle">
            <text  @click="BluetoothGetUUID(item.address)">名称:{{item.name}} 别名:{{item.alias}} 地址:{{item.address}}</text>
        </view>
        <button @click="BluetoothGetUUID('04:57:91:0A:07:90')">已知蓝牙地址获取测试查看uuid拥有的权限</button>
        <button @click="BluetoothConnect('04:57:91:0A:07:90')">已知地址直接连接蓝牙</button>
        <button @click="BluetoothStop">停止搜索</button>
        <button @click="WriteCharacteristic">发送数据</button>
        <button @click="SetCharacteristicNotification(true)">打开通知</button>
        <button @click="SetCharacteristicNotification(false)">关闭通知</button>
        <button @click="requestMtu(200)">设置MTU</button>
    </view>
</template>

<script setup>
    import {
        onShow,
        onReady,
        onReachBottom,
        onPullDownRefresh,
        onShareAppMessage
    }
    from '@dcloudio/uni-app';
    import {
        getCurrentInstance,
        ref,
        reactive,
        inject,
        computed,
        onMounted
    } from 'vue';

    import {
        UTSBluetoothManager,
        UTSBluetoothList,
        UTSBluetoothListBle,
        UTSBluetoothStop,
        UTSBluetoothConnect,
        UTSBluetoothGetUUID,
        UTSWriteCharacteristic,
        UTSSetCharacteristicNotification,
        UTSrequestMtu
    } from "@/uni_modules/mushan-bluetooth";

    let bluetoothList = ref([]);

    let bluetoothListBle = ref([]);

    //初始化蓝牙,使用前必须先初始化,返回“蓝牙初始化成功” 就是初始化完成
    function BluetoothManager(){
        UTSBluetoothManager((res)=>{
            console.log(res)
        })
    }

    //获取蓝牙列表
    function BluetoothList(){
        bluetoothList.value = []
        UTSBluetoothList((res)=>{
            console.log(res);
            if(res != '启动失败'){
                bluetoothList.value.push(JSON.parse(res))
            }else{
                console.log(res)
            }
        })
    }

    //ble低延迟获取蓝牙列表
    function BluetoothListBle(){
        bluetoothListBle.value = []
        UTSBluetoothListBle((res)=>{
            console.log(res);
            if(res != '启动失败'){
                bluetoothListBle.value.push(JSON.parse(res))
            }else{
                console.log(res)
            }
        })
    }

    //停止搜索蓝牙并且断开蓝牙连接
    function BluetoothStop(){
        bluetoothList.value = []
        bluetoothListBle.value = []
        UTSBluetoothStop((res)=>{
            console.log(res)
        });
    }

    //打开通知
    function SetCharacteristicNotification(item){
        UTSSetCharacteristicNotification(item,(res)=>{
            console.log(res);
        })
    }

    //设置mtu,可能有时候不需要设置mtu也可以直接发送超过20个字节以上的数据,应该是看设备的设置
    function requestMtu(item){
        UTSrequestMtu(item,(res)=>{
            console.log(res);
        })
    }

    //查看获取的uuid地址,并且获取该蓝牙物理地址的所有uuid的读写听权限是否开启,该调用会关闭搜索和重新关掉蓝牙然后再去查找
    function BluetoothGetUUID(device){
        UTSBluetoothGetUUID(device,(res)=>{
            let data = JSON.parse(res);

            if(data.type == 'ok'){
                console.log(data.msg);
            }

            if(data.type == 'fail'){
                console.log(data.msg);
            }

        })
    }

    //连接蓝牙read读权限,write写权限,notify监听通知权限,会匹配三种权限都相同的第一个UUID进行通信,例如read:true,write:true,notify:false,只会匹配 有读权限,有写权限,无监听权限 的UUID
    function BluetoothConnect(device){
        UTSBluetoothConnect(device,{read:true,write:true,notify:true},(res)=>{

            let data = JSON.parse(res);

            //连接成功和断开都在这里触发,返回  蓝牙断开连接 , 蓝牙连接成功 , 蓝牙连接失败
            if(data.type == 'connect'){
                console.log(data.msg);
            }

            //发送后的返回
            if(data.type == 'write'){
                // 将字节数组转换为2位数的十六进制字符串
                const hexString = JSON.parse(data.msg)
                console.log(TwoBasesystem(hexString));
            }

            //监听后的返回
            if(data.type == 'notify'){
                // 将字节数组转换为2位数的十六进制字符串
                const hexString = JSON.parse(data.msg)
                console.log(TwoBasesystem(hexString));
            }

        });
    }

    //获取返回的数据转换成2位数的16进制
    function TwoBasesystem(data){
        let num = []
        data.forEach((item)=>{
            num.push((item & 0xFF).toString(16).padStart(2, '0'));
        })
        return num
    }

    //发送指令,发送过去的是10进制的字符数组,如果是0xCE这样的16进制会自动转换,如果只是两个字符的16进制  比如ce,可以先转换成parseInt('ce',16) 10进制的类型进行发送就行
    function WriteCharacteristic(device){

        let array = []

        //头码,命令,长度
        array.push(0x5A)
        array.push(0xCE)
        array.push(0x10)

        //密码
        let Password = String('000000').split('')

        let Bytedata = {
            Byte1: 0x01,
            Byte2: 0x00,
            Byte3: 0x00,
            Byte4: 0x00,
            Byte5: 0x00,
            Byte6: parseInt(Password[0],16),
            Byte7: parseInt(Password[1],16),
            Byte8: parseInt(Password[2],16),
            Byte9: parseInt(Password[3],16),
            Byte10: parseInt(Password[4],16),
            Byte11: parseInt(Password[5],16),
        }

        //数据(11bytes)
        array.push(Bytedata.Byte1)
        array.push(Bytedata.Byte2)
        array.push(Bytedata.Byte3)
        array.push(Bytedata.Byte4)
        array.push(Bytedata.Byte5)
        array.push(Bytedata.Byte6)
        array.push(Bytedata.Byte7)
        array.push(Bytedata.Byte8)
        array.push(Bytedata.Byte9)
        array.push(Bytedata.Byte10)
        array.push(Bytedata.Byte11)

        //校验码
        let code
        code = 0x5A ^ 0xCE ^ 0x10
        for (let item in Bytedata) {
            code = code ^ Bytedata[item]
        }

        array.push(code)

        //尾码
        array.push(0xA5);

        UTSWriteCharacteristic(array);
    }

</script>

<style>

</style>

uniappX 使用插件(打包自定调试包才能测试):

<template>
    <view>
        <button @click="BluetoothManager">初始化蓝牙</button>
        <button @click="BluetoothList">获取蓝牙列表,点击获取的列表直接连接</button>
        <view style="padding: 30rpx 0;" v-for="item in bluetoothList">
            <text @click="BluetoothConnect(item['address'] as string)">名称:{{item["name"]}} 别名:{{item["alias"]}} 地址:{{item["address"]}}</text>
        </view>
        <button @click="BluetoothGetUUID('55:C7:CD:08:EE:2C')">获取该地址的UUID</button>
        <button @click="WriteCharacteristic">发送数据</button>
        <button @click="BluetoothStop">停止搜索</button>
        <button @click="SetCharacteristicNotification(true)">打开通知</button>
        <button @click="SetCharacteristicNotification(false)">关闭通知</button>
        <button @click="requestMtu(200)">设置MTU</button>
    </view>
</template>

<script lang="uts">
    import {
        UTSBluetoothManager,
        UTSBluetoothList,
        UTSBluetoothStop,
        UTSBluetoothConnect,
        UTSBluetoothGetUUID,
        UTSWriteCharacteristic, 
        UTSSetCharacteristicNotification,
        UTSrequestMtu,
        limitsObj
    } from "@/uni_modules/mushan-bluetooth";
    export default {
        data() {
            return {
                bluetoothList: [] as UTSJSONObject[],
            }
        },
        onLoad() {

        },
        methods: {

            //初始化蓝牙,使用前必须先初始化,返回“蓝牙初始化成功” 就是初始化完成
            BluetoothManager(){
                UTSBluetoothManager((res:string)=>{
                    console.log(res)
                })
            },

            BluetoothList(){
                this.bluetoothList = []; 
                UTSBluetoothList((res:string)=>{
                    let jsonObj = JSON.parse(res) as UTSJSONObject;
                    if(jsonObj['msg'] == '获取成功'){
                        this.bluetoothList.push(jsonObj)
                    } else {
                        console.log(jsonObj['msg']);
                    }
                });
            },

            //停止搜索蓝牙并且断开蓝牙连接
            BluetoothStop(){
                this.bluetoothList = []
                UTSBluetoothStop((res:string)=>{
                    console.log(res)
                });
            },

            //打开或关闭通知
            SetCharacteristicNotification(item:boolean){
                UTSSetCharacteristicNotification(item,(res)=>{
                    console.log(res);
                })
            },

            //设置mtu,可能有时候不需要设置mtu也可以直接发送超过20个字节以上的数据,大概可能是看设备的设置
            requestMtu(item:Int){
                UTSrequestMtu(item,(res)=>{
                    console.log(res);
                })
            },

            //获取uuid
            BluetoothGetUUID(device:string){
                UTSBluetoothGetUUID(device,(res)=>{
                    let data = JSON.parse<UTSJSONObject>(res)!;

                    if(data["type"]=="ok"){
                        console.log(JSON.parse(data["msg"] as string))
                    }

                    if(data["type"]=="fail"){
                        console.log(data["msg"])
                    }

                });
            },

            //转换获取返回的数据转换成2位数的16进制
            twoBasesystem(data:string):Array<string>{
                let newData = JSON.parse(data);
                let num:Array<string> = [];

                (newData as Array<number>).forEach((item:number)=>{
                    num.push(Integer.toHexString(item.toInt() & 0xFF).padStart(2, '0'))
                })

                return num;
            },

            //连接蓝牙
            BluetoothConnect(device:string){
                let limitsObj:limitsObj = {
                    read:false,
                    write:true,
                    notify:true
                }
                UTSBluetoothConnect(device,limitsObj,(res)=>{

                    let data = JSON.parse<UTSJSONObject>(res)!;

                    if(data["type"]=="connect"){
                        console.log(data["msg"])
                    }

                    if(data["type"]=="write"){
                        console.log(data["msg"])
                        console.log(this.twoBasesystem(data["msg"] as string))
                    }

                    if(data["type"]=="notify"){
                        console.log(data["msg"])
                    }

                });
            },

            //发送指令,发送过去的是10进制的字符数组,如果是0xCE这样的16进制会自动转换,如果只是两个字符的16进制  比如ce,可以先转换成parseInt('ce',16) 10进制的类型进行发送就行
            WriteCharacteristic(){

                let array: number[] = []

                //头码,命令,长度
                array.push(0x5A)
                array.push(0xC1)
                array.push(0x10)

                //密码
                let Password = '000000'.split('')

                //蓝牙对应数据
                let Bytedata = [
                    0x00,
                    0x01,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                ]

                //数据(11bytes)
                array.push(Bytedata[0])
                array.push(Bytedata[1])
                array.push(Bytedata[2])
                array.push(Bytedata[3])
                array.push(Bytedata[4])
                array.push(Bytedata[5])
                array.push(Bytedata[6])
                array.push(Bytedata[7])
                array.push(Bytedata[8])
                array.push(Bytedata[9])
                array.push(Bytedata[10])

                //校验码
                let code: number;
                code = 0x5A ^ 0xC1 ^ 0x10
                for (let item in Bytedata) {
                    code = code ^ Bytedata[item];
                }

                array.push(code)

                //尾码
                array.push(0xA5);

                UTSWriteCharacteristic(array);
            }

        }
    }
</script>

隐私、权限声明

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

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

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

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问