更新记录
1.0.1(2023-05-29)
Android动态权限失败时返回具体信息
1.0.0(2023-05-16)
低功耗蓝牙扫描
平台兼容性
Android | Android CPU类型 | iOS |
---|---|---|
适用版本区间:5.0 - 12.0 | armeabi-v7a:支持,arm64-v8a:支持,x86:未测试 | 适用版本区间:9 - 16 |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
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原生插件配置”->”云端插件“列表中删除该插件重新选择
一、重要的事说三遍 重要的事说三遍 重要的事说三遍
已适配Android12
和 Android13
有问题咨询QQ
群 - 838332280
支持离线版和源码版的购买 需要加群交流
二、权限申请
Android
"android.permission.BLUETOOTH",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.BLUETOOTH_SCAN",
"android.permission.BLUETOOTH_ADMIN",
"android.permission.BLUETOOTH_CONNECT"
IOS
"NSBluetoothPeripheralUsageDescription",
"NSLocationWhenInUseUsageDescription",
"NSBluetoothPeripheralUsageDescription",
"NSBluetoothAlwaysUsageDescription"
三、类型说明
export interface UUIDs {
service_uuids: string[]; //服务uuids
device_uuids: string[]; //设备uuids
}
interface BleDeviceInfo {
identifier: string; //外设的唯一标识符
name: string; //外设的名称
state: BleState; //外设当前的连接状态
rssi: number;//外设的接收信号强度
services: string[];//外设所提供的所有服务
}
/**
* 连接状态
*/
enum BleState {
Disconnected = 0, //已断开
Connecting = 1, //正在连接
Connected = 2, //已连接
Disconnecting = 3,//正在断开
}
/**
* code:
* 200 - 操作成功 - 返回device_uuid设备
* 201 - 操作成功 - 返回service_uuid设备
*
* 500 - 用户拒绝使用位置权限
* 501 - 用户拒绝使用蓝牙
* 502 - 传入参数错误
* 503 - 正在扫描中,想扫描请先停止之后在执行
* 504 - 停止扫描成功
*/
export interface BleResult {
code: number;
msg: string;
data: any;
}
四、API
1、开始扫描蓝牙
接口
void startScanning(uuids: UUIDs, callback: Function)
2、停止扫描蓝牙
接口
void stopScanning(callback: Function)
五、使用案例
<template>
<view class="content">
<view class="item">
<button type="primary" @click="startScanning">开始扫描</button>
<view style="width: 10px;"></view>
<button type="primary" @click="stopScanning">停止扫描</button>
</view>
<view>----------------device uuids-------------------</view>
<ul>
<li v-for="(device, index) in state1.bleDevices" :key="index">
{{ device.data.name }} - {{ device.data.identifier }}
</li>
</ul>
<view>----------------service uuids-------------------</view>
<ul>
<li v-for="(device, index) in state.bleDevices" :key="index">
{{ device.data.name }} - {{ device.data.services[0] }}
</li>
</ul>
<view class="textbox">
<text>{{result}}</text>
</view>
</view>
</template>
<script setup lang="ts">
import {
reactive, ref
} from "vue";
import { BleResult, UUIDs } from "./ble_config";
var bleModule = uni.requireNativePlugin("RLUni-EasyBleBluetoothModule");
var result = ref('');
const state = reactive({
bleDevices: [],
});
const addDevice = (newBleResult) => {
state.bleDevices.push(newBleResult);
};
const clearDevices = () => {
state.bleDevices = [];
};
const state1 = reactive({
bleDevices: [],
});
const addDevice1 = (newBleResult) => {
state1.bleDevices.push(newBleResult);
};
const clearDevices1 = () => {
state1.bleDevices = [];
};
//IOS
let uuids_ios : UUIDs = {
device_uuids: [
'47FBEC8F-7D21-B31E-1024-4A3A7F0A9F67',
'BB9BAF65-EE9C-6A4A-2471-0BB23BE242B9',
],
service_uuids: ['FCE7', 'FEE0'],
}
//Android
let uuids_android : UUIDs = {
device_uuids: [
'14:08:0D:00:60:54',
'ED:DC:EA:44:CB:8B',
'84:E0:F4:2C:EB:4F'
],
service_uuids: [
'0000fee0-0000-1000-8000-00805f9b34fb',
'0000fce7-0000-1000-8000-00805f9b34fb',
'0000fee0-0000-1000-8000-00805f9b34fb',
],
}
let uuids = uni.getSystemInfoSync().platform == 'ios' ? uuids_ios : uuids_android;
function startScanning() {
clearDevices();
clearDevices1();
result.value = "";
bleModule.startScanning(uuids, callback);
}
function stopScanning() {
clearDevices();
clearDevices1();
bleModule.stopScanning();
}
function callback(data : BleResult) {
if (data.code == 200) {//device uuids - state1
addDevice1(data);
} else if (data.code == 201) {//service uuids - state
addDevice(data);
} else {
result.value = JSON.stringify(data);
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.item {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.textbox {
width: 100%;
word-wrap: break-word;
}
</style>