2017-05-26 11 views
0

私は、ブリースキャナの2つの実装において、走査が停止され、所定の時間の後、例えば20秒ごとに再び開始されることに気づいた。なぜ青い歯の低エネルギースキャナを再起動する必要がありますか?

ここでは、スキャナクラスを別のスレッドで起動する例を示します。あなたは、スレッドが一定時間スリープ状態に置かれstart()方法で見ることができ、およびスキャナは、その後停止して再起動する:

public class BleScanner extends Thread { 

    private final BluetoothAdapter bluetoothAdapter; 
    private final BluetoothAdapter.LeScanCallback mLeScanCallback; 

    private volatile boolean isScanning = false; 

    public BleScanner(BluetoothAdapter adapter, BluetoothAdapter.LeScanCallback callback) { 

     bluetoothAdapter = adapter; 
     mLeScanCallback = callback; 
    } 

    public boolean isScanning() { 
     return isScanning; 
    } 

    public void startScanning() { 
     synchronized (this) { 
      isScanning = true; 
      start(); 
     } 
    } 

    public void stopScanning() { 
     synchronized (this) { 
      isScanning = false; 
      bluetoothAdapter.stopLeScan(mLeScanCallback); 
     } 
    } 

    @Override 
    public void run() { 

     try { 

      // Thread goes into an infinite loop 
      while (true) { 

       synchronized (this) { 

        // If there is not currently a scan in progress, start one 
        if (!isScanning) break; 
        bluetoothAdapter.startLeScan(mLeScanCallback); 
       } 

       sleep(Constants.SCAN_PERIOD); // Thread sleeps before stopping the scan 

       // stop scan 
       synchronized (this) { 
        bluetoothAdapter.stopLeScan(mLeScanCallback); 
       } 

       // restart scan on next iteration of infinite while loop 
      } 

     } catch (InterruptedException ignore) { 


     } finally { // Just in case there is an error, the scan will be stopped 

      bluetoothAdapter.stopLeScan(mLeScanCallback); 

      // The finally block always executes when the try block exits. This ensures that the 
      // finally block is executed even if an unexpected exception occurs. 
     } 
    } 
} 

は、スキャナを停止し、再起動へのメリットがありますか?スキャンを永久に続けるだけではどうですか?

答えて

1

利点があります。一部のデバイスでは、1回のスキャンで1回だけデバイスからの広告が表示されます。一部の広告ではすべての広告が表示されます。また、スキャンを再開すると、低レベルのものがクリーンアップされ、通常はスキャナを常にアクティブにするよりも優れています。

関連する問題