【问题标题】:Get bluetooth signal strength获取蓝牙信号强度
【发布时间】:2013-03-09 16:11:17
【问题描述】:

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

如何获取蓝牙信号强度?

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

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

这是我的活动:

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

}

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

【问题讨论】:

  • 我已经看过了,但是没有任何关于如何找到信号强度的例子。也许你能帮我吗?
  • 我认为您的代码还可以。要查看 Toast,您需要先执行 Discover。

标签: java android


【解决方案1】:

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

基本上,蓝牙发现是向范围内的所有站点广播以进行响应。随着每个设备的响应,Android 会触发一个 ACTION_FOUND 意图。在此意图中,您可以通过 getExtra 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();
        }
    }
};

【讨论】:

  • 你能给我举个例子,我怎样才能使用 RSSI 并从中获得蓝牙强度?
  • 非常感谢,我写了这段代码,但是当我与设备配对时,它没有在 Toast 中显示信号强度,您还有其他方法吗?我还将我的活动添加到主线程
  • EXTRA_RSSI 值是短类型而不是 int 改变它然后检查
  • 如果您对他的回答感到满意,别忘了接受他的回答(答案左侧的绿色图标)
  • 我认为您的代码还可以。要查看 Toast,您需要先执行 Discover。
【解决方案2】:

我认为您的代码还可以,但您需要实现 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");
            }
        }
    };
}

看起来是这样的:

【讨论】:

  • 你能检查一下这个question吗?
【解决方案3】:

在 API 18 (Android 4.3) 中引入了必要的 API。您需要调用BluetoothGatt#readRemoteRssi() 来发起请求。响应显示在 BluetoothCallback#onReadRemoteRssi() 回调中。 (就是处理连接、发现、特征读取等的回调对象)

不再需要广播接收器的东西。

【讨论】:

  • 我如何将它与蓝牙经典套接字连接一起使用。
  • 此答案适用于 BLE 设备,不适用于经典蓝牙
【解决方案4】:

您可以使用 BluetoothDevice 的 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));
      }
    });
  }

}

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

<uses-permission android:name="android.permission.BLUETOOTH" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 2012-03-09
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多