Gionne Lapuz
Gionne Lapuz

Reputation: 548

Bluetooth Pair Device on Item Click

I have been trying to create an app that would display the phones which have their bluetooth on and when I tap one of them in the listview it would pair them. I am kind of still learning bluetooth so I am kind of having trouble

How would I be able to pair the device that is shown in the list when I tap it on the listview?

If anyone decides to downvote, please leave a comment why I was downvoted. Thanks in advance for any help or insight! :D

This is my Activity:

private BluetoothAdapter BA;
private BluetoothDevice device;
private Set<BluetoothDevice>pairedDevices;
private ArrayList list;
private ArrayAdapter adapter;
private ListView lv;


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BA = BluetoothAdapter.getDefaultAdapter();
    lv = (ListView)findViewById(R.id.listViewPaired);

    list = new ArrayList();

    lv.setOnItemClickListener(this);
}


 final BroadcastReceiver bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            adapter.add(device.getName() + "\n" + device.getAddress());
            adapter.notifyDataSetChanged();
        }
    }
};

public void find(View v)
{
    txtTitle.setText("SEARCHING NEW DEVICES");

    adapter.clear();

    if (BA.isDiscovering()) {
       BA.cancelDiscovery();
    }
    else {
       BA.startDiscovery();

        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    String value = String.valueOf(list.get(position));

    Toast.makeText(MainActivity.this, value , Toast.LENGTH_LONG).show();
}
}

Upvotes: 0

Views: 247

Answers (1)

Sergio
Sergio

Reputation: 864

First, you need a BroadcastReceiver, that will be executed after the pairing takes place:

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
   public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();

       if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            final int state        = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
            final int prevState    = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

            if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
                showToast("Paired");
            } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
                showToast("Unpaired");
            }
        }
   }
};

And in the listener you do this:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      BluetoothDevice device = listViewDetected.getItemAtPosition(position);
      try {
          Method method = device.getClass().getMethod("createBond", (Class[]) null);
          method.invoke(device, (Object[]) null);
      } catch (Exception e) {
          e.printStackTrace();
      }
   } 
});

Upvotes: 1

Related Questions