Android BluetoothAdapter禁用功能无法在Device 2.3.6上运行

时间:2013-04-22 12:35:45

标签: android bluetooth

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

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"

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

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版本设备中崩溃的应用程序。请帮助!!

先谢谢。

1 个答案:

答案 0 :(得分:0)

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