如何停止重复请求启用蓝牙

时间:2015-11-03 01:15:40

标签: android bluetooth-lowenergy android-bluetooth

每当我启动/恢复我的Android手机应用程序时,它会提示我启用我的蓝牙两次,无论我在第一个提示消息上选择什么。我的代码出了什么问题?

public class MainActivity extends AppCompatActivity {

    // BLE management
    private static BluetoothManager btManager;
    private static BluetoothAdapter btAdapter;

    // Set the enable bluetooth code
    private final static int REQUEST_ENABLE_BT = 0;


    // String for LogCat documentation
    private final static String DEBUG_TAG= "";

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

        btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        btAdapter = btManager.getAdapter();

        if (btAdapter != null) {
            if (!btAdapter.isEnabled()) {
                // Request Bluetooth Adapter to be turned on
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            }
        }

        else
        {
       Log.i(DEBUG_TAG, "No bluetooth available");
        }



        Button launchReminderButton = (Button) findViewById(R.id.button);
        launchReminderButton.setOnClickListener(new OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                    // Launch Reminder
                                                    // Used Context's startActivity() method

             // Create an intent stating which Activity you would like to
             // start
             Intent reminder = new Intent(MainActivity.this, Reminder.class);

             // Launch the Activity using the intent
             startActivity(reminder);
         }
                                                }
        );
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();

        // check for Bluetooth enabled on each resume
        if(btAdapter != null && !btAdapter.isEnabled()){
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
    }



    @Override
    public void onPause() {
        super.onPause();


    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onRestart() {
        super.onRestart();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        btAdapter = null;
    }

    @Override
    protected void onActivityResult(int request_enable_bt, int result_enable_bt, Intent data)
    {
        super.onActivityResult(request_enable_bt, result_enable_bt, data);

        if(result_enable_bt == RESULT_OK) {
                Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show();
        }

        else if (result_enable_bt == RESULT_CANCELED)
        {
            Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

感谢。

1 个答案:

答案 0 :(得分:1)

请注意以下代码,密钥为finish(),您在此处使用:

@Override
protected void onActivityResult(int request_enable_bt,
        int result_enable_bt, Intent data)
{
    super.onActivityResult(request_enable_bt, result_enable_bt, data);

    if (result_enable_bt == RESULT_OK)
    {
        Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show();
    }

    else if (result_enable_bt == RESULT_CANCELED)
    {
        Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show();
        finish();
    }
}

当您启动应用时,系统会提醒您打开蓝牙,因为您的请求是通过onCreate方法启用蓝牙的。当您deny请求时,方法{{1}回调,您将运行以下代码:

onActivityResult

else if (result_enable_bt == RESULT_CANCELED) { Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show(); finish(); } 每次点击您的应用时,当您第一次点击finish(),然后再次点击deny时,您的活动就会完成每次你得到这个:deny。所以你得到了两次启用蓝牙的请求!