3

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              
  • have you checked this one out? [question](http://stackoverflow.com/questions/3806536/how-to-enable-disable-bluetooth-programmatically-in-android) – Chris Handy Jan 12 '15 at 17:47
  • Thank you, but that only produces the operative system displays a message such as: "One application is trying to use bluetooth. Do you want to activate it?" And thats what I want to avoid. Activate bluetooth, but avoiding that message; otherwise blind people will have to press a button to say yes, but how do they know its position? – Jhoan Sebastian Sierra Hernnde Jan 14 '15 at 02:25

2 Answers2

4

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

qianlv
  • 502
  • 5
  • 15
3

I believe this is what you are after then:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
mBluetoothAdapter.enable();
//mBluetoothAdapter.disable();

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

Android widget page: http://developer.android.com/guide/topics/appwidgets/index.html

edit:

That works for me without prompt :/ You could also try this (it toggles the state each time you call it):

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (!mBluetoothAdapter.isEnabled()) { //Enable if disabled
    Intent localIntent;
    localIntent = new Intent();
    localIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    localIntent.addCategory("android.intent.category.ALTERNATIVE");
    localIntent.setData(Uri.parse("4"));
    getBroadcast(paramContext, 0, localIntent, 0).send();
} 

With the permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
Chris Handy
  • 366
  • 1
  • 5
  • Thanks for your answer. For now it works, but I want to avoid messages like: "Bluetooth permission request An application on your phone is requesting permission to turn on Bluetooth. Do you want to do this? Yes / No" That's what I'm trying to avoid. How in hell the impaired guy is gonna know where the YES button is placed? In other words,,, How Can I tell to Android: Hey Android, please don't tell anything to the user, because he is blind? By the way, Thank you for the information about the widget! – Jhoan Sebastian Sierra Hernnde Feb 20 '15 at 04:50
  • Editied with something else that might be helpful – Chris Handy Feb 20 '15 at 10:20
  • What he's asking is not about turning bluetooth on or off, but bluetooth VISIBILITY on. Which causes android to pop up that message. I'm having the same issue now, so far no way around it. – stu Sep 10 '16 at 18:32