I’m trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.
However, I get “Set cannot be resolved as a variable” at point ##1,
and “mArrayAdapber cannot be resolved” at point ##2
(##1 and ##2 are not part of the code…)
I used the code from the Android site, but being in the dark, I find myself in the dark.
I’d appreciate some guidance…
//Search
ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.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
##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
}
});
For 1) Well if you haven’t done so , add
> import java.util.Set;
in your import statements . This will resolve “Set” error.
For 2) Declare and initialize
For example in your Activity do :
and then on onCreate:
which should then be added to a ListView
Refer to Bluetooth Chat example from Android examples. It should help you get going with the Bluetooth api’s
Update on comment :
If you look closely on BluetoothChat.java file in BT example, you’ll see this
Watch this line :
This function connects to bluetooth device. First time it’ll ask you to pair it automatically. Once paired, next time it’ll auto connect to the bluetooth device.