Get bluetooth signal strength
To get the signal you can check Bluetooth RSSI, you can read RSSI for connected devices, or perform a Bluetooth discovery to check the RSSI for any nearby devices.
Basically a Bluetooth discovery is a broadcast to all stations within range to respond back. As each devices responds back, Android fires off an ACTION_FOUND intent. Within this intent you can getExtra EXTRA_RSSI to obtain the RSSI.
Note that not all bluetooth hardware supports RSSI.
Also Related: Android IRC Office Hours Question About Android Bluetooth RSSI here is a Bluetooth Classic broadcast receiver example
private final BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
Toast.makeText(getApplicationContext()," RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show();
}
}
};
I think your code is ok, but you need to implement startDiscovery()
in order to see results.
The true is that BluetoothDevice.EXTRA_RSSI
only works for discovering devices, when you connect to one of them you are not able any more to get its RSSI.
Here I developed a very simple sample of an Activity that permit you see the RSSI of the devices near to you. You first need to add a TextView and a Button to your layout, then enable the Bluetooth Adapter and then just click the button.
package com.in2apps.rssi;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class RSSIActivity extends Activity {
private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssi);
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
Button boton = (Button) findViewById(R.id.button1);
boton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
BTAdapter.startDiscovery();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rssi, menu);
return true;
}
private final BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
TextView rssi_msg = (TextView) findViewById(R.id.textView1);
rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n");
}
}
};
}
It looks this way:
The necessary API was introduced in API 18 (Android 4.3). You need to call BluetoothGatt#readRemoteRssi()
to initiate the request. The response shows up on the BluetoothCallback#onReadRemoteRssi()
callback. (That's the callback object that handles connect, discovery, characteristic reads, etc.)
The broadcast receiver stuff is no longer required.