How To Find The Devices In The Range By Using Bluetooth?
I am new to android.I want to develop an application to find the devices in the range by using Bluetooth programmatically.If any one has idea please give some sample code to me.
Solution 1:
Find The Devices in the Range by using Bluetooth programmatically.
Yes you can do this using BroadcastReceiver, check out below code, it will help you.
Starting search
mBluetoothAdapter.startDiscovery();
mReceiver = newBroadcastReceiver() {
publicvoidonReceive(Context context, Intent intent) {
String action = intent.getAction();
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
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
Solution 2:
Create Broad cast receiver something like the following and add the device information
private final BroadcastReceiver mReceiver = newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
String action = intent.getAction();
ArrayList<HashMap<String, String> arl = newArrayList<HashMap<String, String>();
// When discovery finds a deviceif (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the IntentBluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed alreadyHashMap<String, String> deviceMap = newHashMap<String, String>();
deviceMap.put(device.getName(), device.getAddress());
arl.add(deviceMap);
// When discovery is finished, change the Activity title
} elseif (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
Solution 3:
you might want to use method startDiscovery() .
I dont have a sample code right now but you might want to have a look at : http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery%28%29
Hope it helps!
Post a Comment for "How To Find The Devices In The Range By Using Bluetooth?"