2017-05-12 9 views
0
내가 서비스 클래스를 확장하는 클래스에 라이브러리 albeacon 내 표지를 스캔하고

altbeacon

public class BeaconActivity extends Service implements BeaconConsumer{} 

내 질문은 다음과 같습니다

1 곳/정지 시작 이 서비스? 나는 응용 프로그램 스캔을

startService(new Intent(getApplicationContext(), BeaconActivity.class)); 
stopService(new Intent(getApplicationContext(), BeaconActivity.class)); 

2 내가/시작할 수있는 모든 시간을 원하는 경우 일부 버튼를 사용하여이 서비스를 중지?

public class BeaconActivity extends Service implements BeaconConsumer { 

BeaconManager beaconManager; 
private static final String UUID1 = "B9407F30-F5F8-466E-AFF9-25556B57FE6D"; 
private static final String Major1 = "14152"; 
public static boolean isInBeaconRegion =false; 
int value, id,distance; 
String beaconMinor; 


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


    beaconManager = BeaconManager.getInstanceForApplication(this); 
    beaconManager.getBeaconParsers().clear(); 
    beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 

    // 5 seconds 
    beaconManager.setBackgroundBetweenScanPeriod(8000l); 
    beaconManager.setForegroundBetweenScanPeriod(8000l); 

    beaconManager.bind(this); } 

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

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // -- continue running until it is stopped -- 
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
    return START_STICKY; } 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); } 

@Override 
public void onBeaconServiceConnect() { 

    beaconManager.addRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) { 
      for (int i = 0; i < collection.size(); i++) { 

       final Beacon beacon = collection.iterator().next(); 

       // -- Get Beacon UUID -- 
       String UUID2 = beacon.getId1().toString().toUpperCase(); 
       // -- Get Beacon Minor -- 
       String Major2 = beacon.getId2().toString(); 

       if (UUID2.equals(UUID1) && Major2.equals(Major1)) { 

        // -- Get Beacon Minor -- 
        beaconMinor = beacon.getId3().toString(); 
        //Check the minor 

       } } } 

    }); 

    try { 
     beaconManager.startRangingBeaconsInRegion(new Region("B9407F30-F5F8-466E-AFF9-25556B57FE6D", null, null, null)); 
    } catch (RemoteException e) { 
    } } 

답변

0

몇 가지 팁 : 시작 버튼은 응용 프로그램 시작 스캔을 누르면 ...

내 클래스입니다

  1. Android Beacon Library 이미 쉽게 검색 및 않는 자신의 서비스를 제공 항상 스캔 할 수 있습니다. 이를 설정하는 방법을 볼 수 있습니다. here. 결과적으로 자체 스캐닝 서비스를 만드는 것이 반드시 필요한 것은 아닙니다. 당신이 당신의 자신의 서비스를 작성하려는 경우

  2. , 당신은 시작하고이 같은 버튼 내부를 중지 할 수 있습니다

    private boolean mServiceStarted = false; 
    public View.OnClickListener startButtonTapped = new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (!mServiceStarted) { 
          Intent intent = new Intent(getApplicationContext(), BeaconActivity.class); 
          startService(intent); 
          mServiceStarted = true; 
         } 
        } 
    }; 
    public View.OnClickListener stopButtonTapped = new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (mServiceStarted) { 
          Intent intent = new Intent(getApplicationContext(), BeaconActivity.class); 
          stopService(intent); 
          mServiceStarted = false; 
         } 
        } 
    }; 
    
  3. 당신은 BeaconScanningService "와 같은 뭔가 클래스"BeaconActivity을 "이름을 바꿀 수 있습니다 ",이 방식으로 안드로이드 서비스 클래스를 명명하는 것은 표준 규칙이기 때문에.

+0

입니다. 그러나 사용자가 (스캔 시작)을 누르고 그 주변에 비컨이 없으면 어떻게 될까요? 어디에서 토스트 경고를 할 수 있습니까? - 내 수업이 붙어 있어요. – Abrar