我试图每秒显示蓝牙信号强度(RSSI)(Timer()
)来自检测到的设备,但我无法调用onRecive()
多次因为接收器生命周期 http://developer.android.com/reference/android/content/BroadcastReceiver.html.
我需要一种方法(想法)来刷新 RSSI,或者需要其他方法来每秒测量信号?
如果设备连接起来会更容易吗?
App 始终给出一个恒定值:
设备名称 2b:3c:d5:6c:3x:0X 40 db
应用程序的一部分存储在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 值。 (蓝牙发现与连接 https://stackoverflow.com/questions/6830887/bluetooth-discovery-vs-connection)
“如果设备连接起来是不是更容易?”根据 android 文档:
“如果您已经与设备保持连接,那么执行发现会显着减少可用于连接的带宽,因此您不应在连接时执行发现。”
你真的需要每1秒执行一次查询吗?会消耗大量电池...
既然您需要定期执行测量,为什么不使用 AlarmManager 或 CountDownTimer 呢?
在您的具体情况下,我相信您应该使用 AlarmManager,因为它可以无限期地重复。例如,如果您希望某件事每秒执行一次,持续 10 秒,请使用 CountDownTimer。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)