以编程方式禁用蓝牙Android

时间:2014-05-08 02:21:18

标签: android bluetooth

我有启用/禁用蓝牙的代码,仅当我通过代码启用和禁用蓝牙时才能正常工作。但是当我通过通知栏启用它并尝试通过代码禁用它时,应用程序将被强制关闭。

package com.example.tutorial;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void optimize(View view) {

            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
             if (mBluetoothAdapter == null) {
                 // Device does not support Bluetooth

                 }else{

                 if (!mBluetoothAdapter.isEnabled()) {

                    mBluetoothAdapter.enable();

                 }else{

                  mBluetoothAdapter.disable();    

                 }

                 }
    }}



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

错误记录

        05-08 07:45:16.976: I/PowerManagerService(1509): Light Animator Finished curIntValue=156
        05-08 07:45:17.046: I/BluetoothService(1509): disable...
        05-08 07:45:17.046: W/BluetoothService(1509): setScanMode is called 20
        05-08 07:45:17.070: E/BluetoothEventLoop.cpp(1509): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/7862/hci0
        05-08 07:45:17.078: D/AndroidRuntime(8015): Shutting down VM
        05-08 07:45:17.078: W/dalvikvm(8015): threadid=1: thread exiting with uncaught exception (group=0x40018578)
        05-08 07:45:17.085: E/BluetoothEventLoop.cpp(1509): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/7862/hci0
        05-08 07:45:17.117: E/AndroidRuntime(8015): FATAL EXCEPTION: main
        05-08 07:45:17.117: E/AndroidRuntime(8015): java.lang.IllegalStateException: Could not execute method of the activity
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.view.View$1.onClick(View.java:2144)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.view.View.performClick(View.java:2485)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.view.View$PerformClick.run(View.java:9080)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.os.Handler.handleCallback(Handler.java:587)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.os.Handler.dispatchMessage(Handler.java:92)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.os.Looper.loop(Looper.java:130)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.app.ActivityThread.main(ActivityThread.java:3687)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at java.lang.reflect.Method.invokeNative(Native Method)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at java.lang.reflect.Method.invoke(Method.java:507)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at dalvik.system.NativeStart.main(Native Method)
        05-08 07:45:17.117: E/AndroidRuntime(8015): Caused by: java.lang.reflect.InvocationTargetException
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at java.lang.reflect.Method.invokeNative(Native Method)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at java.lang.reflect.Method.invoke(Method.java:507)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.view.View$1.onClick(View.java:2139)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     ... 11 more
        05-08 07:45:17.117: E/AndroidRuntime(8015): Caused by: java.lang.SecurityException: Calling uid 10070 gave packageandroid which is owned by uid 1000
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.os.Parcel.readException(Parcel.java:1322)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.os.Parcel.readException(Parcel.java:1276)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:632)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:563)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     at com.example.tutorial.MainActivity.optimize(MainActivity.java:40)
        05-08 07:45:17.117: E/AndroidRuntime(8015):     ... 14 more
        05-08 07:45:17.125: E/liblog(1509): failed to call dumpstate
        05-08 07:45:17.140: E/BluetoothEventLoop.cpp(1509): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/7862/hci0
        05-08 07:45:17.148: E/BluetoothEventLoop.cpp(1509): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/7862/hci0
        05-08 07:45:17.171: E/(1509): Dumpstate > /data/log/dumpstate_app_error

1 个答案:

答案 0 :(得分:1)

来自documentation

  

public boolean disable()

     

在API级别5中添加关闭本地蓝牙适配器 - 不要使用   没有明确的用户操作来关闭蓝牙

     

这可以优雅地关闭所有蓝牙连接,停止蓝牙   系统服务,并关闭底层蓝牙硬件。

     

未经用户同意,不得禁用蓝牙。 disable()方法仅适用于包含用户的应用程序   用于更改系统设置的界面,例如&#34;电源管理器&#34;   应用

     

这是一个异步调用:它将立即返回,并返回客户端   应该听取ACTION_STATE_CHANGED以获得后续通知   适配器状态更改。如果此调用返回true,则适配器   state将立即从STATE_ON转换为STATE_TURNING_OFF,   一段时间后转换到STATE_OFF或STATE_ON。如果   这个调用返回false然后会出现一个直接的问题   防止适配器关闭 - 例如适配器   已被关闭。

     

需要BLUETOOTH_ADMIN权限

     

返回true表示适配器关闭已开始,或者为false   立即错误

这可能是你的困境的原因。这里有用户互动吗?

This有同样的问题。