获取蓝牙信号强度

47

我想获取连接到我的手机的另一个设备的蓝牙信号强度。

我该如何获取蓝牙信号强度?

我尝试在谷歌上搜索很多,但没有找到答案。

有人知道我该如何实现吗?

这是我的活动:

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();
            }
        }
    };

}

我的清单文件中也有蓝牙权限。


1
https://dev59.com/007Sa4cB1Zd3GeqP216r - Srinath Ganesh
1
我已经看过了,但是没有任何关于如何查找信号强度的示例。也许你能帮我吗? - Idan Rahamim
1
我认为你的代码没问题。为了看到Toast,你需要先执行一次Discover。 - Memochipan
4个回答

48

要获取信号,您可以检查蓝牙RSSI,您可以读取已连接设备的RSSI,或执行蓝牙发现以检查任何附近设备的RSSI。

基本上,蓝牙发现是向范围内所有站点广播以回复的广播。每个设备都会回复,Android会触发一个ACTION_FOUND意图。在此意图中,您可以使用getExtra EXTRA_RSSI来获取RSSI。

请注意,并非所有蓝牙硬件都支持RSSI。

还有相关内容:关于Android蓝牙RSSI的Android IRC办公时间问答 这里有一个Bluetooth Classic广播接收器示例

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();
        }
    }
};

1
非常感谢,我写了这段代码,但是当我与设备配对时,Toast中没有显示信号强度,你有其他的方法吗?我还将我的活动添加到了主线程。 - Idan Rahamim
1
EXTRA_RSSI的值是short类型,而不是int类型,请将其更改并进行检查。 - Arvind
我将其更改为int而不是short,但是当我与另一个设备配对时仍然没有显示任何Toast..您有其他建议吗?非常感谢。 - Idan Rahamim
我已经修改了它,但仍然没有显示任何内容...也许是我上面的代码有问题? - Idan Rahamim
1
我认为你的代码没问题。为了看到Toast,你需要先执行一次Discover。 - Memochipan
显示剩余5条评论

36

我认为你的代码没问题,但是你需要实现startDiscovery()方法才能看到结果。

事实上,BluetoothDevice.EXTRA_RSSI仅适用于发现设备,一旦你连接了其中一个设备,你就无法再获取其RSSI。

这里我开发了一个非常简单的Activity示例,可让您查看附近设备的RSSI。您首先需要将TextView和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");
            }
        }
    };
}

看起来是这个样子:

RSSI Detection - Android Example


你能帮我看看这个问题吗? - Skizo-ozᴉʞS ツ

13

3
我该如何使用它与经典蓝牙套接字连接。 - Herry
2
此答案适用于BLE设备,而非经典蓝牙。 - David

6
您可以使用rssi获取BluetoothDevice的信号强度。可以使用BluetoothAdapter获取绑定设备来完成此操作。
一旦您找到了感兴趣的设备,只需调用其上的connectGatt()方法,并定义一个新的BluetoothGattCallback。这是一个提供少量可重写方法的接口。下面列出的两个方法将允许您在每次连接状态更改时获取rssi。
@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));
      }
    });
  }

}

注意:此示例需要在您的清单文件中声明以下权限。
<uses-permission android:name="android.permission.BLUETOOTH" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接