2013-04-27 95 views
3

我试图从检测到的设备上显示蓝牙信号强度(rssi)evry second(Timer()),但由于Receiver Lifecycle我无法多次呼叫onRecive()Android:每秒钟更新蓝牙rssi

我需要一种方法(想法)来刷新RSSI,或者其他方式来每秒钟测量一次信号? 设备连接更容易吗?

应用给出了一个恒定的值始终:

的DeviceName 2B:3C:D5:6C:3×:0X40分贝存储在Timer()方法

部分的应用程式:

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"; 

     } 

}}; 

回答

2

您只能在设备发现扫描期间获取RSSI值。 (Bluetooth Discovery vs Connection

“设备连接起来更容易吗?”因此,如果您已经与设备保持连接,则使用android文档: “,则执行发现操作可能会显着减少可用于连接的带宽,因此您不应在连接时执行发现。”

你真的需要每隔1秒进行一次查询吗?将消耗大量电池...

由于您需要定期执行度量,为什么不使用AlarmManager或CountDownTimer?

在您的具体情况下,我相信您应该使用AlarmManager,因为它可以无限次重复。例如,如果您想要每秒执行一次,持续10秒钟,请使用CountDownTimer。