【发布时间】:2022-12-13 09:57:19
【问题描述】:
当我将 Android 更新为目标 SDK 31 时,问题就开始了。
首先,我在清单中有错误,因为库中有一个没有设置 android:exported 的接收器。这原来是 org.altbeacon.android-beacon-library。通过更新到最新的非测试版 2.19.4 修复
接下来,我必须将蓝牙的新权限添加到我的清单中。
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
并且还调整了旧的权限。
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
并添加了功能设置。
<uses-feature android:name="android.hardware.bluetooth"
android:required="false"/>
<uses-feature android:name="android.hardware.bluetooth_le"
android:required="false"/>
在应用程序中,我要求用户提供权限。 (如果 android 发布版本为 12 或更高版本,则使用此版本)。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE)
!= PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
!= PackageManager.PERMISSION_GRANTED) {
FirebaseCrashlytics.getInstance().log("requesting permissions.");
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_ADVERTISE,
}, 22);
}
请注意,我在整个程序中有相当多的 Crashlytics 日志调试消息。
很快一些我在 altbeacon 库中遇到各种崩溃。例子:
Fatal Exception: java.lang.SecurityException: Need android.permission.BLUETOOTH_ADVERTISE permission for android.content.AttributionSource@881430fb: GattService startAdvertisingSet
at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:482)
at com.android.bluetooth.Utils.checkAdvertisePermissionForDataDelivery(Utils.java:570)
at com.android.bluetooth.gatt.GattService.startAdvertisingSet(GattService.java:3252)
at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.startAdvertisingSet(GattService.java:1392)
at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.startAdvertisingSet(GattService.java:1376)
at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:362)
at android.os.Binder.execTransactInternal(Binder.java:1285)
at android.os.Binder.execTransact(Binder.java:1244)
还获得相同一般类型的 BLUETOOTH_SCAN 权限崩溃。
1:所有崩溃都发生在打开应用程序后的 1 到 5 秒内。
2:尽管设置了日志消息,但我在 Crashlytics 中没有收到任何日志。我想知道如果崩溃发生在库中,日志功能是否有效?
3:Crashlytics 控制台显示 99% 的崩溃发生在三星设备上。
4:我可以在安卓12的测试手机上运行这个,也就是不是一个三星,它工作正常。如果我进入应用程序的权限设置并在下次运行时关闭“附近的设备”,它会再次询问我的权限,如果我拒绝它们,它将在没有蓝牙功能的情况下运行。
【问题讨论】:
标签: android bluetooth altbeacon