更新记录

1.1.1(2023-11-24)

兼容 Android 13 兼容 HarmonyOS 3.0

1.1.0(2022-09-06)

1、支持02 A7 B8 47 23 06类型的16进制命令 2、完善使用示例和文档

1.0.9(2021-10-12)

修复个别手机连接崩溃得问题

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:5.0 - 11.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 ×

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


经典蓝牙插件使用文档

一、引入插件

复制代码const bt = uni.requireNativePlugin('Common-BT');

二、配置

App原生插件配置中引入云插件 Common-BT

三、常用方法

  • 1、检查是否有蓝牙权限

    复制代码bt.hasPermission(result => {
        //result数据:{"status":true} 如果没有权限程序会自动申请权限
        const msg = JSON.stringify(result);
        console.log('蓝牙权限:'+msg.status)         
    });

    status : true表示授权;false表示未授权。

  • 2、检查设备是否支持蓝牙

    复制代码bt.isSupport(result => {
        //result数据:{"status":true} 
        const msg = JSON.stringify(result);
        console.log('是否支持蓝牙:'+msg.status)           
    });

    status : true表示支持蓝牙设备;false表示不支持。

  • 3、判断蓝牙是否打开

    复制代码bt.isOpen(result => {
        //result数据:{"status":true} 
        const msg = JSON.stringify(result);
        console.log('蓝牙是否打开:'+msg.status)           
    });

    status : true表示蓝牙已打开;false表示蓝牙未打开。

  • 4、打开蓝牙

    复制代码bt.openBT(result => {
        //result数据:{"status":true} 
        const msg = JSON.stringify(result);
        console.log('开启蓝牙是否成功:'+msg.status)         
    });

    status : true表示打开成功;false表示打开失败。

  • 5、关闭蓝牙

    复制代码bt.closeBT(result => {
      //result数据:{"status":true} 
      const msg = JSON.stringify(result);
      console.log('蓝牙是否关闭:'+msg.status)           
    });

    status : true表示关闭成功;false表示关闭失败。

  • 6、搜索蓝牙设备

    复制代码bt.listBT(result => {
      //result数据:{"msg":"搜索完成","list":[{"name":"蓝牙名称","address":"mac地址","status":"配对状态"}]} 
      const msg = JSON.stringify(result);
      console.log('蓝牙设备列表:'+msg.status)           
    });

    每搜索到一个蓝牙设备就追加到设备列表返回;

    code: 0开始搜索;2搜索中;1搜索完成

    list: 存放搜索到设备列表,如果没有发现设备,返回 []

  • 7、蓝牙配对

    复制代码bt.pairBT({"btAddress":"mac地址"},result => {
    //result数据:{"code":100,"msg":"配对完成,请刷新列表"} 
    const msg = JSON.stringify(result);
    console.log('配对结果:'+msg.status)         
    });

    code: 2表示配对中;200表示配对成功;-200表示配对未成功,可能设备地址错误或者未同意配对

  • 8、获取已配对列表

    复制代码bt.listBondedBT(result => {
    //result数据:{"msg":"搜索完成","list":[{"name":"蓝牙名称","address":"mac地址","status":"配对状态"}]} 
    const msg = JSON.stringify(result);
    console.log('已配对列表:'+msg.status)            
    });

    code: 200

    list: 存放已配对设备列表,如果没有已配对得设备,返回 []

  • 9、连接蓝牙

    复制代码bt.connectBT({"btAddress":"mac地址"},result => {
    //result数据:{"code":100,"msg":"连接成功"}
    const msg = JSON.stringify(result);
    console.log('连接蓝牙:'+msg.status)         
    });

    说明:此连接方法为长连接,连接成功后,如果蓝牙断开也会回调该方法。连接成功后,自动接收数据。

    code: 200表示连接成功;-200表示连接异常

  • 10、查找蓝牙设备

    复制代码bt.findBT({"btAddress":"mac地址"},result => {
    //result数据:{"code":100,"msg":"连接成功"}
    const msg = JSON.stringify(result);
    console.log('蓝牙设备:'msg)         
    });
  • 11、发送消息

    复制代码特别说明:chareset用于设置编码,默认GBK(可不设置);isHex是否16进制发送,默认false,发送:02 89 A4 B8这种16进制的命令设置为true 
    bt.sendMsg({"cmd": "发送的消息内容",isHex:false}, result => {
    const content = JSON.stringify(result);
    modal.toast({
       message: content,
       duration: 1.5
    });
    });

    发送文本类型的字符串信息。

    code: 200表示连接成功;-200表示连接异常

  • 12、接收消息

    连接蓝牙时,自动接收设备返回的信息。

  • 13、判断蓝牙连接状态 bt.connectStatus({ }, result => { //接收 const content = JSON.stringify(result); modal.toast({ message: content, duration: 1.5 }); });

  • 14、完整示例

复制代码<template>
    <view class="button-sp-area">
        <button type="primary" plain="true" @click="hasPermission()">判断蓝牙权限</button>
        <button type="primary" plain="true" @click="isSupport()">是否支持蓝牙</button>
        <button type="primary" plain="true" @click="isOpen()">蓝牙是否打开</button>
        <button type="primary" plain="true" @click="openBT()">打开蓝牙</button>
        <button type="primary" plain="true" @click="closeBT()">关闭蓝牙</button>
        <button type="primary" plain="true" @click="listBT()">蓝牙设备列表</button>
        <button type="primary" plain="true" @click="findBT()">查找蓝牙设备</button>
        <button type="primary" plain="true" @click="pairBT()">蓝牙配对</button>
        <button type="primary" plain="true" @click="removeBond()">蓝牙取消配对</button>
        <button type="primary" plain="true" @click="listBondedBT()">已配对列表</button>
        <button type="primary" plain="true" @click="connectBT()">打开连接</button>
        <button type="primary" plain="true" @click="connectStatus()">连接状态</button>
        <button type="primary" plain="true" @click="sendMsg()">发送数据</button>
        <button type="primary" plain="true" @click="breakBT()">断开连接</button>
    </view>
</template>
<script>
    //弹框
    const modal = uni.requireNativePlugin('modal');
    const bt = uni.requireNativePlugin('Common-BT');
    export default {
        data() {
            return {
                title: ''
            }
        },
        onLoad() {

        },
        methods: {
            hasPermission() {
                bt.hasPermission(result => {
                    //result数据:{"status":true} 如果没有权限程序会自动申请权限
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('蓝牙权限:' + result.status);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            isSupport() {
                bt.isSupport(result => {
                    //result数据:{"status":true} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('是否支持蓝牙:' + result.status);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            isOpen() {
                bt.isOpen(result => {
                    //result数据:{"status":true} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('蓝牙是否打开:' + result.status);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            openBT() {
                bt.openBT(result => {
                    //result数据:{"status":true} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('开启蓝牙是否成功:' + result.status);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            closeBT() {
                bt.closeBT(result => {
                    //result数据:{"status":true} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('蓝牙是否关闭:' + result.status);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            listBT() {
                bt.listBT(result => {
                    //result数据:{"msg":"搜索完成","list":[{"name":"蓝牙名称","address":"mac地址","status":"配对状态"}]} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('蓝牙设备列表:' + result.list);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            pairBT() {
                bt.pairBT({
                    "btAddress": "1C:52:16:40:55:BC"//  88:10:8F:C9:33:C5
                }, result => {
                    //result数据:{"code":100,"msg":"配对完成,请刷新列表"} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('配对结果:' + result.msg);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            removeBond() {
                bt.removeBond({
                    "btAddress": "1C:52:16:40:55:BC"
                }, result => {
                    //result数据:{"code":100,"msg":"配对完成,请刷新列表"} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('取消结果:' + result.msg);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            listBondedBT() {
                bt.listBondedBT(result => {
                    //result数据:{"msg":"搜索完成","list":[{"name":"蓝牙名称","address":"mac地址","status":"配对状态"}]} 
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    console.log('已配对列表:' + result.list);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            connectBT() {
                //04:D1:3A:C3:20:C5  
                bt.connectBT({
                    "btAddress": "1C:52:16:40:55:BC"
                }, result => {
                    //result数据:{"code":100,"msg":"连接成功"},并接收数据
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            breakBT() {
                bt.breakBT(result => {
                    //result数据:{"code":100,"msg":"连接成功"}
                    const msg = JSON.stringify(result);
                    console.log(msg);
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            findBT() {
                bt.findBT({
                    "btAddress": "88:10:8F:C9:33:C5"
                }, result => {
                    //result数据:{"name":"蓝牙名称","address":"mac地址","status":"配对状态"}
                    const msg = JSON.stringify(result);
                    console.log(msg);               
                    modal.toast({
                        message: msg,
                        duration: 1.5
                    });
                });
            },
            sendMsg() {
                bt.sendMsg({
                    "cmd": "123"
                }, result => {
                    //接收
                    const content = JSON.stringify(result);
                    modal.toast({
                        message: content,
                        duration: 1.5
                    });
                });
            },
            connectStatus() {
                bt.connectStatus(result => {
                    //接收
                    const content = JSON.stringify(result);
                    modal.toast({
                        message: content,
                        duration: 1.5
                    });
                });
            },
            sendFile() {
                bt.sendFile({
                    "filePath": "文件绝对路径"
                }, result => {
                    //接收
                    const content = JSON.stringify(result);
                    modal.toast({
                        message: content,
                        duration: 1.5
                    });
                });
            },
        }
    }
</script>

<style>
    button {
        margin-top: 30upx;
        margin-bottom: 30upx;
    }

    .button-sp-area {
        margin: 0 auto;
        width: 60%;
    }

    .content {
        text-align: center;
        height: 400upx;
    }

    .wrapper {
        flex-direction: column;
        justify-content: center;
    }

    .button {
        width: 200px;
        margin-top: 30px;
        margin-left: 20px;
        padding-top: 20px;
        padding-bottom: 20px;
        border-width: 2px;
        border-style: solid;
        border-color: #458B00;
        background-color: #458B00;
    }

    .text {
        font-size: 30px;
        color: #666666;
        text-align: center;
    }
</style>

四、更新说明

  • 兼容 Android 13
  • 兼容 HarmonyOS 3.0

    五、定制服务和技术支持

    QQ:690898091

    wx:qq690898091

隐私、权限声明

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

蓝牙权限

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

插件不采集任何数据

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

188***@163.com (已付费)

2025-03-15

有没有大佬能交流一下呀

eyesky

2025-01-02

这个数据收到的包含汉字都是乱码啊?

423***@qq.com (已付费)

2024-12-07

自定义调试基座运行的时候没问题,但正式打包后用不了。打包的时候,包名,插件配置都检查过了,都没写错。

160***@qq.com (已付费)

2024-10-25

listBT这个方法 第一次搜索没搜到蓝牙设备 第二次为啥不执行

106***@qq.com (已付费)

2023-11-01

这插件在发送命令上有问题,只是接收的想用还可以,需要给设备发送指令的话,还是不要买了,贵,还不好用

sun***@mohekeji.com (已付费)

2023-10-30

android13现在控制不了蓝牙,只能是11以下的好使了,是还没适配么,评论区13是咋好用的啊?

MacroCheng (已付费)

2023-09-25

为什么在Android 13的手机上经常就搜不到设备,但是Android 11的设备上就非常稳定都能搜到,这个可以帮忙看下么?

158***@qq.com

2023-06-05

可以同时连接两个蓝牙设备吗?

桂在坚持 2023-06-21

经典蓝牙不可以,低功耗蓝牙插件可以

kakalc

2023-05-12

作者还在吗,怎么联系到你啊 qq有问题加不上

桂在坚持 2023-06-21

加微信

909***@qq.com (已付费)

2023-04-28

安卓5到13都没问题 全都正常使用 ,发送命令很方便 , 但是(鸿蒙系统)搜索不到蓝牙设备 貌似是Android M 以上版本需要显式获取授权,网上有处理方案,请大神处理下

桂在坚持 2023-06-21

1

2022-08-23

请问怎么获取当前连接的蓝牙设备

2021-12-13

{"code":200,"value":"d\u0000\u0000\u0000\u0014\u0000\u0006\u0000\u000b\u0000\u0005\u0000\t\u0000\u0013\u0000\u0000\u0000\u000...

2021-09-23

断开连接后,就连不上,需要重启app

2021-09-22

该插件是否能用于微信小程序,如何使用

2021-08-09

该插件是否能用于微信小程序,如何使用