获得蓝牙信号强度

Ida*_*mim 32 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();
            }
        }
    };

}
Run Code Online (Sandbox Code Playgroud)

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

Arv*_*ind 35

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

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

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

另外相关:Android IRC办公时间问题关于Android蓝牙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();
        }
    }
};
Run Code Online (Sandbox Code Playgroud)


Mem*_*pan 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");
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

RSSI检测 -  Android示例


fad*_*den 7

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

不再需要广播接收器.

  • 我如何将它与蓝牙经典套接字连接一起使用。 (2认同)
  • 此答案适用于 BLE 设备,不适用于经典蓝牙 (2认同)

Yoa*_*n.G 6

您可以使用蓝牙设备的 rssi获取其信号强度。这可以使用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));
      }
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

注意:此示例需要在清单文件中声明以下权限

<uses-permission android:name="android.permission.BLUETOOTH" />
Run Code Online (Sandbox Code Playgroud)