2016-11-11 43 views
0

我正在使用Alt信標庫進行信標掃描。我正在掃描信標服務類。我想繼續掃描信標,即使應用程序關閉也。但如果關閉應用程序服務被採空和信標不scanning.I試過,但我不getting.please幫助我,感謝
// MainActivity如何從服務發送數據,甚至關閉應用程序

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
setContentView(R.layout.activity_main); 

    Intent intent = new Intent(MainActivity.this, ScaningService.class); 
    startService(intent);//starting service 
    getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//bounding service 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 

} 

private ServiceConnection mConnection = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName className, 
            IBinder service) {   


    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 


    } 
}; 

//服務類

public class ScaningService extends Service implements BeaconConsumer { 
public BeaconManager beaconManager; 
public MainActivity activity; 

private final IBinder mBinder = new SimpleServiceBinder(); 


public class SimpleServiceBinder extends Binder { 
    public ScaningService getService() { 
     return ScaningService.this; 
    } 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 


@Override 
public void onCreate() { 
    super.onCreate(); 
    handler = new Handler(); 
    beaconManager = BeaconManager.getInstanceForApplication(getBaseContext()); 
    beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); 
    beaconManager.setBackgroundMode(true); 
    beaconManager.setBackgroundScanPeriod(1100);//this will set how long a bluetooth should scan 
    beaconManager.setBackgroundBetweenScanPeriod(2000);//this will set bluetooth scanning interval   
    beaconManager.bind(ScaningService.this); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY; 
} 

@Override 
public boolean onUnbind(Intent intent) { 
    return super.onUnbind(intent); 
} 

@Override 
public void onDestroy() {  
    super.onDestroy(); 
} 

@Override 
public void onBeaconServiceConnect() { 

    beaconManager.setRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 

      if (beacons.size() > 0) { 

       beacon = beacons.iterator().next(); 
       Log.e(TAG, " UUID: " + beacon.getId1());// this is not showing and i tried toast also     
     } 
    }); 

    try {   

     beaconManager.startRangingBeaconsInRegion(new Region("sBeacon", null, null, null));    

    } catch (RemoteException e) { 
     Log.i(TAG, "RemoteException: " + e); 
    } 
} 

}

+0

使用背景'服務' –

+0

[圖書館的文件](https://altbeacon.github.io/android-beacon-library/documentation.html)也談到後臺操作。 –

+0

請[編輯]你的問題,以顯示[你到目前爲止嘗試過的](http://whathaveyoutried.com)。您應該包含您遇到問題的代碼[mcve],然後我們可以嘗試幫助解決特定問題。你還應該閱讀[問]。 –

回答

1

是的,這是可能的。正確配置後,Android Beacon Library將在應用程序被殺後五分鐘內恢復在後臺掃描。在測距回調中,雖然您可能需要在新線程(使用Handler或類似結構)上執行此操作,但可能會將數據發送到服務器。

上述項目網站上的示例應該讓你有很多的方式。您將需要結合後臺啓動和自定義Application類中的測距。

編輯:看到代碼發佈後,顯然問題是掃描是從自定義服務觸發的。雖然這可以起作用,但要使其正常工作非常困難,所以掃描會在應用程序被終止時重新啓動。遵循使用RegionBootstrap來觸發使用自定義Application類的背景檢測的示例要簡單得多。