1

我正在使用iBeacon技術,並且我正在嘗試創建一個RangingService,它將每隔5秒搜索附近的iBeacons並在我的應用程序的後臺運行。下面的代碼不起作用。我確信我在某個地方犯了一些愚蠢的錯誤,但我可以在日誌文件中看到Checkpoint 3和4每5秒鐘到達一次,而檢查點1和2永遠不會到達。因此,附近的信標沒有被檢測到。我沒有太多的服務或信標經驗,所以我會很感激任何幫助,特別是@davidgyoung。iBeacon測距服務不返回任何信標

如果下面的代碼沒有完全縮進,請原諒我:)非常感謝任何能夠幫助我的人。

import android.app.Service; 
import android.content.Intent; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.Message; 
import android.os.RemoteException; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.Toast; 

import org.altbeacon.beacon.Beacon; 
import org.altbeacon.beacon.BeaconConsumer; 
import org.altbeacon.beacon.BeaconManager; 
import org.altbeacon.beacon.RangeNotifier; 
import org.altbeacon.beacon.Region; 

import java.util.Collection; 

public class RangingService extends Service implements BeaconConsumer { 
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); 
Handler handler; 
String b = ""; 

@Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

@Override 
public void onStart(Intent intent, int startId) { 
    // Let it continue running until it is stopped. 
    Log.d("Service", "Started"); 

    handler = new Handler(){ 

     @Override 
      public void handleMessage(Message msg) { 
       // TODO Auto-generated method stub 
       super.handleMessage(msg); 
       Log.d("Checkpoint", "5 seconds have passed"); 
      } 

     }; 

     new Thread(new Runnable(){ 
      public void run() { 
       // TODO Auto-generated method stub 
       while(true) 
       { 
        try { 
         startJob(); 
         Log.d("Checkpoint", "Job has started"); 
         Thread.sleep(5000); 
         handler.sendEmptyMessage(0); 

        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       } 

      } 
     }).start(); 




     //return START_STICKY; 
    } 

public void startJob() { 
    beaconManager.bind(this); 
    Log.d("The first beacon", "Starting job for realzies"); 
    onBeaconServiceConnect(); 
} 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d("Service", "Ended"); 
    } 

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

      Log.d("Checkpoint1", "Checkpoint1"); 
      if (beacons.size() > 0) { 
       Log.d("Checkpoint2", "Checkpoint2"); 
       //EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText); 
       Beacon firstBeacon = beacons.iterator().next(); 
       String a = "The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away. RSSI = " + firstBeacon.getRssi(); 
       Log.d("Service", a); 
       //logToDisplay(a); 
      } 
     } 

    }); 

    try { 
     beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null)); 
     Log.d("Checkpoint4", "Checkpoint4"); 

    } catch (RemoteException e) { } 
} 
}// Update code formatting 

主要活動:

public void didEnterRegion(Region arg0) { 
    // In this example, this class sends a notification to the user whenever a Beacon 
    // matching a Region (defined above) are first seen. 
    Log.d(TAG, "did enter region."); 
    startService(new Intent(this, RangingService.class)); 

} 

回答

0

一些提示:

  1. 不要手動調用onBeaconServiceConnect();。這是一個回叫方法,當信標掃描服務隨時可以通過撥打beaconManager.bind(this);時,由Android Beacon庫調用。如果您自己調用它,則會導致其失敗並導致問題。

  2. 您不應該每五秒鐘撥打一次beaconManager.bind(this); - 只需在服務啓動時調用一次即可。反覆調用會導致問題。

  3. 確保Android信標庫配置了您正在使用的信標類型。默認情況下,它只會檢測開源的AltBeacon數據包。如果您使用的是專有信標類型,則需要創建一個BeaconParser並添加對庫進行配置。您可以執行谷歌搜索「BeaconParser」和專有信標類型以獲取適當的代碼行來配置庫。

+0

非常感謝。我做了所有這三項修改,現在我的代碼已經到達了檢查點1和2,但是它只檢測一次信標。我希望它每五秒鐘收到一次信標(距離)的位置。我怎樣才能實現這個?再次感謝 - 我真的很感激它。 –

+0

你能否請創建一個新的問題,並從這裏鏈接到它?你遇到的問題已經發生了很大的變化,代碼也基於你描述的變化。 – davidgyoung

+0

http://stackoverflow.com/questions/35702632/ibeacon-ranging-service-not-returning-any-beacons-part-2謝謝! –