Android:每秒更新一次蓝牙rssi [英] Android: Update bluetooth rssi every second
问题描述
我正在尝试从检测到的设备显示每秒的蓝牙信号强度(rssi)(Timer()
),但是我无法多次调用onRecive()
,因为
I'm trying to display the bluetooth signal strength (rssi) evry second (Timer()
) from detected device but i couldn't call onRecive()
multiple times because Receiver Lifecycle.
我需要一种刷新RSSI的方法(想法),或者每秒其他一种测量信号的方法? 连接设备会更容易吗?
I need a way(idea) to refresh the RSSI, or some other way to measure a signal every second? Is it easier if the device is connected?
应用始终提供恒定值:
DeviceName 2b:3c:d5:6c:3x:0X 40 db
DeviceName 2b:3c:d5:6c:3x:0X 40 db
以Timer()
方法存储的部分应用程序:
Part of the app stored in Timer()
method:
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
// Add the name and address to an array adapter to show in a ListView
msg = device.getName() + " " + device.getAddress() + " " +rssi+ " db";
}
}};
推荐答案
您只能在设备发现扫描期间获取RSSI值. (蓝牙发现与连接)
You can only get the RSSI value during a device discovery scan. (Bluetooth Discovery vs Connection)
连接设备是否容易?"因此,与android docs: 如果已经与设备建立了连接,那么执行发现操作会大大减少可用于该连接的带宽,因此在连接时不应执行发现操作."
"Is it easier if the device is connected?" Accordingly with android docs: "if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected."
您真的需要每1秒执行一次查询吗?会消耗大量的电池...
Do you really need to perform an inquiry every 1 second? Will consume a lot of battery...
由于需要定期执行测量,为什么不使用AlarmManager或CountDownTimer?
Since you need to perform the measure periodically, why not use AlarmManager or CountDownTimer?
在您的特定情况下,我相信您应该使用AlarmManager,因为它可以无限期地重复.例如,如果要每秒执行某件事10秒钟,请使用CountDownTimer.
In your specific case, I believe you should be using AlarmManager, since it can repeat indefinitely. If you want something to execute every second for 10 seconds, for example, use CountDownTimer.
这篇关于Android:每秒更新一次蓝牙rssi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!