获得蓝牙信号强度

时间:2013-03-09 16:11:17

标签: java android

我想获得连接到手机的另一台设备的蓝牙信号强度,

如何获得蓝牙信号强度?

我试图在谷歌搜索很多,但没有找到任何答案。

有人知道我该如何实施它?

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

}

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

4 个答案:

答案 0 :(得分:33)

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

基本上,蓝牙发现是对范围内所有站点进行广播以进行响应。当每个设备响应时,Android会触发ACTION_FOUND意图。在此意图中,您可以获取Extra EXTRA_RSSI以获取RSSI。

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

还相关:Android IRC Office Hours Question About Android Bluetooth RSSI 此处为示例

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 :(得分:24)

我认为您的代码没问题,但您需要实施startDiscovery()才能看到结果。

正确的是BluetoothDevice.EXTRA_RSSI仅适用于发现设备,当您连接到其中一个设备时,您无法再获取其RSSI。

在这里,我开发了一个非常简单的活动样本,允许您查看靠近您的设备的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

答案 2 :(得分:6)

API 18(Android 4.3)中引入了必要的API。您需要致电enter image description here以发起请求。响应显示在BluetoothGatt#readRemoteRssi()回调上。 (这是处理连接,发现,特征读取等的回调对象。)

不再需要广播接收器。

答案 3 :(得分:1)

您可以使用其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" />