更新记录

1.0.0(2025-01-22)

  • 新版发布

平台兼容性

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

yt-uts-bluetooth

特别提醒

  • 购买本插件前,请先试用、请先试用、请先试用,并充分自测确认满足需求之后再行购买。虚拟物品一旦购买之后无法退款;
  • 如有使用上的疑问、bug,可以进交流群联系作者;
  • 作者可承接各种插件定制;
  • 请在合法范围内使用,若使用本插件做非法开发,本方概不负责;
  • 结合文档和示例代码使用;

插件使用文档 -- 试用需自定义基座

//引入插件
import {
        BluetoothInitConfig,
        BluetoothListener,
        initBluetooth,
        getBondList,
        discoveryBluetooth,
        connectBluetoothWithName,
        ConnectBluetoothOptions,
        disconnectBluetooth,
        GetBoundDeviceOptions,
        BluetoothDeviceInfo,
        SendDataOptions,
        sendData,
        SendDataResult,
        ApiFail,
           destroy
    } from '@/uni_modules/yt-uts-bluetooth'

使用前先初始化

initBluetooth({
    MTU: 1021,//可选参数,默认1021--单次发送数据长度
    readDataInterval: 200,//可选参数,读取数据间隔默认500毫秒
    bluetoothListener: (res) => {//蓝牙监听回调
        switch (res.status) {
            case 1: //读取蓝牙数据
                this.blueToothValue = res.value ?? '';
                break;
            case 2: //连接成功
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 3: //蓝牙已断开
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 5: //扫描到设备回调
                this.discoveryList.push(res.device!)
                break;
            case 9: //设备(手机)关闭蓝牙---设置里关闭蓝牙
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 10: //设备(手机)关闭蓝牙---设置里开启蓝牙
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 11: //配对请求中,向蓝牙发送请求配对
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 12: //配对中
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
            case 13: //配对失败
                uni.showToast({
                    icon: 'none',
                    title: res.mes ?? ''
                })
                break;
        }
    }
} as BluetoothInitConfig)
  • 获取已绑定列表
getBondList({
    success: (res : BluetoothDeviceInfo[]) => {
        console.log(res)
    },
    fail: (error : ApiFail) => {

    }
} as GetBoundDeviceOptions)
  • 扫描周边设备
discoveryBluetooth()
  • 连接指定蓝牙
//名称和address 2选一 address优先  -- 连接结果回调在初始化回调里
connectBluetoothWithName({
    bluetoothName: "",
    address: ""
} as ConnectBluetoothOptions)
  • 向蓝牙发送数据
sendData({
    value: 'hello world',
    callBack: (res : SendDataResult) => {
        uni.showToast({
            icon: res.errCode == 200 ? 'success' : 'error',
            title: res.mes
        })
    }
} as SendDataOptions)
  • 断开蓝牙
disconnectBluetooth()
  • 释放资源
//不需要使用蓝牙时再调用释放,调用此api若需再次使用插件需重新调用初始化
destroy()

完整示例代码

  • uniapp
