Listview 从集合中访问Bluetooth设备

Listview 从集合中访问Bluetooth设备,listview,bluetooth,set,onitemclick,Listview,Bluetooth,Set,Onitemclick,我正在尝试在Android和蓝牙模块之间建立连接。我按照你的指示去做 在“发现”之后,我将找到的设备放入一个ListView,该ListView有一个注册的OnItemClickListener 我现在的问题是,我需要单击条目中的BluetoothDevice,以便在下一步中建立连接。我所拥有的只是ListView中的一个职位ID和一个集合。但我不知道如何从集合中提取特定的BluetoothDevice 这是到目前为止我的代码。 谢谢 ProgressBar旋转轮; 列表视图开发列表; 蓝牙适配

我正在尝试在Android和蓝牙模块之间建立连接。我按照你的指示去做 在“发现”之后,我将找到的设备放入一个ListView,该ListView有一个注册的
OnItemClickListener

我现在的问题是,我需要单击条目中的
BluetoothDevice
,以便在下一步中建立连接。我所拥有的只是ListView中的一个职位ID和一个
集合
。但我不知道如何从集合中提取特定的
BluetoothDevice

这是到目前为止我的代码。 谢谢

ProgressBar旋转轮;
列表视图开发列表;
蓝牙适配器;
阵列适配器;
广播接收机;
设置配对设备;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchlist);
spinWheel=(ProgressBar)findViewById(R.id.progressBar1);
btAdapter=BluetoothAdapter.getDefaultAdapter();
devList=(ListView)findViewById(R.id.devicelist);
lvAdapter=new ArrayAdapter(getApplicationContext(),
R.layout.simplerow,R.id.simplerow);
devList.setAdapter(lvAdapter);
setOnItemClickListener(新的OnItemClickListener(){
公共单击(AdapterView父视图、View子视图、int位置、,
长id){
//建立连接,需要点击蓝牙设备
}
});
//模具列表视图中的gepaarte Geräte
pairedDevices=btAdapter.getBondedDevices();
//如果有配对的设备
如果(pairedDevices.size()>0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
添加(device.getName()+“\n”+“#”+device.getAddress());
}
}
//为找到的操作创建广播接收器
mReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
//当发现发现发现设备时
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//将名称和地址添加到要在列表视图中显示的阵列适配器
lvAdapter.add(device.getName()+“\n”+device.getAddress());
pairedDevices.add(设备);
}
}
};
//注册广播接收器
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
registerReceiver(mReceiver,filter);//在onDestroy期间不要忘记取消注册
btAdapter.startDiscovery();
}
@凌驾
公共空间{
super.ondestory();
未注册接收人(mReceiver);
}
}

我自己解决了这个问题,但我想把它贴出来帮助别人。 我使用了函数BluetoothAdapter.getRemoteDevice(字符串地址)。 在ListView中插入字符串作为地址,在“#”处拆分

public-void-onItemClick(AdapterView-parentView、View-childView、int-position、,
长id){
String[]separated=lvAdapter.getItem(position).split(“#”);
if(btAdapter.checkBluetoothAddress(分隔[1])==true){
devtoconnect=btAdapter.getRemoteDevice(分开[1]);
}
}
});
不过,谢谢你在其他问题上的帮助

public void onItemClick(AdapterView<?> parentView, View childView, int position,
                long id) {
            String[] separated = lvAdapter.getItem(position).split("#");
            if(btAdapter.checkBluetoothAddress(separated[1])==true){
            devtoconnect = btAdapter.getRemoteDevice(separated[1]);
            }
        }
    });