0

I have the code for Enabling/Disabling Bluetooth which works fine only when I enable and disable Bluetooth via code only. but when I enable it through the notification bar and try to disable it through code, the app gets force closed.

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" /> 

Error Log

        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
Cœur
  • 37,241
  • 25
  • 195
  • 267
JRE.exe
  • 801
  • 1
  • 15
  • 28

1 Answers1

1

From the documentation

public boolean disable ()

Added in API level 5 Turn off the local Bluetooth adapter—do not use without explicit user action to turn off Bluetooth.

This gracefully shuts down all Bluetooth connections, stops Bluetooth system services, and powers down the underlying Bluetooth hardware.

Bluetooth should never be disabled without direct user consent. The disable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

This is an asynchronous call: it will return immediately, and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this call returns true, then the adapter state will immediately transition from STATE_ON to STATE_TURNING_OFF, and some time later transition to either STATE_OFF or STATE_ON. If this call returns false then there was an immediate problem that will prevent the adapter from being turned off - such as the adapter already being turned off.

Requires the BLUETOOTH_ADMIN permission

Returns true to indicate adapter shutdown has begun, or false on immediate error

This could be the cause of your woes. Is there any user interaction here?

This person had the same problem.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • how does the app called "Tasker" do it? and what is the solution for this? i don't want to take the user to the settings. – JRE.exe May 08 '14 at 03:03
  • the code can enable bluetooth without user interaction, so if bluetooth is enabled, can i enable it again via code and then disable it via code? maybe this should work – JRE.exe May 08 '14 at 03:05
  • I am not sure about Tasker. If the documentation explicitly states that you should not do it, you probably should not do it. And if you do it anyway, you shouldn't expect it to work right. – Jack May 08 '14 at 17:39
  • yes i want to do it and i dont care about what is written in the documentation. can you figure it out? – JRE.exe May 09 '14 at 16:56