<template>
    <view class="content">

        <view style="font-size: 28rpx;color: cadetblue;height: 80rpx;background-color: azure;">{{blueToothValue}}</view>

        <view class="header">
            <view class="btn" @click="boundDevices()">获取绑定设备</view>
            <view class="btn" @click="scanDevices()">扫描蓝牙</view>
            <view class="btn" @click="closeBluetooth()">断开蓝牙</view>
            <view class="btn" @click="sendDataAction()">发送数据</view>
        </view>

        <view style="width: 750rpx;display: flex;flex-direction: column;" v-if="bondDeviceList.length>0">
            <view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">已配对设备
            </view>
            <view class="list">
                <view class="cell" style="background-color: azure;" v-for="(item,index) in bondDeviceList"
                    :key="'bondDevice'+index" @click.stop="connectBluetooth(item.name,item.address)">
                    <text style="color:blueviolet;">名称:{{item.name}}</text>
                    <text style="color: blue;">地址:{{item.address}}</text>
                    <view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
                </view>
            </view>
        </view>
        <view style="width: 750rpx;display: flex;flex-direction: column;" v-if="discoveryList.length>0">
            <view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">周边设备
            </view>
            <view class="list">
                <view class="cell" style="background-color: azure;" v-for="(item,index) in discoveryList"
                    :key="'bondDevice'+index" @click.stop="connectBluetooth(item.name,item.address)">
                    <text style="color:blueviolet;">名称:{{item.name}}</text>
                    <text style="color: blue;">地址:{{item.address}}</text>
                    <view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    import {
        BluetoothInitConfig,
        BluetoothListener,
        initBluetooth,
        getBondList,
        discoveryBluetooth,
        connectBluetoothWithName,
        ConnectBluetoothOptions,
        disconnectBluetooth,
        GetBoundDeviceOptions,
        BluetoothDeviceInfo,
        SendDataOptions,
        sendData,
        SendDataResult,
        ApiFail
    } from '@/uni_modules/yt-uts-bluetooth'
    export default {
        data() {
            return {
                bondDeviceList: [], //已绑定蓝牙列表
                blueToothValue: '', //蓝牙返回的数据
                discoveryList: [] //搜索周边蓝牙设备
            }
        },
        onLoad() {
            this.initBluetoothAction()
        },
        methods: {
            //初始化
            initBluetoothAction() {
                initBluetooth({
                    MTU: 1021,
                    readDataInterval: 200,
                    bluetoothListener: (res) => {
                        switch (res.status) {
                            case 1: //读取蓝牙数据
                                this.blueToothValue = res.value;
                                break;
                            case 2: //连接成功
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 3: //蓝牙已断开
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 5: //扫描到设备回调
                                this.discoveryList.push(res.device)
                                break;
                            case 9: //设备(手机)关闭蓝牙---设置里关闭蓝牙
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 10: //设备(手机)关闭蓝牙---设置里开启蓝牙
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 11: //配对请求中,向蓝牙发送请求配对
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 12: //配对中
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                            case 13: //配对失败
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes
                                })
                                break;
                        }
                    }
                })
            },
            //获取已绑定设备
            boundDevices() {
                getBondList({
                    success: (res) => {
                        console.log(res)
                        this.bondDeviceList = res
                    },
                    fail: (error) => {

                    }
                })
            },
            //连接蓝牙
            connectBluetooth(deviceName, address) {
                connectBluetoothWithName({
                    bluetoothName: deviceName,
                    address: address
                })
            },
            //获取周边设备
            scanDevices() {
                discoveryBluetooth()
            },
            //发送数据
            sendDataAction() {
                sendData({
                    value: 'hello word',
                    callBack: (res) => {
                        uni.showToast({
                            icon: res.errCode == 200 ? 'success' : 'error',
                            title: res.mes
                        })
                    }
                })
            },
            //断开连接
            closeBluetooth() {
                disconnectBluetooth()
            }
        }
    }
</script>

<style>
    .content {
        width: 750rpx;
        display: flex;
        height: 100%;
        flex-direction: column;
    }

    .header {
        width: 750rpx;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 0 10rpx;
        box-sizing: border-box;
    }

    .btn {
        display: flex;
        height: 80rpx;
        flex: 1;
        align-items: center;
        justify-content: center;
        background-color: cadetblue;
        color: white;
        margin-right: 10rpx;
        font-size: 28rpx;
    }

    .btn:nth-last-of-type(1) {
        margin-right: 0;
    }

    .list {
        width: 750rpx;
        display: flex;
        flex-direction: column;
    }

    .cell {
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 10rpx;
        box-sizing: border-box;
        background-color: antiquewhite;
    }
</style>
  • uni-app x
