2

I am doing a bluetooth project in which I want to pair two bluetooth devices programmatically in Android. I am using default api in Android. It is finding devices. But does not return Action_found intent. While checking the condition for the action_found intent, it returns false. But in logcat it shows the found devices address. Here is the sample code I tried.

package com.qburst.android.settingsmanager.view;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

public class Bluetooth extends Activity {
    protected static final int REQUEST_ENABLE_BT = 1;
    private View mBluetooth;
    private Button mOnButton;
    private Button mActiveDevices;
    private Button mDiscoverable;
     public ArrayAdapter<String> mArrayAdapter;
     public BluetoothAdapter mBluetoothAdapter;
     ArrayList<String> devices = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContentView(R.layout.bluetooth);
    mOnButton = (Button) findViewById(R.id.on_button);
    mActiveDevices = (Button) findViewById(R.id.active_devices);
    mDiscoverable = (Button) findViewById(R.id.discoverable_button);
    mArrayAdapter = new ArrayAdapter<String>(this, R.layout.main);
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver, filter);


    mOnButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
              mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
              if (mBluetoothAdapter == null) {
                  // Device does not support Bluetooth
              }
              if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                }
        }
    });


    mActiveDevices.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

             Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
             System.out.println("checking paired devices");
          // If there are paired devices
          if (pairedDevices.size() > 0) {
              System.out.println(" paired devices");
              // 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());

              }
          }else {
              mBluetoothAdapter.startDiscovery();
          }
        }
    });

    mDiscoverable.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             Intent discoverableIntent = new
                     Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                     startActivity(discoverableIntent);
          }
    });
    }

     public void onActivityResult(int requestCode, int resultCode, Intent data) {

            switch (requestCode) {


            case REQUEST_ENABLE_BT:
                // When the request to enable Bluetooth returns
                if (resultCode == Activity.RESULT_OK) {
                    Toast.makeText(this, "Bluetooth enebled", Toast.LENGTH_SHORT).show();

                } else {
                    // User did not enable Bluetooth or an error occurred

                    Toast.makeText(this, "Bluetooth not enebled", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }


     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                devices.add(device.getName());
                   devices.add(device.getAddress());
                   System.out.println(devices);
                // When discovery finds a device
                Log.i("receiver","Inside receiver");

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    System.out.println("device found");
                    Toast.makeText(getBaseContext(), "No device found",Toast.LENGTH_LONG);
                    // 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
                   System.out.println(device.getName());
                   devices.add(device.getName());
                   devices.add(device.getAddress());
                   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                   System.out.println(devices);
                }
            }
        };


        // Register the BroadcastReceiver

        // Don't forget to unregister during onDestroy
        @Override
        protected void onDestroy() {

            super.onDestroy();
        }
}
0

1 Answer 1

2

You should add the correct action to mReceiver's filter! You should have added: filter.addAction(BluetoothDevice.ACTION_FOUND);

BluetoothDevice.ACTION_FOUND is not in the filter, so it is never received!

So change these lines:

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);

to this:

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);

Let me know if it worked!

0

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.