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.
3 Answers 3
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.
mBluetoothAdapter.startDiscovery(); mReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction();
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);
Create Broad cast receiver something like the following and add the device information
private final BroadcastReceiver mReceiver = new BroadcastReceiver() < @Override public void onReceive(Context context, Intent intent) < String action = intent.getAction(); ArrayListarl = new ArrayList(); // 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); // If it's already paired, skip it, because it's been listed already HashMapdeviceMap = new HashMap(); deviceMap.put(device.getName(), device.getAddress()); arl.add(deviceMap); // When discovery is finished, change the Activity title > else if (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); >> > >;
Find already paired bluetooth devices automatically, when they are in range
I am no Bluetooth specialist and wondering what possibilities are available to find already paired Bluetooth devices automatically when they are range of each other. Background: In our case an Android application needs to connect to a dedicated accessory via Bluetooth (Rfcomm). Both devices are known to each other (they are paired). The Android application registers a broadcast receiver. During the startup of the application, the app initiates a discovery to find the dedicated accessory. If the accessory is in range everything works great. Problem: The user starts the application outside the range of the dedicated accessory. The Android application tries to discover the accessory without success. Then the user goes into the range of the Bluetooth accessory. The broadcast receiver won’t get notified about the accessory that is in range now. Similar Thread / Possible Solutions Similar questions were already asked on stackoverflow (e.g. autoconnect to bluetooth device when in range). But continuously trying to discover Bluetooth devices in range isn't what I am looking for because this would cause too much battery drain of the Android device. Another solution would be to try to connect to the paired device in the onResume method of the Activity. This would work but has the disadvantage that the application can’t run in the background. So the user had to bring the application at least once to the foreground to initiate the connection. A third idea I thought about is to implement a server socket into the Android application too. When the android application is started and the discovery finished without success, the Android application could create server socket and to listen to incoming notifications of the accessory. This would help in some scenarios (e.g. the user starts his application, approaches the accessory, activates the accessory and the accessory notifies the application on startup that it is in range now). But this is still no 100% solution because both devices can start outside the range of each other. Also it would be mandatory to implement additional functionality (Bluetooth server socket in the Android device…). So I am wondering if better solutions exist. I am looking for a solution where no additional ServerSockets are required and I always get the notification that the two already paired devices are in range of each other 🙂 Thanks for any help!
I would go by your 2nd solution in which phone tries to connect the accessory without searching for the same, take the example of phone and a headset, if connected and 1 goes out of range, normally headsets tries to connect to previously paired devices for a specific duration of time (depending on headset implementation).
Hey! Thanks for your reply. The disadvantage of the second solution is that this has to be triggered somehow (e.g. onResume). In the case of onResume the user has to bring the app into the foreground. I am looking for a possibility to do it automatically at the correct time to make it easy for the user 🙂
Is it not possible to have a service running in background which will take care of bluetooth device connect/search
Yeah that is the solution. You let the user pair the device manually. A background service tries to create a rfcomm socket to the bound device. Due to this no discovery is required. But for this approach it is mandatory, that the accessory hosts the bluetooth server and the app acts as the client
Is it possible to extend the range of Bluetooth on Android
In this article, we will talk about the range of Bluetooth on Android. Some users may not like their range of Bluetooth module (for example, when you are at home and the phone is behind three walls, your headset loses the connection to your smartphone). In such cases, many people are looking for a solution to this problem, trying to find a way to extend the range of Bluetooth on Android. We will talk about this in more detail in this article.
Theory
The receiver works in the 2.4 – 2.48 GHz frequency range. It operates as a conventional radio communication, and the data transfer rate (depending on the version of the receiver) may reach from 721 Kb/s (in the version 1.1) to 24 MB/s (in the versions 3.0 – 4.0).
The distance, at which the information can be exchanged, can reach 50 meters, if the devices are in direct line of sight of each other, and 10-20 meters in buildings.
Although the manufacturer claims that version 3.0 of the module can reach up to 100 m, but the conditions have to be ideal. In fact, the range is at least 2 times less (about 50 meters in the direct line of sight).
If talking about strengthening the frequency on the module of the smartphone, this option does not always solve the problem, because the connection between the devices depends on both modules. To make it clear: even if you will extend the range of the phone, then the radius of the safe work with your headset will remain in the same, due to the fact that the headset is able to find a smartphone at the same frequency as before.
The phone will be able to detect the headset at a greater distance than before, and perhaps you will be able to pair the devices, but the distance will not increase. Maybe your voice from the microphone will reach the phone, but you will not hear the companion, or vice versa.
You will see the following situation: the phone will be able to download the data from the headset, but the headset will not have not enough range of its module.
Therefore, if you want to increase the range of operation, you have to work with two devices.
What do we need
Theoretically, to enhance the Bluetooth, we will just need to apply more power to the module: we expect that there should be some kind of a program, (for example, “Bluetooth range extender for Android”), which will work with ROOT privileges and do everything in a couple of clicks… However, such a program does not exist.
It is impossible to calculate the amplification parameters suitable for all types of modules.
The situation is the same as with firmware: each device has its own firmware and there is no universal firmware for all devices. That is because the parameters of OS store the information about the individual parameters for the particular components that will provide the proper work of your smartphone.
As an option, it is possible to expand the range by adding an antenna. If this approach can still be suggested on a stationary PC (there is a physical space where you can add this antenna), this method is simply inappropriate in a situation with a smartphone.
The last method, which is the most successful in our opinion, is to purchase a special flash card with built-in Bluetooth receiver. Such cards are similar to the normal SD-cards, they have different formats, and they have a communication module (or Wi-Fi).
Initially, these cards were created for devices that had no built-in module, but in our case, we advise you to try to insert it. Perhaps, the Bluetooth in the card will be more sensitive than the one that is built into your device. There is a possibility that the module of the card will have a new version, so the radius of action will be greater.
It is very simple to connect the receiver. You need to put on the place of the usual SD-card, after which the system will do everything itself (load the appropriate drivers etc.), then the parameters of the new Bluetooth device should appear in the settings.
These cards are not easy to find in our country, so we advise to search them at the famous Chinese website .
We hope that our article helped you solve your problem, or, on the contrary, justified the need to accept the situation as it is.
[rBlock name=after_video return=1]
How to find available bluetooth devices in range?
I am trying to find all the available bluetooth devices in a range. But I am getting only one device I am using it in thread in run method. I already checked many links for this problem on it but could not resolve this issue. Here is my code
public void run() < if(service != null) < IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); service.registerReceiver(this.bReceiver, filter); bluetooth.startDiscovery(); >> class BluetoothReceiver extends BroadcastReceiver < public void onReceive(Context context, Intent intent) < SetpairedDevices = bluetooth.getBondedDevices(); String action = intent.getAction(); if (pairedDevices.size() > 0) < for (BluetoothDevice device : pairedDevices) < int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); Log.d(TAG, device.getName()); >> if(BluetoothDevice.ACTION_FOUND.equals(action)) < BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID); int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); Log.d(TAG, device.getName()); >> >
1 Answer 1
This is how I search for Bluetooth devices in an Activity and show their name and mac-address in a ListView . Apart from displaying the devices in a ListView , you can do practically anything with the discovered BluetoothDevice object.
FindBluetoothActivity.java
public class FindBluetoothActivity extends Activity < private BluetoothAdapter mBtAdapter; private ListView mLvDevices; private ArrayListmDeviceList = new ArrayList(); @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_bluetooth); mLvDevices = (ListView) findViewById(R.id.lvDevices); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mBtReceiver, filter); // Getting the Bluetooth adapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBtAdapter != null) < mBtAdapter.startDiscovery(); Toast.makeText(this, "Starting discovery. ", Toast.LENGTH_SHORT).show(); >else < Toast.makeText(this, "Bluetooth disabled or not available.", Toast.LENGTH_SHORT).show(); >> @Override protected void onDestroy() < super.onDestroy(); if (mBtAdapter != null) < mBtAdapter.cancelDiscovery(); >unregisterReceiver(mBtReceiver); > private final BroadcastReceiver mBtReceiver = new BroadcastReceiver() < @Override public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) < BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address ArrayAdapteradapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList); mLvDevices.setAdapter(adapter); > > >; >
Layout .xml file:
Android Manifest.xml file:
- Make sure Bluetooth is enabled on your device
- Add permissions android.permission.BLUETOOTH and android.permission.BLUETOOTH_ADMIN to your Manifest.xml file
- Make sure to unregister your broadcast-receiver when destroying the Activity
- Keep in mind that the Bluetooth devices in range need to be discoverable to be found by your application. Enabling bluetooth on them alone may not be enough. It is very often the case that some kind of "discoverable" mode needs to be enabled by the user before the device can be discovered by other devices.
- Be aware that the range in which devices are discoverable via Bluetooth is usually around 10m indoors and around 50m outdoors