Search code examples
androidbluetoothwidgetbackground-processandroid-permissions

Activate android bluetooth visibility without confirmation messages


Please, have a good day.

I'm developing an electronic white cane for impaired people; and I want to send some signals to my android device by using bluetooth.

My problem is I don't know how to avoid all the permissions related with the bluetooth activation. My friends (blind people) already use intelligent phones, so what I need to my application is to give the support for impaired people, Thereafter they can use the app in a easy way.

In view of that, I need to know how I could activate bluetooth in the background, without the device throws the confirmation message. Besides, I need to know, how can I transform my app into a widget, in order to it remain in execution meanwhile they can use other things?

Does anyone know how to manage bluetooth in this way? Could someone tell me where I can find this kind of information? Because I couldn't find any in Android Developers.

Thank you very much.

(edit)

I use this, thank you!

if(mBluetoothAdapter.isEnabled())
    {
        Toast.makeText(getBaseContext(), "Bluetooth already turned On.", Toast.LENGTH_LONG).show();
        label_bt_state.setText(getResources().getString(R.string.labelB_BT_ON));

        //If it isn't connected we can start searching the whiteCane.
        if(isConnected==0)
        {
            label_con_state.setText(getResources().getString(R.string.labelB_CON_OFF));
            if(wasFound==0)
            {
                btn_connect.setEnabled(true);                   
            }
        }else
        {
            label_con_state.setText(getResources().getString(R.string.labelB_CON_ON));
            btn_connect.setEnabled(false);
        }               
    }
    else
    {                           
        //If it's disable I'll turn it on
        Toast.makeText(getBaseContext(), "Bluetooth is disable.\n\tTurning on...", Toast.LENGTH_SHORT).show();                      


        new CountDownTimer(2000, 200) 
        {

             public void onTick(long millisUntilFinished) {
                 //Each 200mS will blink (5 times)
                 label_bt_state.setText(getResources().getString(R.string.labelB_BT_OFF));
                 label_con_state.setText(getResources().getString(R.string.labelB_BT_CON_LOAD));                         
                 btn_connect.setEnabled(false);

                 /*
                  * Radio Button disable
                  */
                 cb_xz.setEnabled(false);
                 cb_yz.setEnabled(false);
                 cb_xy.setEnabled(false);
                 cb_d.setEnabled(false);
        }
             public void onFinish() {
                 mBluetoothAdapter.enable();        
                 Toast.makeText(getBaseContext(), "Bluetooth enabled!", Toast.LENGTH_LONG).show();
                 label_bt_state.setText(getResources().getString(R.string.labelB_BT_ON));
                 label_con_state.setText(getResources().getString(R.string.labelB_CON_OFF));
                 btn_connect.setEnabled(true);

                 /*
                  * Radio Button enable
                  */
                 cb_xz.setEnabled(true);
                 cb_yz.setEnabled(true);
                 cb_xy.setEnabled(true);
                 cb_d.setEnabled(true);
             }
        }.start();//End CountDownTimer              

Solution

  • BluetoothAdapter.enable(),you can use this method to enable bluetooth without dialog.As doc suggest "Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth."