1

I made an application with two activities. The first activity imports user parameters and the second activity sends data via bluetooth. I use .ACTION_REQUEST_ENABLE to enable bluetooth if it is disabled but when bt is closed my apk exits. It doesn't work. Any help?

I use this; code on create activity and after start handler and runnnable... I test it only findBT and work...

 void findBT()
 {
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if(mBluetoothAdapter == null)
  {
      myLabel.setText("No bluetooth adapter available");
  }

  if(!mBluetoothAdapter.isEnabled())
  {
      //My problem is there
      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBluetooth, 0);
  }

  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  if(pairedDevices.size() > 0)
  {
      for(BluetoothDevice device : pairedDevices)
      {
          if(device.getName().equals(strValue2))
          {
              mmDevice = device;
              break;
          }
      }
  }
  myLabel.setText("Bluetooth Device Found");
  }
2
  • Do you mean your application is crashing by "apk exit"? Dec 20, 2012 at 8:38
  • hey shreya do u have any about bluetooth API ... :) Dec 20, 2012 at 9:02

2 Answers 2

5

here is the code :

 public class AndroidBluetooth extends Activity {

    private static final int REQUEST_ENABLE_BT = 1;

    /** Called when the activity is first created. */

    TextView stateBluetooth;
    BluetoothAdapter bluetoothAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        CheckBlueToothState();
    }

    private void CheckBlueToothState(){
        if (bluetoothAdapter == null){
            stateBluetooth.setText("Bluetooth NOT support");
        }else{
            if (bluetoothAdapter.isEnabled()){
                if(bluetoothAdapter.isDiscovering()){
                    stateBluetooth.setText("Bluetooth is currently in device discovery process.");
                }else{
                    stateBluetooth.setText("Bluetooth is Enabled.");
                }
            }else{
                stateBluetooth.setText("Bluetooth is NOT Enabled!");
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(requestCode == REQUEST_ENABLE_BT){
            CheckBlueToothState();
        }

    }


}
3
  • Ok.What is REQUEST_ENABLE_BT?How i declare it? Dec 20, 2012 at 8:50
  • Bluetooth admin permission when needed? Dec 20, 2012 at 9:08
  • "android.permission.BLUETOOTH_ADMIN" give this in ur Manifest.xml Dec 20, 2012 at 9:13
2

This code to check your device is BlueTooth enabled or not:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this.getParent(), ConfigClass.BLUETOOTH_NOT_SUPPORTED_ERROR, Toast.LENGTH_LONG);
}

Check and Enable your Bluetooth :

else if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, RESULT_OK);
1
  • Its my constant class...U can write your own message at that place...let me know If you need any other functionality info Dec 20, 2012 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.