3

How can we make bluetooth discoverable state off programmatically.

On first click(buton) I could able to make it discoverable and for second click on the same button Its not working

I mean if its in discoverable state I should make it off

Here is my code

     bt_strength.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        //  if (mBtAdapter.getScanMode()!= BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
            if(!mBtAdapter.isDiscovering())
            {
                // if(bluetoothimg.getTag().toString().equalsIgnoreCase("off"))
                Log.i(TAG, "BLUETOOTH STATUS ON");
                bt_strength.setImageResource(R.drawable.bt);
                 Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
                    startActivity(discoverableIntent);  
            } 
            else if(mBtAdapter.isDiscovering()){

                bt_strength.setImageResource(R.drawable.bt_grey);
                // Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
               //     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
                //    startActivity(discoverableIntent);    
                mBtAdapter.cancelDiscovery();


            }

Any help is always appreciated, Thanks

Randroid
  • 3,688
  • 5
  • 30
  • 55

1 Answers1

0

What you are doing in the if-clause is, that you enable discoverable mode - that means that your device can be seen by other Bluetooth devices.

But the thing you are doing in the else-if-clause is that you cancel the discovery - which means that you stop searching for other devices.

What you wrote as a commentary right now (in the else-if-clause) should be the best working, alltough not perfect, solution, regarding to: Disable Bluetooth discoverable mode on Android

Community
  • 1
  • 1
julthedroid
  • 31
  • 11