【问题标题】:Android BluetoothAdapter Disable function not working on Device 2.3.6Android BluetoothAdapter 禁用功能在设备 2.3.6 上不起作用
【发布时间】:2013-04-22 12:35:45
【问题描述】:

我无法在 4.0 + 版本的 Android 版本较低(检查 2.3.6)的设备上禁用我的应用程序中的蓝牙,它工作正常。这是我的简单代码:

public class MainActivity extends Activity implements View.OnClickListener {
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    findViewById(R.id.helloTxt).setOnClickListener(this);
}
@Override
public void onClick(View v) {       
    if (mBluetoothAdapter.isEnabled()) {            
        mBluetoothAdapter.disable();
    }
}
}

在清单中也给予了必要的许可:

uses-permission android:name="android.permission.BLUETOOTH"
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"

和由aLogCat应用程序生成的设备上的logcat结果。

E/AndroidRuntime(28167): FATAL EXCEPTION: main
E/AndroidRuntime(28167): java.lang.SecurityException: Calling uid 10097 gave packageandroid which is owned by uid 1000
E/AndroidRuntime(28167):    at android.os.Parcel.readException(Parcel.java:1322)
E/AndroidRuntime(28167):    at android.os.Parcel.readException(Parcel.java:1276)
E/AndroidRuntime(28167):    at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:632)
E/AndroidRuntime(28167):    at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:563)
E/AndroidRuntime(28167):    at com.example.bluetoothtest.MainActivity.onClick(MainActivity.java:25)
E/AndroidRuntime(28167):    at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(28167):    at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(28167):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(28167):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(28167):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(28167):    at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(28167):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28167):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(28167):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
E/AndroidRuntime(28167):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E/AndroidRuntime(28167):    at dalvik.system.NativeStart.main(Native Method)

它在 android 2.3.6 版本的设备中崩溃了应用程序。请帮忙!!

提前致谢。

【问题讨论】:

  • 什么是崩溃,报告 logcat
  • @Siddharth 感谢您的回复,我已经添加了 logcat 详细信息。请帮忙。
  • 我收到了类似这样的报告,涉及 BluetoothAdapter.disable()。所有用户都在运行 Android 4.x,所以这不是因为版本较旧。我怀疑它可能是运行 PrivacyGuard (?) 并拒绝应用程序的某些权限的用户,在这种情况下是蓝牙。希望看到明确的答复。

标签: android bluetooth


【解决方案1】:

要获取代表本地蓝牙适配器的BluetoothAdapter,在JELLY_BEAN_MR1及以下运行时,调用静态getDefaultAdapter()方法;在 JELLY_BEAN_MR2 及更高版本上运行时,通过 getSystemService(String) 和 BLUETOOTH_SERVICE 检索它。

【讨论】:

    猜你喜欢
    • 2020-02-26
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2013-02-07
    相关资源
    最近更新 更多