<template>
    <view class="content">

        <view style="font-size: 28rpx;color: cadetblue;height: 80rpx;background-color: azure;">{{blueToothValue}}</view>

        <view class="header">
            <view class="btn" @click="boundDevices()">获取绑定设备</view>
            <view class="btn" @click="scanDevices()">扫描蓝牙</view>
            <view class="btn" @click="closeBluetooth()">断开蓝牙</view>
            <view class="btn" @click="sendDataAction()">发送数据</view>
        </view>

        <view style="width: 750rpx;display: flex;flex-direction: column;" v-if="bondDeviceList.length>0">
            <view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">已配对设备
            </view>
            <view class="list">
                <view class="cell" style="background-color: azure;" v-for="(item,index) in bondDeviceList"
                    :key="'bondDevice'+index" @click.stop="connectBluetooth(item.name,item.address)">
                    <text style="color:blueviolet;">名称:{{item.name}}</text>
                    <text style="color: blue;">地址:{{item.address}}</text>
                    <view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
                </view>
            </view>
        </view>
        <view style="width: 750rpx;display: flex;flex-direction: column;" v-if="discoveryList.length>0">
            <view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">周边设备
            </view>
            <view class="list">
                <view class="cell" style="background-color: azure;" v-for="(item,index) in discoveryList"
                    :key="'bondDevice'+index" @click.stop="connectBluetooth(item.name,item.address)">
                    <text style="color:blueviolet;">名称:{{item.name}}</text>
                    <text style="color: blue;">地址:{{item.address}}</text>
                    <view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    import {
        BluetoothInitConfig,
        BluetoothListener,
        initBluetooth,
        getBondList,
        discoveryBluetooth,
        connectBluetoothWithName,
        ConnectBluetoothOptions,
        disconnectBluetooth,
        GetBoundDeviceOptions,
        BluetoothDeviceInfo,
        SendDataOptions,
        sendData,
        SendDataResult,
        ApiFail

    } from '@/uni_modules/yt-uts-bluetooth'
    export default {
        data() {
            return {//as BluetoothDeviceInfo[]
                bondDeviceList: [] as BluetoothDeviceInfo[], //已绑定蓝牙列表
                blueToothValue: '', //蓝牙返回的数据
                discoveryList: [] as BluetoothDeviceInfo[]//搜索周边蓝牙设备
            }
        },
        onLoad() {
            this.initBluetoothAction()
        },
        methods: {
            initBluetoothAction() {
                initBluetooth({
                    MTU: 1021,
                    readDataInterval: 200,
                    bluetoothListener: (res) => {
                        switch (res.status) {
                            case 1: //读取蓝牙数据
                                this.blueToothValue = res.value ?? '';
                                break;
                            case 2: //连接成功
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 3: //蓝牙已断开
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 5: //扫描到设备回调
                                this.discoveryList.push(res.device!)
                                break;
                            case 9: //设备(手机)关闭蓝牙---设置里关闭蓝牙
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 10: //设备(手机)关闭蓝牙---设置里开启蓝牙
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 11: //配对请求中,向蓝牙发送请求配对
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 12: //配对中
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                            case 13: //配对失败
                                uni.showToast({
                                    icon: 'none',
                                    title: res.mes ?? ''
                                })
                                break;
                        }
                    }
                } as BluetoothInitConfig)
            },
            boundDevices() {
                getBondList({
                    success: (res : BluetoothDeviceInfo[]) => {
                        console.log(res)
                        this.bondDeviceList = res
                    },
                    fail: (error : ApiFail) => {

                    }
                } as GetBoundDeviceOptions)
            },
            scanDevices() {
                discoveryBluetooth()
            },
            closeBluetooth() {
                disconnectBluetooth()
            },
            sendDataAction() {
                sendData({
                    value: 'hello world',
                    callBack: (res : SendDataResult) => {
                        uni.showToast({
                            icon: res.errCode == 200 ? 'success' : 'error',
                            title: res.mes
                        })
                    }
                } as SendDataOptions)
            },
            connectBluetooth(deviceName : string, address : string) {
                connectBluetoothWithName({
                    bluetoothName: deviceName,
                    address: address
                } as ConnectBluetoothOptions)
            },
        }
    }
</script>

<style>
    .content {
        width: 750rpx;
        display: flex;
        height: 100%;
        flex-direction: column;
    }

    .header {
        width: 750rpx;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 0 10rpx;
        box-sizing: border-box;
    }

    .btn {
        display: flex;
        height: 80rpx;
        flex: 1;
        align-items: center;
        justify-content: center;
        background-color: cadetblue;
        color: white;
        margin-right: 10rpx;
        font-size: 28rpx;
    }

    .list {
        width: 750rpx;
        display: flex;
        flex-direction: column;
    }

    .cell {
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 10rpx;
        box-sizing: border-box;
        background-color: antiquewhite;
    }
</style>

隐私、权限声明

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!--Android 12以后--> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!-- 手机当外围向外广播 --> <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

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

插件不采集任何数据

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

暂无用户评论。

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