47

I want to get the Bluetooth signal strength of an another device which connected to my phone,

How can I get the Bluetooth signal strength?

I tried to search a lot over google and did not find any answer.

Does someone know how can I implement it?

this is myActivity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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);
                Toast.makeText(getApplicationContext(),"  RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show();
            }
        }
    };

}

I also has a Bluetooth permission in my manifest file.

Reg
  • 10,717
  • 6
  • 37
  • 54
Idan Rahamim
  • 597
  • 1
  • 6
  • 11

4 Answers4

48

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)) {
            short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            Toast.makeText(getApplicationContext(),"  RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show();
        }
    }
};
The Berga
  • 3,744
  • 2
  • 31
  • 34
Arvind
  • 1,548
  • 17
  • 23
  • can you give me an example, how can i reed the RSSI and get from it the bluetooth strength? – Idan Rahamim Mar 09 '13 at 16:36
  • 1
    thanks alot, i wrote this code, however when i paired with device it does not show me in Toast the signal strength, do you have another way? i also add my activity to the main thread – Idan Rahamim Mar 09 '13 at 17:28
  • 1
    EXTRA_RSSI value is a short type instead of int change it and then check – Arvind Mar 09 '13 at 17:40
  • Don't forget to accept his answer if you're happy with it(The green icon left of the answer) – SlowDeepCoder Mar 09 '13 at 17:40
  • i will accept it, its still doesnt work for me... @Arvind, the EXTRA_RSSI value is short and not int in my code, i also updated my code(in the main thread) however it still does not work – Idan Rahamim Mar 09 '13 at 17:47
  • I telling you to change it from short to int .Take into consideration that rssi values are usually negative (it could be dBm ) – Arvind Mar 09 '13 at 17:49
  • i changed it to int instead of short, however still when i get paired with another device it does not show any Toast..do you have another suggestion?thanks alot – Idan Rahamim Mar 09 '13 at 17:55
  • You forget to change Integer.MIN_VALUE – Arvind Mar 09 '13 at 18:22
  • i changed it, still show me nothing...maybe something wrong with my code above? – Idan Rahamim Mar 09 '13 at 18:31
  • 1
    I think your code is Ok. In order to see the Toast you need to execute a Discover first. – Memochipan Aug 04 '13 at 22:41
36

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:

RSSI Detection - Android Example

Memochipan
  • 3,405
  • 5
  • 35
  • 60
13

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.

fadden
  • 51,356
  • 5
  • 116
  • 166
6

You can get the signal strengh of a BluetoothDevice using its rssi. This can be done using BluetoothAdapter to get the bounded devices.

Once you have the one you are interested in, just call connectGatt() on it and define a new BluetoothGattCallback. This is an interface that provides few method to override. The two written below will allow you to have the rssi everytime the connection state changes.

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Get the default bluetoothAdapter to store bonded devices into a Set of BluetoothDevice(s)
  BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  // It will work if your bluetooth device is already bounded to your phone
  // If not, you can use the startDiscovery() method and connect to your device
  Set<BluetoothDevice> bluetoothDeviceSet = bluetoothAdapter.getBondedDevices();

  for (BluetoothDevice bluetoothDevice : bluetoothDeviceSet) {
    bluetoothDevice.connectGatt(this, true, new BluetoothGattCallback() {
      @Override
      public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
      }
      @Override
      public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        if(status == BluetoothGatt.GATT_SUCCESS)
          Log.d("BluetoothRssi", String.format("BluetoothGat ReadRssi[%d]", rssi));
      }
    });
  }

}

NOTE : This sample requires a the following permission declared in your manifest file

<uses-permission android:name="android.permission.BLUETOOTH" />
Yoann.G
  • 283
  • 3
  • 6