2017-05-09 10 views
0

我試圖玩弄燈塔和我有這樣的代碼: 通過以下this教程:如何檢查,如果沒有信標可用的Android

Activity2.java

public class Activity2 extends AppCompatActivity { 

    //String myString = MyViewClass.infoToPass; 

    public static final String TAG = "Airport"; 
    TextView tvFooter; 

    private static final Map<String, List<String>> PLACES_BY_BEACONS; 

    // TODO: replace "<major>:<minor>" strings to match your own beacons. 
    static { 
     Map<String, List<String>> placesByBeacons = new HashMap<>(); 
     placesByBeacons.put("22504:48827", new ArrayList<String>() {{ 
      add("Heavenly Sandwiches"); 
      // read as: "Heavenly Sandwiches" is closest 
      // to the beacon with major 22504 and minor 48827 
      add("Green & Green Salads"); 
      // "Green & Green Salads" is the next closest 
      add("Mini Panini"); 
      // "Mini Panini" is the furthest away 
     }}); 
     placesByBeacons.put("648:12", new ArrayList<String>() {{ 
      add("Mini Panini"); 
      add("Green & Green Salads"); 
      add("Heavenly Sandwiches"); 
     }}); 
     PLACES_BY_BEACONS = Collections.unmodifiableMap(placesByBeacons); 
    } 

    private List<String> placesNearBeacon(Beacon beacon) { 
     String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor()); 
     if (PLACES_BY_BEACONS.containsKey(beaconKey)) { 
      return PLACES_BY_BEACONS.get(beaconKey); 
     } 
     return Collections.emptyList(); 
    } 

    private BeaconManager beaconManager; 
    private Region region; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_2); 

     //updateCordinate("Just some random text"); 

     Log.d(TAG, "onCreate"); 

     beaconManager = new BeaconManager(this); 
     beaconManager.setRangingListener(new BeaconManager.RangingListener() { 
      @Override 
      public void onBeaconsDiscovered(Region region, List<Beacon> list) { 
       Log.d(TAG, "onBeaconsDiscovered"); 
       if (!list.isEmpty()) { 
        Beacon nearestBeacon = list.get(0); 
        List<String> places = placesNearBeacon(nearestBeacon); 
        // TODO: update the UI here 
        Log.d(TAG, "Nearest places: " + places); 
       } 
       else{ 
        // this line is not executing 
        Log.d(TAG, "You are not in area of beacons"); 
       } 
      } 
     }); 
     region = new Region("ranged region", UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(TAG, "onResume"); 
     SystemRequirementsChecker.checkWithDefaultDialogs(this); 

     beaconManager.connect(new BeaconManager.ServiceReadyCallback() { 
      @Override 
      public void onServiceReady() { 
       Log.d(TAG, "onServiceReady"); 
       beaconManager.startRanging(region); 
      } 
     }); 
    } 

    @Override 
    protected void onPause() { 
     beaconManager.stopRanging(region); 
     Log.d(TAG, "onPause"); 
     super.onPause(); 
    } 


    //public void updateCordinate(String text1) { 
    // tvFooter = (TextView) findViewById(R.id.tvFooter); 
    // tvFooter.setText("Location is :" + myString); 
    // } 
} 

現在我想要什麼:

  • 如果我周圍沒有任何燈塔,我想檢查它並將該值存儲在某個變量中我可以在其他課程(非活動課)中使用它。
  • 另外我該如何檢查它的範圍? (我知道這是超出了這個問題,但很少知道的會受到歡迎的範圍)
+0

是不是你的班級應該是Service的延伸。你需要爲信標服務。例如。 public class EstimoteService extends Service實現了BeaconManager.RangingListener,BeaconManager.MonitoringListener,然後在你的activity中調用這個startService(new Intent(this,EstimoteService.class));當然你需要確保你的藍牙已經打開並且有權限。 – kggoh

+0

你好,感謝您的評論。那麼我有其他類:'MyApplication.java'。我從[Here]下載GitHub項目(https://github.com/wondek/estimote-android-tutorial-ea99e83c) – fWd82

回答

0

//你可能忽略的通知代碼,爲我的信號燈的使用在我的應用程序,我 //發送通知即使通過應用程序在我的應用程序沒有打開,當一個燈塔/ /進出範圍。

public class EstimoteService extends Service implements BeaconManager.RangingListener, BeaconManager.MonitoringListener { 
    private static final int NOTIFICATION_ID = 123; 
    private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("regionId", null, null, null); 
    private Beacon beaconFound; 

    private NotificationManager notificationManager; 
    private BeaconManager beaconManager; 

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

     initResources(); 
    } 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     notificationManager.cancel(NOTIFICATION_ID); 

     // Default values are 5s of scanning and 25s of waiting time to save CPU cycles. 
     // In order for this demo to be more responsive and immediate we lower down those values. 
     beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0); 
     beaconManager.connect(new BeaconManager.ServiceReadyCallback() { 
      @Override 
      public void onServiceReady() { 
       try { 
        beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION); 
       } catch (RemoteException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 


     return START_NOT_STICKY; 
    } 

    private void initResources() { 
     notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     beaconManager = new BeaconManager(this); 
     beaconManager.setRangingListener(this); 
     beaconManager.setMonitoringListener(this); 
    } 

    @Override 
    public void onBeaconsDiscovered(Region region, List<Beacon> beacons) { 
     if (beaconFound == null) { 
      // Get the first available beaconFound 
      if (beacons.isEmpty()) 
       return; 

      beaconFound = beacons.get(0); 
      final Region beaconRegion = new Region("regionId", beaconFound.getProximityUUID(), beaconFound.getMajor(), beaconFound.getMinor()); 
      beaconManager.connect(new BeaconManager.ServiceReadyCallback() { 
       @Override 
       public void onServiceReady() { 
        try { 
         beaconManager.startMonitoring(beaconRegion); 
        } catch (RemoteException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } 
    } 

    @Override 
    public void onEnteredRegion(Region region, List<Beacon> beacons) { 
     postNotification("Entered region"); 
     System.out.println("---> enter region"); 
    } 

    @Override 
    public void onExitedRegion(Region region) { 
     postNotification("Exited region"); 
     System.out.println("---> exit region"); 
    } 

    private void postNotification(String msg) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setSmallIcon(R.drawable.beacon_gray).setContentTitle("XXX").setContentText(msg); 
     notificationManager.notify(NOTIFICATION_ID, builder.build()); 
    } 
} 
+0

你好@kggoh謝謝你的寶貴回答。我只是在'beaconFound'和'imports'中遇到問題。如果您請發佈完整的代碼,我會很喜歡。你的代碼看起來非常棒,但是我很難整合我的代碼。謝謝。 – fWd82

+1

按要求。這裏是。 – kggoh

+0

您好@kggoh非常感謝您對我的回答和特殊編輯。當我學習了這麼多的代碼,現在我很困惑。上面的類「EstimoteService」類沒有錯誤。但我現在很困惑如何訪問它。我將它包含在我的項目中,並沒有顯示任何錯誤。你能告訴我如何使它工作。 :( – fWd82

相關問題