带有PendingIntent的Oreo BLE扫描仪startScan在三星设备上不起作用

编程入门 行业动态 更新时间:2024-10-12 12:26:07
本文介绍了带有PendingIntent的Oreo BLE扫描仪startScan在三星设备上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 startScan(过滤器,设置,callbackIntent).我有一个适用于Sony Xperia XZ和Nexus 5X的实现.我唯一可用的带有Android O的其他设备是三星Galaxy S8,适用于其他设备的产品在三星上什么都没有. (蓝牙扫描实际上已嵌入到库模块中,但是即使在创建虚拟应用程序时,三星也会失败,因此在本示例中将使用它).我删除了startScan的过滤器和设置,因为如果没有它们,扫描将无法进行,并且这些是可选的.

I am trying to scan for beacons using startScan(filters, settings, callbackIntent). I have an implementation that works fine for Sony Xperia XZ, and Nexus 5X. The only other device with Android O I have available is a Samsung Galaxy S8, and what works for the other devices produce nothing on the Samsung. (The bluetooth scan is really imbedded in a library module, but even when creating a dummy app the samsung fails, so I'll use that in this example). I have removed the filter and the settings used for startScan since the scan doesn't work without them anyway and these are optional.

MainActivity - checks and asks for permissions (ACCESS_COARSE_LOCATION) - simplified onStart override fun onStart() { super.onStart() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val startScan = bleScanner.startScan(null, null, getPendingIntent()) Log.d("testApp", "Start scan! ${startScan == 0}") } }

PendingIntent:

PendingIntent:

private fun getPendingIntent(): PendingIntent { return PendingIntent.getBroadcast( this, REQ_CODE, Intent(this.applicationContext, BleReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) }

清单

权限:

<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

接收器:

<receiver android:name="com.testapp.samsungoscan.BleReceiver" > <intent-filter> <action android:name="BluetoothDevice.ACTION_FOUND" /> <action android:name="BluetoothDevice.EXTRA_UUID" /> <action android:name="BluetoothDevice.EXTRA_RSSI" /> </intent-filter> </receiver>

接收器实现:

class BleReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Log.e("testApp", "On Receive!") } }

所以!为什么它不适用于三星,却适用于索尼和Nexus? 注意:如果我将接收器android:name更改为相对路径.BleReceiver而不是com.testapp.samsungoscan.BleReceiver,则Nexus会停止工作,但索尼仍然可以工作!

So! Why is this not working for Samsung, while it works for Sony and Nexus? Note: If I change the receivers android:name to a relative path .BleReceiver instead of com.testapp.samsungoscan.BleReceiver, then the Nexus stops working, but Sony still works!

通过工作,我的意思是所有类都被使用,并且日志被触发. 怎么了?

By work I mean all classes gets used and the logs are triggered. What is wrong?

推荐答案

您已在AndroidManifest.xml中使用意向过滤器名称注册了广播接收器

You have registered the broadcast receiver in the AndroidManifest.xml with an Intent Filter name

<action android:name="BluetoothDevice.ACTION_FOUND" />

首先,您可以在此处提供任何String.上面的建议您使用的是此常量,但实际上您只是按原样使用字符串(BluetoothDevice.ACTION_FOUND).我建议将其更改为某些com.testapp.samsungoscan.ACTION_FOUND.不需要附加功能,它们暗示它们实际上是附加功能,但它们只是名称为"extra"的另一个动作名称.

First of all, you may provide there any String. Above suggest that you are using this constant, but in fact you are just using the String as is (BluetoothDevice.ACTION_FOUND). I would recommend changing it to some com.testapp.samsungoscan.ACTION_FOUND. The Extras are not needed and they suggest they are in fact extras, but they are just another action names with name "extra".

我建议更改为:

<receiver android:name="com.testapp.samsungoscan.BleReceiver"> <intent-filter> <action android:name="com.testapp.samsungoscan.ACTION_FOUND" /> </intent-filter> </receiver>

第二,您必须使用以下操作名称创建PendingIntent:

Secondly, you have to create the PendingIntent with this action name:

private fun getPendingIntent(): PendingIntent { return PendingIntent.getBroadcast( this, REQ_CODE, Intent(this.applicationContext, BleReceiver::class.java).setAction("com.testapp.samsungoscan.ACTION_FOUND"), PendingIntent.FLAG_UPDATE_CURRENT) }

需要创建一个提供组件名称的意图,因为Oreo能够获得任何广播.但是,我不确定在您的应用被系统杀死之后是否可以正常工作.

Creating an intent with providing Component name is required since Oreo to be able to get any broadcasts. I'm not sure, however, will it work after your app has been killed by the system.

最后,我建议您请求FINE_LOCATION,如下所示: android-review.googlesource/c/platform/packages/apps/Bluetooth/+/848935 将来可能需要更改位置信息才能扫描蓝牙设备.

Lastly, I recommend requesting FINE_LOCATION, as this: android-review.googlesource/c/platform/packages/apps/Bluetooth/+/848935 change may in the future require fine location permission to scan for Bluetooth devices.

更多推荐

带有PendingIntent的Oreo BLE扫描仪startScan在三星设备上不起作用

本文发布于:2023-11-27 13:25:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扫描仪   上不   设备   Oreo   PendingIntent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!