
- 发布:2024-01-09 15:15
- 更新:2025-08-28 10:17
- 阅读:552
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win11
HBuilderX类型: 正式
HBuilderX版本号: 3.99
手机系统: 全部
手机厂商: 华为
页面类型: vue
vue版本: vue3
打包方式: 离线
项目创建方式: HBuilderX
测试过的手机:
示例代码:
/**
- 判断初始化蓝牙状态
*/
function initTypes(code, errMsg) {
switch (code) {
case 10000:
toast('未初始化蓝牙适配器');
break;
case 10001:
toast('未检测到蓝牙,请打开蓝牙重试!');
break;
case 10002:
toast('没有找到指定设备');
break;
case 10003:
toast('连接失败');
break;
case 10004:
toast('没有找到指定服务');
break;
case 10005:
toast('没有找到指定特征值');
break;
case 10006:
toast('当前连接已断开');
break;
case 10007:
toast('当前特征值不支持此操作');
break;
case 10008:
toast('其余所有系统上报的异常');
break;
case 10009:
toast('Android 系统特有,系统版本低于 4.3 不支持 BLE');
break;
default:
toast(errMsg);
}
}
/**
- 弹出框封装
*/
function toast(content, showCancel = false) {
uni.showModal({
title: '提示',
content,
showCancel
});
}
/**
- 初始化蓝牙适配器
*/
export function initAdapter() {
uni.closeBluetoothAdapter({
success(res) {
console.log(res);
}
});
openBluetoothAdapter();
}
/**
- 初始化蓝牙设备
*/
function openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
console.log(JSON.stringify(e));
//获取蓝牙适配器状态成功后 回调进行蓝牙周围设备扫描
getBluetoothAdapterState(() => {
startBluetoothDevicesDiscovery(
); // 在 getBluetoothAdapterState 方法的回调函数中调用 startBluetoothDevicesDiscovery 方法
});
},
fail: e => {
console.log(e);
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
if (e.errCode !== 0) {
initTypes(e.errCode, e.errMsg);
}
}
});
}
/**
- 停止搜索蓝牙设备
*/
export function stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log('停止搜索蓝牙设备:' + e.errMsg);
},
fail: e => {
console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/**
- 开始搜索蓝牙设备
*/
export function startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
services: ['00005a30-3c17-d293-8e48-14fe2e4da212'],
allowDuplicatesKey: true,
success: e => {
console.log('开始搜索蓝牙设备:' + e.errMsg);
onBluetoothDeviceFound();
},
fail: e => {
console.log('搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/**
- 获取本机蓝牙适配器状态
*/
function getBluetoothAdapterState(callback) {
console.log('--->');
uni.getBluetoothAdapterState({
success: res => {
console.log('获取本机蓝牙适配器状态成功:' + JSON.stringify(res));
if (typeof callback === 'function') {
callback(); // 在回调函数中调用 startBluetoothDevicesDiscovery 方法
}
},
fail: e => {
console.log('获取本机蓝牙适配器状态失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/**
-
发现外围设备
*/
function onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(devices => {
console.log('搜索到的设备信息:'+JSON.stringify(devices));
//getBluetoothDevices();
});
}
/**
-
获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/
function getBluetoothDevices() {
uni.getBluetoothDevices({
success: res => {
res.devices.forEach(device => {
if (!device.connected) {
getApp().globalData.bleDevicesInfo[device.deviceId]= {
name: device.name,
mac: device.deviceId,
signal: device.RSSI,
connectStatus: false
}
}
});
},
fail: e => {
console.log('获取蓝牙设备错误,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/**
-
连接低功耗蓝牙
*/
export function createBLEConnection(device,callback) {
const deviceId = device.mac;
uni.showToast({
title: '连接蓝牙...',
icon: 'loading',
duration: 99999
});
uni.createBLEConnection({
deviceId,
success: res => {
console.log(res);
console.log('连接蓝牙成功:' + res.errMsg);
//停止搜索
stopBluetoothDevicesDiscovery();
uni.hideToast();
uni.showToast({
title: '连接成功',
icon: 'success',
duration: 500
});
getApp().globalData.bleDevicesInfo[deviceId].connectStatus= true;
callback(null, device); // 调用回调函数,传递null表示没有错误
},
fail: e => {
console.log('连接低功耗蓝牙失败,错误码:' + JSON.stringify(e));
if (e.code == -1) {
toast("该设备已连接");
} else if (e.code !== 0) {
initTypes(e.errCode);
}
uni.hideToast();
callback(e); // 调用回调函数,传递错误对象
}
});
}
/**
-
断开蓝牙
*/
export function closeBLEConnection(deviceId) {
uni.closeBLEConnection({
deviceId,
success: res => {
console.log(res);
console.log('断开低功耗蓝牙成功:' + res.errMsg);
getApp().globalData.bleDevicesInfo[deviceId].connectStatus= true;
},
fail: e => {
console.log('断开低功耗蓝牙成功,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/**
- 监听连接的蓝牙设备状态
*/
export function onBLEConnectionStateChange(){
uni.onBLEConnectionStateChange(function (res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(device ${res.deviceId} state has changed, connected: ${res.connected}
)
getApp().globalData.bleDevicesInfo[res.deviceId].connectStatus= res.connected;
})
}
/**
-
读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用
*/
export function readBLECharacteristicValue(deviceId,serviceId,characteristicId) {
console.log(deviceId);
console.log(serviceId);
console.log(characteristicId);
uni.readBLECharacteristicValue({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success: res => {
console.log('读取设备数据值成功');
console.log(JSON.stringify(res));
this.notifyBLECharacteristicValueChange();
},
fail(e) {
console.log('读取设备数据值失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
this.onBLECharacteristicValueChange();
}
/**
-
订阅特征值变化
*/
export function notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId) {
console.log(deviceId);
console.log(serviceId);
console.log(characteristicId);
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success(res) {
console.log('notifyBLECharacteristicValueChange success:' + res.errMsg);
console.log(JSON.stringify(res));
}
});
}
/**
-
监听特征值的特征值数据变化
/
/ export function onBLECharacteristicValueChange(){
onBLECharacteristicValueChange() {
// 必须在这里的回调才能获取
uni.onBLECharacteristicValueChange(characteristic => {
console.log('监听低功耗蓝牙设备的特征值变化事件成功');
console.log(JSON.stringify(characteristic));
this.valueChangeData = characteristic;
});
}
} */
/**
- 发送数据到蓝牙设备
*/
/**
- 判断初始化蓝牙状态
*/
function initTypes(code, errMsg) {
switch (code) {
case 10000:
toast('未初始化蓝牙适配器');
break;
case 10001:
toast('未检测到蓝牙,请打开蓝牙重试!');
break;
case 10002:
toast('没有找到指定设备');
break;
case 10003:
toast('连接失败');
break;
case 10004:
toast('没有找到指定服务');
break;
case 10005:
toast('没有找到指定特征值');
break;
case 10006:
toast('当前连接已断开');
break;
case 10007:
toast('当前特征值不支持此操作');
break;
case 10008:
toast('其余所有系统上报的异常');
break;
case 10009:
toast('Android 系统特有,系统版本低于 4.3 不支持 BLE');
break;
default:
toast(errMsg);
}
}
/** - 弹出框封装
*/
function toast(content, showCancel = false) {
uni.showModal({
title: '提示',
content,
showCancel
});
}
/** - 初始化蓝牙适配器
*/
export function initAdapter() {
uni.closeBluetoothAdapter({
success(res) {
console.log(res);
}
});
openBluetoothAdapter();
}
/** - 初始化蓝牙设备
*/
function openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
console.log(JSON.stringify(e));
//获取蓝牙适配器状态成功后 回调进行蓝牙周围设备扫描
getBluetoothAdapterState(() => {
startBluetoothDevicesDiscovery(
); // 在 getBluetoothAdapterState 方法的回调函数中调用 startBluetoothDevicesDiscovery 方法
});
},
fail: e => {
console.log(e);
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
if (e.errCode !== 0) {
initTypes(e.errCode, e.errMsg);
}
}
});
}
/** - 停止搜索蓝牙设备
*/
export function stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log('停止搜索蓝牙设备:' + e.errMsg);
},
fail: e => {
console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/** - 开始搜索蓝牙设备
*/
export function startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
services: ['00005a30-3c17-d293-8e48-14fe2e4da212'],
allowDuplicatesKey: true,
success: e => {
console.log('开始搜索蓝牙设备:' + e.errMsg);
onBluetoothDeviceFound();
},
fail: e => {
console.log('搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/** - 获取本机蓝牙适配器状态
*/
function getBluetoothAdapterState(callback) {
console.log('--->');
uni.getBluetoothAdapterState({
success: res => {
console.log('获取本机蓝牙适配器状态成功:' + JSON.stringify(res));
if (typeof callback === 'function') {
callback(); // 在回调函数中调用 startBluetoothDevicesDiscovery 方法
}
},
fail: e => {
console.log('获取本机蓝牙适配器状态失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
}
/** -
发现外围设备
*/
function onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(devices => {console.log('搜索到的设备信息:'+JSON.stringify(devices)); //getBluetoothDevices();
});
}
/** -
获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/
function getBluetoothDevices() {
uni.getBluetoothDevices({
success: res => {res.devices.forEach(device => { if (!device.connected) { getApp().globalData.bleDevicesInfo[device.deviceId]= { name: device.name, mac: device.deviceId, signal: device.RSSI, connectStatus: false } } }); }, fail: e => { console.log('获取蓝牙设备错误,错误码:' + e.errCode); if (e.errCode !== 0) { initTypes(e.errCode); } }
});
}
/** -
连接低功耗蓝牙
*/
export function createBLEConnection(device,callback) {const deviceId = device.mac;
uni.showToast({
title: '连接蓝牙...',
icon: 'loading',
duration: 99999
});
uni.createBLEConnection({
deviceId,
success: res => {
console.log(res);
console.log('连接蓝牙成功:' + res.errMsg);
//停止搜索
stopBluetoothDevicesDiscovery();
uni.hideToast();
uni.showToast({
title: '连接成功',
icon: 'success',
duration: 500
});
getApp().globalData.bleDevicesInfo[deviceId].connectStatus= true;
callback(null, device); // 调用回调函数,传递null表示没有错误
},
fail: e => {
console.log('连接低功耗蓝牙失败,错误码:' + JSON.stringify(e));
if (e.code == -1) {
toast("该设备已连接");
} else if (e.code !== 0) {
initTypes(e.errCode);
}
uni.hideToast();
callback(e); // 调用回调函数,传递错误对象
}
});
}
/**
-
断开蓝牙
*/
export function closeBLEConnection(deviceId) {uni.closeBLEConnection({ deviceId, success: res => { console.log(res); console.log('断开低功耗蓝牙成功:' + res.errMsg); getApp().globalData.bleDevicesInfo[deviceId].connectStatus= true; }, fail: e => { console.log('断开低功耗蓝牙成功,错误码:' + e.errCode); if (e.errCode !== 0) { initTypes(e.errCode); } } });
}
/**
- 监听连接的蓝牙设备状态
*/
export function onBLEConnectionStateChange(){
uni.onBLEConnectionStateChange(function (res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(device ${res.deviceId} state has changed, connected: ${res.connected}
)
getApp().globalData.bleDevicesInfo[res.deviceId].connectStatus= res.connected;
})
}
/**
-
读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用
*/
export function readBLECharacteristicValue(deviceId,serviceId,characteristicId) {console.log(deviceId); console.log(serviceId); console.log(characteristicId); uni.readBLECharacteristicValue({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId, success: res => { console.log('读取设备数据值成功'); console.log(JSON.stringify(res)); this.notifyBLECharacteristicValueChange(); }, fail(e) { console.log('读取设备数据值失败,错误码:' + e.errCode); if (e.errCode !== 0) { initTypes(e.errCode); } } }); this.onBLECharacteristicValueChange(); }
/**
-
订阅特征值变化
*/
export function notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId) {console.log(deviceId); console.log(serviceId); console.log(characteristicId); uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId, success(res) { console.log('notifyBLECharacteristicValueChange success:' + res.errMsg); console.log(JSON.stringify(res)); } }); }
/**
-
监听特征值的特征值数据变化
/
/ export function onBLECharacteristicValueChange(){onBLECharacteristicValueChange() { // 必须在这里的回调才能获取 uni.onBLECharacteristicValueChange(characteristic => { console.log('监听低功耗蓝牙设备的特征值变化事件成功'); console.log(JSON.stringify(characteristic)); this.valueChangeData = characteristic; }); }
} */
/**
- 发送数据到蓝牙设备
*/
操作步骤:
打开蓝牙搜索界面 调用api 搜索附近设备
打开蓝牙搜索界面 调用api 搜索附近设备
预期结果:
能获取 到广播中的serviceData 自定义数据 读取 厂商自定义的mac地址
能获取 到广播中的serviceData 自定义数据 读取 厂商自定义的mac地址
实际结果:
serviceData 数据为null ,function onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(devices => {
console.log('搜索到的设备信息:'+JSON.stringify(devices));
//getBluetoothDevices();
});
} 中打印 数据null 多次搜索 值全部为null
serviceData 数据为null ,function onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(devices => {
console.log('搜索到的设备信息:'+JSON.stringify(devices));
//getBluetoothDevices();
});
} 中打印 数据null 多次搜索 值全部为null
x***@163.com
我这蓝牙搜索可以停止, 但是搜不到那个 自定义的广播内容 也就是 advertisData 里面的东西
2025-08-28 14:52
h***@163.com
回复 x***@163.com: 我这广播内容也是空对象,而且不能停止搜索
2025-09-02 11:02