Click here to Skip to main content
15,849,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have this code i get in[^] to discover devices that is triggered by a button. But whenever i run it, the program always pop-up a message to force close it. i don't know why. please help me.. here's is my code.. Thanks!
public void checkIfBluetoothOpen(View view) {

       BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       if (mBluetoothAdapter == null) {
           // Device does not support Bluetooth
           AlertDialog.Builder builder=new AlertDialog.Builder(this);
           .setMessage("Your device does not support bluetooth.")
           .setPositiveButton("OK", null)
           if (!mBluetoothAdapter.isEnabled()) {

               // Bluetooth is close
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                   startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

           Set<bluetoothdevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
           // If there are paired devices
           if (pairedDevices.size() > 0) {
               // Loop through paired devices
               for (BluetoothDevice device : pairedDevices) {
                   // Add the name and address to an array adapter to show in a ListView
                   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
           // Create a BroadcastReceiver for ACTION_FOUND
           final BroadcastReceiver mReceiver = new BroadcastReceiver() {
               public void onReceive(Context context, Intent intent) {
                   String action = intent.getAction();
                   // When discovery finds a device
                   if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                       // Get the BluetoothDevice object from the Intent
                       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                       // Add the name and address to an array adapter to show in a ListView
                       mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
           // Register the BroadcastReceiver
           IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
           registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

           Intent discoverableIntent = new
           discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
Updated 20-Jan-13 1:57am

1 solution

Generally an error like that is caused by inappropriate manifest file. Please check at the top of the document about setting permission.

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

If you have done that and still getting the error, it means that your emulator is not setup properly to use your System's bluetooth. You can just debug the App, copy the .apk in bin folder in your phone/tab and install it there. Test the app in real device.
Share this answer
Janna Dela Cruz 24-Jan-13 19:54pm    
i already did that.. still getting the same..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900