新闻详情

新闻详情

首页 / 资讯中心 / 详情

uniapp开发蓝牙搜索startBluetoothDevicesDiscovery:fail Location services are turned off

发布时间:2026/6/6 20:28:54
uniapp开发蓝牙搜索startBluetoothDevicesDiscovery:fail Location services are turned off
问题描述在使用uniapp开发安卓应用程序时使用uni.startBluetoothDevicesDiscovery搜索蓝牙时在部分设备上出现fail Location services are turned off报错特别是无GPS硬件支持的设备已确认开启位置信息且给了权限解决方法使用安卓原生蓝牙扫描广播接收实现借助AI具体示例代码如下template view classcontainer view classstatus状态: {{ isScanning ? 正在扫描... : 停止扫描 }}/view button typeprimary clickstartScan :disabledisScanning开始扫描/button button typewarn clickstopScan :disabled!isScanning停止扫描/button view classlist-title已发现设备:/view scroll-view scroll-ytrue classdevice-list view v-for(item, index) in deviceList :keyindex classdevice-item text classname{{ item.name || 未知设备 }}/text text classaddress{{ item.address }}/text text classrssi信号: {{ item.rssi }} dBm/text /view /scroll-view /view /template script // 在这里声明变量使其在整个组件内可用 let _BluetoothAdapter; let _BluetoothDevice; let _IntentFilter; let _mainActivity; export default { data() { return { isScanning: false, deviceList: [], receiver: null, nativeAdapter: null }; }, onLoad() { // 确保在 plus 环境下初始化 // #ifdef APP-PLUS this.initNativeClasses(); // #endif }, onUnload() { this.stopScan(); this.unregisterReceiver(); }, methods: { // 1. 导入原生类并获取适配器 initNativeClasses() { try { // 导入原生类并赋值给外部变量 _BluetoothAdapter plus.android.importClass(android.bluetooth.BluetoothAdapter); _BluetoothDevice plus.android.importClass(android.bluetooth.BluetoothDevice); _IntentFilter plus.android.importClass(android.content.IntentFilter); _mainActivity plus.android.runtimeMainActivity(); this.nativeAdapter _BluetoothAdapter.getDefaultAdapter(); if (!this.nativeAdapter) { uni.showToast({ title: 该设备不支持蓝牙, icon: none }); } } catch (e) { console.error(初始化原生类失败, e); } }, // 2. 注册广播接收器 registerReceiver() { if (this.receiver) return; const self this; // 必须使用 plus.android.implements 实现 BroadcastReceiver 接口 this.receiver plus.android.implements(io.dcloud.feature.internal.reflect.BroadcastReceiver, { onReceive: function(context, intent) { plus.android.importClass(intent); const action intent.getAction(); if (action _BluetoothDevice.ACTION_FOUND) { // 查找到设备 const device intent.getParcelableExtra(_BluetoothDevice.EXTRA_DEVICE); const rssi intent.getShortExtra(_BluetoothDevice.EXTRA_RSSI, -100); // 使用 plus.android.invoke 调用原生方法获取属性 const name plus.android.invoke(device, getName); const address plus.android.invoke(device, getAddress); const index self.deviceList.findIndex(d d.address address); if (index -1) { self.deviceList.push({ name, address, rssi }); } else { self.deviceList[index].rssi rssi; } } else if (action _BluetoothAdapter.ACTION_DISCOVERY_FINISHED) { self.isScanning false; uni.hideLoading(); } } }); const filter new _IntentFilter(); filter.addAction(_BluetoothDevice.ACTION_FOUND); filter.addAction(_BluetoothAdapter.ACTION_DISCOVERY_FINISHED); _mainActivity.registerReceiver(this.receiver, filter); }, // 3. 开始扫描 async startScan() { if (!this.nativeAdapter) return; // 权限检查 const hasPermission await this.checkPermission(); if (!hasPermission) return; this.deviceList []; this.registerReceiver(); // 如果正在扫描先停止 if (this.nativeAdapter.isDiscovering()) { this.nativeAdapter.cancelDiscovery(); } // 启动原生扫描 const success this.nativeAdapter.startDiscovery(); if (success) { this.isScanning true; uni.showLoading({ title: 原生扫描中... }); } else { uni.showToast({ title: 开启扫描失败, icon: none }); } }, // 4. 停止扫描 stopScan() { if (this.nativeAdapter this.nativeAdapter.isDiscovering()) { this.nativeAdapter.cancelDiscovery(); } this.isScanning false; uni.hideLoading(); }, unregisterReceiver() { if (this.receiver) { _mainActivity.unregisterReceiver(this.receiver); this.receiver null; } }, // Android 高版本定位权限申请 checkPermission() { return new Promise((resolve) { plus.android.requestPermissions( [android.permission.ACCESS_FINE_LOCATION], (result) { if (result.granted.length 0) resolve(true); else resolve(false); }, (err) resolve(false) ); }); } } }; /script style scoped .container { padding: 20px; } .status { color: #007AFF; margin-bottom: 10px; font-size: 14px; } .list-title { margin-top: 20px; font-weight: bold; border-bottom: 1px solid #eee; padding-bottom: 5px; } .device-list { height: 400px; } .device-item { padding: 10px 0; border-bottom: 1px solid #f9f9f9; } .name { display: block; font-size: 16px; } .address { font-size: 12px; color: #888; } .rssi { font-size: 12px; color: #007AFF; margin-left: 10px; } button { margin-top: 10px; } /style
网站建设 高端定制 企业官网