如何在manuvally中设置iBeacon扫描时间

时间:2016-06-03 10:03:19

标签: android android-ibeacon

我正在使用AltBeacon Library。我想在5分钟和1秒内扫描信标。我的代码在第一次和第二次正确扫描时没有正确扫描。请帮帮我?

//这是我的片段类

public class LocationIdFragment extends Fragment {  


@Override
public void onStart() {
    super.onStart();      
    Intent intent = new Intent(getActivity(), SimpleService.class);
    getActivity().getApplicationContext().startService(intent);

}

@Override
public void onResume() {
    super.onResume();
    Intent intent = new Intent(getActivity(), SimpleService.class);
    getActivity().getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);


}

@Override
public void onPause() {
    super.onPause();        
        getActivity().getApplicationContext().unbindService(mConnection);
        mBound = false;

}

@Override
public void onStop() {
    super.onStop();
    Intent intent = new Intent(getActivity(), SimpleService.class);
    getActivity().getApplicationContext().stopService(intent);

}      

}

//这是我的服务类

public class SimpleService extends Service implements BeaconConsumer {


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

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

}

@Override
public void onCreate() {
    super.onCreate();       

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


        beaconManager.setForegroundScanPeriod(1000);//this is not scanning properly
        beaconManager.setBackgroundScanPeriod(1000);
        beaconManager.setForegroundBetweenScanPeriod(301000);
        beaconManager.setBackgroundBetweenScanPeriod(301000);
        beaconManager.bind(SimpleService.this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY; 
    }

@Override
public void onDestroy() {

    try {
        // Unbind scan beacon progress
        if (beaconManager != null) {
            beaconManager.unbind(SimpleService.this);
            beaconManager.stopRangingBeaconsInRegion(new Region("sBeacon", null, null, null));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    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();          
                getMajor = beacon.getId2().toString();//here i'm getting data
                getMinor = beacon.getId3().toString();
                beaconID = getMajor + "-" + getMinor;

                DateFormat df = new SimpleDateFormat("dd-MM-yyyy/hh:mm:ss.mmm");// i'm getting different time
                String sDate = df.format(Calendar.getInstance().getTime());
                String[] sDateTime = sDate.split("/");                  


            }                
        }

    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("sBeacon", null, null, null));
        Log.i(TAG, "*** startRangingBeaconsInRegion ***");
        Logger.info(" LocationIdFragment : Start Ranging BeaconsInRegion ");
    } catch (RemoteException e) {
        Log.i(TAG, "RemoteException: " + e);
    }
} 

}

0 个答案:

没有答案