I am trying to find the available Bluetooth devices.
This is my OnClickListener which is called when the user tries to search for the available devices.
View.OnClickListener OnSearchDevices = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "Search Devices", Toast.LENGTH_LONG).show();
Log.d("Discovery", "Started");
listOfDevices.clear();
label.setText("Searching Available Devices...");
label.setEnabled(false);
}
};
I have also registered a BroadcastReceiver.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
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);
Log.d("DeviceList" , device.getName() + "\n" + device.getAddress());
MyBluetoothDevice tempDevice = new MyBluetoothDevice();
tempDevice.setDeviceAddress(device.getAddress());
tempDevice.setDeviceName(device.getName());
listOfDevices.add(tempDevice);
mListAdapter.notifyDataSetChanged();
// discovery is finished
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d("Discovery","Finished");
label.setEnabled(true);
if(listOfDevices.size() == 0)
{
label.setText("No Devices Available!");
label.setTextColor(Color.parseColor("#FF0000"));
}
else
{
label.setText("Available Devices");
}
}
}
};
But nothing happens. It does not show anything. Please help.
Using the BluetoothAdapter , you can find remote Bluetooth devices either through device discovery or by querying the list of paired devices. Make sure you have the appropriate Bluetooth permissions and set up your app for Bluetooth before attempting to find Bluetooth devices.
If your Bluetooth devices won't connect, it's likely because the devices are out of range, or aren't in pairing mode. If you're having persistent Bluetooth connection problems, try resetting your devices, or having your phone or tablet "forget" the connection.
By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.
Looks like you are missing the call to mBluetoothAdapter.startDiscovery()
.
That would explain why you are not getting any results, because the adapter doesn't even start searching for the devices.
Your code looks fine to me otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With