更新记录

v1.0.2023.09.14(2023-09-17)

1、修复iOSuuid和Android参数不对应的bug。 2、修改iOS write方法发送不成功。 3、修改Android write方法success为false的bug。

v1.0.2023.09.05(2023-09-07)

修复iOS write方法bytes和hexStr判断bug

v1.0.2023.09.01(2023-09-04)

1、修复未搜索到设备后导致的连接失败。 2、增加iOS缺失的部分接口和功能。

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:4.4 - 12.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 适用版本区间:11 - 16

原生插件通用使用流程:

  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原生插件配置”->”云端插件“列表中删除该插件重新选择


前言

XM-Ble是一款在Android和iOS中使用BLE蓝牙的插件,提供扫描,连接,读取数据,写入数据,notify数据等功能。需要帮助,请联系作者,QQ:1804945430(备注来源)。

使用说明

<template>

    <div>
        <button type="primary" @click="initBleSDK">初始化</button>
        <button type="primary" @click="isSupportBle">是否支持BLE蓝牙</button>
        <button type="primary" @click="isBlueEnable">蓝牙是否开启</button>
        <button type="primary" @click="enableBluetooth">打开蓝牙</button>
        <button type="primary" @click="disableBluetooth">关闭蓝牙</button>
        <button type="primary" @click="startScan">开始扫描</button>
        <button type="primary" @click="stopScan">停止扫描</button>
        <button type="primary" @click="connect">连接</button>
        <button type="primary" @click="disconnect">断开连接</button>
        <button type="primary" @click="read">读取</button>
        <button type="primary" @click="readRssi">读取rssi</button>
        <button type="primary" @click="setMtu">写入mtu</button>
        <button type="primary" @click="write">写入</button>
        <button type="primary" @click="notify">开始notify</button>
        <button type="primary" @click="stopNotify">停止notify</button>
        <button type="primary" @click="release">释放资源</button>
    </div>
</template>

<script>
    // 获取 module 
    var sdkModule = uni.requireNativePlugin("XM-BleModule2")
    const modal = uni.requireNativePlugin('modal');
    export default {
        onLoad() {

        },
        methods: {
            //初始化
            initBleSDK() {
                //不需要设置的参数请注释掉
                sdkModule.initBleSDK({
                    logEnable: false, // 设置是否输出打印蓝牙日志,uniapp无法看到,故设置为false
                    connectRetryCount: 1, //连接异常时(如蓝牙协议栈错误),重新连接次数
                    connectTimeout: 30, //连接超时时间,单位秒
                    operateTimeout: 30, //操作超时时间,单位秒
                    splitWriteNum: 20, //写入分片大小,默认20个字节
                    //1. 如果项目中的设备是统一类型(服务,特征uuid相同),则推荐在初始化时把服务,特征的uuid配置完整。
                    //2. 如果项目中需要兼容多种设备类型(服务,特征uuid不相同),则在通信时传入对应的Uuid的方式进行。
                    uuidService: '0000fff0-0000-1000-8000-00805f9b34fb',//设置主服务的uuid(选填)
                    uuidWrite: '0000fff1-0000-1000-8000-00805f9b34fb',//设置可写特征的uuid(选填)
                    uuidRead: '0000fff4-0000-1000-8000-00805f9b34fb',//设置可读特征的uuid (选填)
                    uuidNotify: '0000fff4-0000-1000-8000-00805f9b34fb'//设置可通知特征的uuid (选填,库中默认已匹配可通知特征的uuid)
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            isSupportBle() {
                var ret = sdkModule.isSupportBle();
                console.log(ret)
            },
            isBlueEnable() {
                var ret = sdkModule.isBlueEnable();
                console.log(ret)
            },
            enableBluetooth() {
                sdkModule.enableBluetooth();
            },
            disableBluetooth() {
                sdkModule.disableBluetooth();
            },
            //开始扫描
            startScan() {
                sdkModule.startScan({
                    services: [] //过滤的uuid数组
                }, (ret) => {
                    //扫描回调结果
                    //console.log(ret.data.bleName)
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            //停止扫描
            stopScan() {
                sdkModule.stopScan();
            },
            connect() {
                sdkModule.connect({
                    macAddress: '22:02:16:14:49:73' //mac地址
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            disconnect() {
                sdkModule.disconnect({
                    macAddress: '22:02:16:14:49:73' //mac地址
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            read() {
                sdkModule.read({
                    macAddress: '', //mac地址
                    uuid_service: '',//可空
                    uuid_characteristic_read: ''//可空
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            readRssi() {
                sdkModule.readRssi({
                    macAddress: '' //mac地址
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            setMtu() {
                sdkModule.setMtu({
                    macAddress: '', //mac地址
                    mtu: 200
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            write() {
                sdkModule.write({
                    macAddress: '', //mac地址
                    uuid_service: '',//可空
                    uuid_characteristic_write: '',//可空
                    bytes: [0xFF,0xFE,0xFD,0xFC,0xFB,0xFA],//同时输入时优先
                    hexStr: 'FFFEFDFCFBFA'//bytes为空时可用
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            notify() {
                sdkModule.notify({
                    macAddress: 'AA:FC:12:04:C0:37', //mac地址
                    uuid_service: '0000feea-0000-1000-8000-00805f9b34fb',//可空
                    uuid_characteristic_notify: '00002aa1-0000-1000-8000-00805f9b34fb'//可空
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            stopNotify() {
                sdkModule.stopNotify({
                    macAddress: '', //mac地址
                    uuid_service: '',//可空
                    uuid_characteristic_notify: ''//可空
                }, (ret) => {
                    //扫描回调结果
                    console.log(ret)
                    modal.toast({
                        //发送操作结果
                        message: ret,
                        duration: 1.5
                    });
                });
            },
            release() {
                sdkModule.release();
            }
        }
    }
</script>

支持定制,联系QQ:1804945430,微信:18980392735 请备注来源

隐私、权限声明

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

蓝牙、定位

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

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

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