gpt4 book ai didi

java - Listview 和 OnItemClickListener(蓝牙设备)

转载 作者:行者123 更新时间:2023-12-01 09:17:50 25 4
gpt4 key购买 nike

我的 OnItemClickListener 遇到问题。例如,我的列表中有 4 个蓝牙设备,我单击一个元素,应用程序尝试仅与列表的最后一个元素配对,而不是与我单击的元素配对

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();
}
}
}

最佳答案

这可能是因为您已将最后发现的设备保存在变量“device”中,并且在 ListView onItemClick 中您正在调用pairDevice(device),无论单击的项目的位置如何。

您可能希望将所有广播发现的设备保存在一个列表中,并根据点击的项目从列表中获取相应的设备对象,如下所示:

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));
}
});
}
};

关于java - Listview 和 OnItemClickListener(蓝牙设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com