2016-05-12 48 views
0

這是我的Singleton類,而我可以添加觀察員:綁定服務與Altbeacon第二個綁定後不工作

以下鏈接全類實現:

http://pastebin.com/bLpTH4u9

相關的代碼:

public void addSensor(AbstractBeaconSensor sensor){ 
    lock.lock(); 

    if(sensors.isEmpty() && !isBinded) { 
     beaconManager.bind(this); 
     Log.d(TAG, "Binding+++++++++++++++++++"); 
     isBinded = true; 
    } 

    sensors.add(sensor); 
    lock.unlock(); 
} 

public void removeSensor(AbstractBeaconSensor sensor){ 
    lock.lock(); 

    sensors.remove(sensor); 

    if(sensors.isEmpty() && isBinded){ 
     try { 
      beaconManager.stopRangingBeaconsInRegion(myRegion); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
     beaconManager.unbind(this); 
     isBinded = false; 
    } 
    lock.unlock(); 
} 

@Override 
public void onBeaconServiceConnect() { 
    Log.d(TAG, "On beacon connect+++++++++++++++++"); 
    beaconManager.setRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 

      Log.d(TAG, "++ " + beacons.size()); 
      long time = System.currentTimeMillis(); 
      try { 
       //lock.lock(); 
       for(AbstractBeaconSensor sensor : sensors) { 
        sensor.setData(beacons, time); 
       } 
      } finally { 
       //lock.unlock(); 
      } 
     } 
    }); 
    try { 
     beaconManager.startRangingBeaconsInRegion(myRegion); 
    } catch (RemoteException e) { 
     Log.d(TAG, "Got exception" + e.getMessage()); 
    } 
} 

問題是添加一個AbstractSensor後,掃描工作正常。

在調用stopRangingBeaconInRegion()和unbind(),然後再次調用bind()之後,該服務似乎只保留在後臺掃描模式下(每30秒我會得到一個信標列表)。

05-12 13:34:39.969 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++ 
05-12 13:34:42.497 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++ 
05-12 13:34:42.502 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++ 
05-12 13:34:43.728 25700-27614/interdroid.swan D/BeaconSingleton: ++ 5 
05-12 13:34:44.848 25700-27635/interdroid.swan D/BeaconSingleton: ++ 1 
05-12 13:34:46.029 25700-27655/interdroid.swan D/BeaconSingleton: ++ 3 
05-12 13:34:47.161 25700-27672/interdroid.swan D/BeaconSingleton: ++ 7 
05-12 13:34:54.091 25700-27778/interdroid.swan D/BeaconSingleton: ++ 6 
05-12 13:34:55.237 25700-27799/interdroid.swan D/BeaconSingleton: ++ 7 
05-12 13:34:56.423 25700-27817/interdroid.swan D/BeaconSingleton: ++ 4 
05-12 13:34:57.566 25700-27833/interdroid.swan D/BeaconSingleton: ++ 1 
05-12 13:34:58.747 25700-27850/interdroid.swan D/BeaconSingleton: ++ 3 
05-12 13:34:59.894 25700-27873/interdroid.swan D/BeaconSingleton: ++ 5 
05-12 13:35:01.053 25700-27891/interdroid.swan D/BeaconSingleton: ++ 7 
05-12 13:35:02.199 25700-27913/interdroid.swan D/BeaconSingleton: ++ 6 
05-12 13:35:03.354 25700-27930/interdroid.swan D/BeaconSingleton: ++ 3 
05-12 13:35:04.504 25700-27951/interdroid.swan D/BeaconSingleton: ++ 3 
05-12 13:35:05.648 25700-27968/interdroid.swan D/BeaconSingleton: ++ 2 
05-12 13:35:11.405 25700-28056/interdroid.swan D/BeaconSingleton: ++ 8 
05-12 13:35:12.548 25700-28071/interdroid.swan D/BeaconSingleton: ++ 4 
05-12 13:35:13.703 25700-28088/interdroid.swan D/BeaconSingleton: ++ 6 
05-12 13:35:38.580 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++ 
05-12 13:35:38.585 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++ 
05-12 13:35:39.789 25700-28555/interdroid.swan D/BeaconSingleton: ++ 3 
05-12 13:36:24.995 25700-25700/interdroid.swan D/BeaconSingleton: Binding+++++++++++++++++++ 
05-12 13:36:25.022 25700-25700/interdroid.swan D/BeaconSingleton: On beacon connect+++++++++++++++++ 
05-12 13:36:26.224 25700-29401/interdroid.swan D/BeaconSingleton: ++ 6 

正如您在logcat中所看到的,調用第二個綁定後,範圍通知器上的調用非常罕見。

如果有人遇到類似的問題,任何幫助表示讚賞。

+0

這將是很好的在你的問題來發表您的代碼的簡化版本,以便其他人可以看到很容易。基於查看鏈接,我看到bind()被重複調用單個實例的同一個實例。這不應該做。應該只有一個調用來綁定每個對象實例。你不應該再次調用bind()而不先調用unbind() – davidgyoung

+0

你好,我從我的服務中發佈了相關的方法。正如你所看到的,當有至少一個AbstractBeaconSensor時,我會調用綁定,如果沒有,我會調用unbind()。即使綁定和onBeaconConnect被正確調用,Altbeacon也不會像第一次那樣快速地掃描... – Veaceslav

回答

1

目前尚不清楚應用程序的生命週期是什麼序列,但有些東西會導致庫在第二次bind()調用時認爲它處於後臺模式。您可以通過添加下面的行onBeaconServiceConnect()糾正這一點,

beaconManager.setBackgroundMode(false); 
+0

謝謝,現在它可以工作。我也不明白爲什麼圖書館進入後臺模式... – Veaceslav