luc luc - 1 month ago 19
Java Question

Listview and OnItemClickListener(Bluetooth device)

I have a problem with OnItemClickListener.
For example, i have 4 bluetooth devices in the list,i click on one element and the application try to pair only with the last element of the list not with the element that i clicked

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.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.reflect.Method;
import java.util.ArrayList;


public class bluetooth extends AppCompatActivity {
private ListView listView;
private ArrayList<String> mDeviceList = new ArrayList<String>();
private BluetoothAdapter mBluetoothAdapter;
private final static int REQUEST_ENABLE_BT=1;

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

listView = (ListView) findViewById(R.id.listView);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
mBluetoothAdapter.startDiscovery();
registerReceiver(mReceiver, filter);
}

}

@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, final Intent intent) {
final BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(bluetoothDevice.getName() + "\n"
+ bluetoothDevice.getAddress());
listView.setAdapter(new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, mDeviceList));
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long id){
String devicep=listView.getAdapter().getItem(pos).toString();
Log.i("device",devicep);
pairDevice(device);
}
});
}
};

private void pairDevice(BluetoothDevice b1) {
try {
Method method = b1.getClass().getMethod("createBond", (Class[]) null);
method.invoke(b1, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Answer

This is probably because you have saved the last discovered device in the variable 'device' and in your on ListView onItemClick you are calling pairDevice(device), irrespective of the position of item being clicked.

You may want to save all the broadcasted discovered devices in a list and get the corresponding device object from the list according to the item clicked , something like this:

private ArrayList<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, final Intent intent) {
        final BluetoothDevice device = intent
            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the recently discovered device in a list
            mDevices.add(bluetoothDevice);
            mDeviceList.add(bluetoothDevice.getName() + "\n"
                + bluetoothDevice.getAddress());
            listView.setAdapter(new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, mDeviceList));
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int pos, long id){
                String devicep=listView.getAdapter().getItem(pos).toString();
                Log.i("device",device);
                // Get the BluetoothDevice corresponding to the clicked item
                pairDevice(mDevices.get(position));
            }
        });
    }
};