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) {
Set<BluetoothDevice> pairedDevices = 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());
}
}
}
In Addition I want rssi value of each found device, but please ignore the syntax
Tip: If you don't get a notification, open the Settings app on your phone and tap Connected devices. Under “Nearby devices,” select the device you want to pair. To learn more, go to Find Fast Pair accessories.
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.
The range of the Bluetooth® connection is approximately 30 feet (10 meters). However, maximum communication range will vary depending on obstacles (person, metal, wall, etc.) or electromagnetic environment.
Checking your Bluetooth signal strength is easy if you know what you're looking for. Bluetooth signal strength is measured by the Received Signal Strength Indicator (RSSI). You can access the device's RSSI by accessing the Bluetooth device settings and then reading the RSSI. An ideal RSSI is between -40 and -55.
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 ArrayList<String> mDeviceList = new ArrayList<String>();
@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
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList);
mLvDevices.setAdapter(adapter);
}
}
};
}
Layout .xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FindBluetoothActivity" >
<ListView
android:id="@+id/lvDevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Android Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name="com.example.bluetoothexample.FindBluetoothActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Additional information:
android.permission.BLUETOOTH
and android.permission.BLUETOOTH_ADMIN
to your Manifest.xml fileActivity
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