实现搜索蓝牙功能,ios系统的微信小程序初始化蓝牙失败是为什么,安卓系统没有出现该问题,该如何实现苹果手机在微信小程序中初始化蓝牙,并搜索蓝牙的功能家人们!

9***@qq.com
- 发布:2025-07-04 21:10
- 更新:2025-07-04 22:44
- 阅读:61

9***@qq.com (作者)
您好,下面是我使用蓝牙模块的代码,点击按钮触发checkAndRequestPermissions()事件,后续一系列逻辑
// 检验是否有蓝牙权限
const checkAndRequestPermissions = () => {
const permissions = ['scope.bluetooth']
// 检查权限
uni.getSetting({
success: res => {
let authResult = true
// console.log('获取当前权限', res);
permissions.forEach(permission => {
if (!res.authSetting[permission]) {
authResult = false
return
}
})
if (!authResult) {
// 没有权限,请求权限
// console.log('无蓝牙权限,申请授权');
requestPermissions()
} else {
// 已经有权限,可以进行蓝牙操作
initBluetooth()
}
}
})
}
// 申请蓝牙权限
const requestPermissions = () => {
uni.authorize({
scope: 'scope.bluetooth',
success: () => {
// console.log(`授权成功`)
// 已经有权限,可以进行蓝牙操作
initBluetooth()
},
fail: err => {
// console.log(`授权失败`, err)
uni.showModal({
title: '提示',
content: '请在设置中开启蓝牙权限',
cancelText: '取消',
confirmText: '去设置',
success: function(res) {
if (res.confirm) {
uni.openSetting({
success(res) {
// console.log(res.authSetting)
// uni.navigateBack()
// checkAndRequestPermissions()
}
});
} else if (res.cancel) {
uni.showToast({
title: '请开启蓝牙后再关锁',
icon: 'error'
})
}
}
})
}
})
}
const initBluetooth = () => {
uni.openBluetoothAdapter({
success: () => {
uni.showLoading({
title: '位置校验中...',
mask: true
})
// console.log('蓝牙初始化成功');
getBluetoothState();
},
fail: (err) => {
// console.log('初始化蓝牙错误', err);
uni.showToast({
title: err.errMsg,
icon: 'none',
duration: 2000
})
//监听蓝牙适配器是否打开,若打开则改变状态
// uni.onBluetoothAdapterStateChange(function(res) {
// if (res.available) {
// console.log('监听蓝牙适配器状态-已就绪');
// } else {
// console.log('监听蓝牙适配器状态-蓝牙不可用');
// stopSearch()
// }
// });
},
complete: () => {
}
});
};
// 获取蓝牙状态
const getBluetoothState = () => {
uni.getBluetoothAdapterState({
success: (res) => {
if (!res.available) {
uni.showToast({
title: '蓝牙不可用',
icon: 'none'
})
return
}
// bluetoothStatus.value = '初始化完成-就绪';
// console.log('获取蓝牙状态-蓝牙初始化完成-已就绪');
startSearch()
}
});
};
// 定义目标设备信息
const targetDevice = reactive({
name: '',
deviceIds: []
});
const deviceId = ref('') // 蓝牙设备id
let searchTimer = null; // 超时计时器
// 搜索设备
const startSearch = () => {
// console.log('开始搜索附近蓝牙设备');
isScanning = true
// deviceList.value = [];
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: () => {
// 监听设备发现事件
uni.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
// 检查是否为目标设备
if (device.name === targetDevice.name &&
targetDevice.deviceIds.includes(device.deviceId)) {
// console.log('发现目标设备', device.deviceId);
deviceId.value = device.deviceId
returnOK.value = 1
uni.hideLoading()
close() // 关闭1弹框
openRet() // 打开2弹框
return;
}
// 原有的设备去重逻辑
// if (!deviceList.value.some(d => d.deviceId === device.deviceId)) {
// deviceList.value.push(device);
// }
});
});
searchTimer = setTimeout(() => {
returnOK.value = 0
uni.hideLoading()
close() // 关闭1弹框
openRet() // 打开2弹框
}, 10000)
},
fail: (err) => {
uni.showToast({
title: '位置校验失败,请打开蓝牙后重试',
icon: 'none'
})
stopSearch();
}
});
};
// 停止搜索
const stopSearch = () => {
clearTimeout(searchTimer); // 清除超时计时器
if (isScanning) {
uni.stopBluetoothDevicesDiscovery();
// console.log('结束后搜索到的设备', deviceList.value);
// uni.closeBluetoothAdapter({
// success(res) {
// console.log(res)
// }
// })
isScanning = false
}
};
// 组件卸载时清理
onUnmounted(() => {
stopSearch(); // 停止搜索
// uni.closeBluetoothAdapter(); // 关闭蓝牙适配器
});
9***@qq.com (作者)
您好,下面是我使用蓝牙模块的代码,点击按钮触发checkAndRequestPermissions()事件,后续一系列逻辑
// 检验是否有蓝牙权限
const checkAndRequestPermissions = () => {
const permissions = ['scope.bluetooth']
// 检查权限
uni.getSetting({
success: res => {
let authResult = true
// console.log('获取当前权限', res);
permissions.forEach(permission => {
if (!res.authSetting[permission]) {
authResult = false
return
}
})
if (!authResult) {
// 没有权限,请求权限
// console.log('无蓝牙权限,申请授权');
requestPermissions()
} else {
// 已经有权限,可以进行蓝牙操作
initBluetooth()
}
}
})
}
// 申请蓝牙权限
const requestPermissions = () => {
uni.authorize({
scope: 'scope.bluetooth',
success: () => {
// console.log(
授权成功
)// 已经有权限,可以进行蓝牙操作
initBluetooth()
},
fail: err => {
// console.log(
授权失败
, err)uni.showModal({
title: '提示',
content: '请在设置中开启蓝牙权限',
cancelText: '取消',
confirmText: '去设置',
success: function(res) {
if (res.confirm) {
uni.openSetting({
success(res) {
// console.log(res.authSetting)
// uni.navigateBack()
// checkAndRequestPermissions()
}
});
} else if (res.cancel) {
uni.showToast({
title: '请开启蓝牙后再关锁',
icon: 'error'
})
}
}
})
}
})
}
const initBluetooth = () => {
});
2025-07-04 22:45