java.lang.SecurityException:蓝牙权限崩溃。 (仅限三星设备)

java.lang.SecurityException: BLUETOOTH permission crash. (Samsung devices only)

我遇到了以下崩溃,但这只发生在某些三星设备上(不是全部,我们 develop/test 在三星设备上)

崩溃日志:

java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10025 nor current process has android.permission.BLUETOOTH.
   at android.os.Parcel.readException(Parcel.java:1540)
   at android.os.Parcel.readException(Parcel.java:1493)
   at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:1156)
   at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:697)
   at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:245)
   at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:234)
   at org.altbeacon.beacon.service.scanner.CycledLeScanner.run(CycledLeScanner.java:230)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5972)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是我的 AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.myapp" >


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!--
     The following two permissions are not required to use Google Maps Android API v2,
     but are recommended. We also require them for location updates.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<permission
    android:name=".permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name=".permission.C2D_MESSAGE" />

<application
    android:name=".myapp"
    android:allowBackup="true"
    android:icon="@drawable/appicon"
    android:label="@string/app_name"
    android:logo="@drawable/action_bar_pinwheel_back"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".activities.VerifyPinActivity"
        android:label="@string/title_activity_verify_pin"
        android:screenOrientation="portrait"
        android:theme="@style/NoActionBarDark"
        android:windowSoftInputMode="stateVisible|adjustPan" >
    </activity>
    ...(more activities here removed)
</application>
</manifest>

我真的不知道为什么它只出现在少数设备上;我最初的猜测是它与三星 KNOX 有关,崩溃的(少数)用户是 KNOX 用户,他们使用 phone 政策禁用蓝牙。

还有其他人 运行 参与其中吗?或者有人知道这可能是什么吗?

尝试将以下内容添加到您的清单中

<uses-feature android:name="android.hardware.bluetooth" android:required="true" />

如果您确定用户仅在 Samsung KNOX 上遇到此问题,以下答案可能会对您有所帮助。

循序渐进的思考,才是解决问题的方法。

1.This only occurs on some Samsung devices.

So, it has nothing to do with your code,also the permission.There is something wrong with the Samsung devices.

2.Check the crash log:  
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10025 nor current process has android.permission.BLUETOOTH.

According to the experience,if you have met the log like "Neither user **** nor current process has *** permission.",this means that your app doesn't have the permission to do something in the system.This occurs often on that you do something need the system level permission.

If you are installed as system level app together with customized Android ROM,or you signed your app with the platform.x509.pem and platform.pk8 file of the customized Android ROM,and then you can have the permission.

现在检查三星 KNOX 是什么:

Samsung Knox (stylized Samsung KNOX) is an enterprise mobile security solution.

Bluetooth is one kind of data transmission way to the other's device.This will cause the security problem.

查看官网:
https://www.samsungknox.com

搜索关键词'Bluetooth',我们发现: https://www.samsungknox.com/en/faq/are-bluetooth-bluetooth-low-energy-and-nfc-available-inside-knox-container

KNOX 2.4+ — KNOX 容器内支持蓝牙、低功耗蓝牙 (GATT) 和 NFC。

KNOX 2.3 或更低版本 — KNOX 容器内不支持蓝牙、蓝牙 GATT 和 NFC。如果设备连接到个人 space 中的外围设备(例如耳机),即使用户进入 KNOX 容器,蓝牙也将保持连接。 KNOX 容器内的应用无法启动蓝牙连接。

1.If your users are using KNOX 2.3 or below,and they can update their OS of device to KNOX 2.4+.tell your users who use Samsung KNOX update their OS of device.

2.If your users are using KNOX 2.3 or below,and they can't update their OS of device to KNOX 2.4+.your app will not work under this situation,the problem is not the problem,what you can do is only tell uses that:the OS of your device doesn't support Bluetooth,so it's not working.

3.If the above will not help you, you can contact with Samsung KNOX Official

另一个提示:
为了改善您的应用的用户体验,您需要防止您的应用崩溃。
崩溃是最糟糕的用户体验。
如果你不能解决问题,你可以这样做:

 try{
     You code related with Bluetooth.
    }catch (Exception e)
    {
    }

Remember:The offical always helps you to solve the problems better!


从 SDK 23 开始,您需要检查蓝牙权限以备不时之需。

   if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
         if (mContext.checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
//TODO - Do the work
    }
  }

你可以看看here。我想这是你的问题。

Are Bluetooth, Bluetooth Low Energy, and NFC available inside the KNOX container?

KNOX 2.4+ — Bluetooth, Bluetooth Low Energy (GATT), and NFC are supported inside the KNOX container.

KNOX 2.3 or below — Bluetooth, Bluetooth GATT, and NFC aren't supported inside the KNOX container. If the device is connected to a peripheral (e.g. headphones) in the personal space, Bluetooth will remain connected, even if the user enters the KNOX container. Apps inside the KNOX container can't initiate a Bluetooth connection.

我从来没有这方面的实践经验,但我希望这对您有所帮助。

从 KNOX Premium v​​1.1 开始,三星不再需要签署第三方应用程序 (APK) 文件,这样它们就可以 运行 在 KNOX 容器中。相反,在应用程序成功激活其 ELM 和 KLM 许可证后,在 运行 时间授予适当的权限。有关详细信息,请参阅 this