Android と Bluetooth モジュール間の接続を確立しようとしています。
「発見」後、 http://developer.android.com/guide/topics/connectivity/bluetooth.htmlの指示に従います。見つかったデバイスを、登録済みの ListView に配置しましたOnItemClickListener
。
私の問題はBluetoothDevice
、次のステップで接続を確立するために、クリックしたエントリから が必要なことです。私が持っているのは、ListView の位置 ID とSet<BluetoothDevice>
. BluetoothDevice
しかし、セットから特定のものを抽出する方法がわかりません。
これはこれまでの私のコードです。ありがとう!!
ProgressBar spinWheel;
ListView devList;
BluetoothAdapter btAdapter;
ArrayAdapter<String> lvAdapter;
BroadcastReceiver mReceiver;
Set<BluetoothDevice> pairedDevices;
@Override
public void onCreate(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<String>(getApplicationContext(),
R.layout.simplerow, R.id.simpleRow);
devList.setAdapter(lvAdapter);
devList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View childView, int position,
long id) {
//establish Connection, need the clicked BluetoothDevice
}
});
//gepaarte Geräte in die ListView
pairedDevices = btAdapter.getBondedDevices();
//If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
lvAdapter.add(device.getName() + "\n"+ "#"+ device.getAddress());
}
}
// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 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
lvAdapter.add(device.getName() + "\n" + device.getAddress());
pairedDevices.add(device);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
btAdapter.startDiscovery();
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(mReceiver);
}
}