2015-11-30 62 views
0

我有來自Radius網絡的2個Rad信標。我使用Locate應用程序將它們配置爲Eddystone。現在我寫了一個小程序在後臺發送通知,即應用程序未運行時。我需要在應用程序處於後臺時發送通知。我正在使用Android信標庫來實現此目的。我嘗試了幾乎所有的鏈接,但我無法檢測到它。無法在Android的背景中檢測到我的信標

我在這裏

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier { 
    private static final String TAG = "BeaconReferenceApp"; 
    private RegionBootstrap regionBootstrap; 
    private BackgroundPowerSaver backgroundPowerSaver; 
    private boolean haveDetectedBeaconsSinceBoot = false; 
    private MonitoringActivity monitoringActivity = null;  

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

     BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); 

     beaconManager.getBeaconParsers().clear(); 
     beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); 
     beaconManager.setBackgroundBetweenScanPeriod(1000); 

     Log.i(TAG, "setting up background monitoring for beacons and power saving"); 

     //Toast.makeText(getApplicationContext(), "Called!!!" , Toast.LENGTH_LONG).show(); 

     // wake up the app when a beacon is seen 
     Region region = new Region("backgroundRegion", null, null, null); 

     regionBootstrap = new RegionBootstrap(this, region); 

     // simply constructing this class and holding a reference to it in your custom Application 
     // class will automatically cause the BeaconLibrary to save battery whenever the application 
     // is not visible. This reduces bluetooth power usage by about 60% 
     //backgroundPowerSaver = new BackgroundPowerSaver(this); 

     // If you wish to test beacon detection in the Android Emulator, you can use code like this: 
     // BeaconManager.setBeaconSimulator(new TimedBeaconSimulator()); 
     // ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons(); 
    } 

    @Override 
    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.i(TAG, "did enter region."); 

     //sendNotification(); 

     if (!haveDetectedBeaconsSinceBoot) { 
      Log.i(TAG, "auto launching MainActivity"); 

      // The very first time since boot that we detect an beacon, we launch the 
      // MainActivity 
      Intent intent = new Intent(this, MonitoringActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      // Important: make sure to add android:launchMode="singleInstance" in the manifest 
      // to keep multiple copies of this activity from getting created if the user has 
      // already manually launched the app. 
      this.startActivity(intent); 
      haveDetectedBeaconsSinceBoot = true; 
     } else { 
      if (monitoringActivity != null) { 
       // If the Monitoring Activity is visible, we log info about the beacons we have 
       // seen on its display 
       monitoringActivity.logToDisplay("I see a beacon again"); 
      } else { 
       // If we have already seen beacons before, but the monitoring activity is not in 
       // the foreground, we send a notification to the user on subsequent detections. 
       Log.i(TAG, "Sending notification."); 
       //sendNotification(); 
      } 
     }  
    } 

    @Override 
    public void didExitRegion(Region region) { 
     if (monitoringActivity != null) { 
      monitoringActivity.logToDisplay("I no longer see a beacon."); 
     } 
    } 

    @Override 
    public void didDetermineStateForRegion(int state, Region region) { 
     if (monitoringActivity != null) { 
      monitoringActivity.logToDisplay("I have just switched from seeing/not seeing beacons: " + state); 
     } 
    } 

    private void sendNotification() { 
     NotificationCompat.Builder builder = 
       new NotificationCompat.Builder(this) 
         .setContentTitle("Beacon Reference Application") 
         .setContentText("An beacon is nearby in application.") 
         .setSmallIcon(R.drawable.ic_launcher); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class)); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
         0, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 

     builder.setContentIntent(resultPendingIntent); 
     NotificationManager notificationManager = 
       (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(1, builder.build()); 
    } 

    public void setMonitoringActivity(MonitoringActivity activity) { 
     Log.i("Log", "TEST ONLY"); 

     this.monitoringActivity = activity; 
    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) { 
     // TODO Auto-generated method stub 

     sendNotification(); 

     //Log.i("Log", "TEST ONLY"); 
    } 
+0

更容易爲人們幫助你,如果他們有一些相關的**一段代碼**上工作 - 請告訴我們什麼你已嘗試到目前爲止 – 0X0nosugar

+0

我更新了我的代碼。請讓我知道是否可以做任何事情。 – Sangie

+0

我還沒有經驗的信標(還),對不起。但是我可以看到一方面使用NotificationCompat.Builder,另一方面使用NotificationManager(而不是NotificationManagerCompat)。那樣有用嗎? – 0X0nosugar

回答

0

粘貼我的代碼,你都在用,我可以說,你要掃描的AltBeacon不埃迪斯通的佈局。所以你應該改變(或添加)以下的信標佈局;

S:0-1 = FEAA,M:2-2 = 00,P:3-3:-41,I:4-13,I:14-19

beaconManager.getBeaconParsers().add(BeaconParser.EDDYSTONE_UID_LAYOUT); 

此外,您還可以查看以下有關使用AltBeacon庫埃迪斯通燈塔更多信息的鏈接;

順便說一句,你可以設置Locate應用程序只檢測埃迪斯通燈塔,這樣就可以縮小的問題,並查看是否你的代碼不工作或你的信。

+0

不,還是一樣的。我沒有在後臺通知。 – Sangie

+0

根據您的代碼,如果您可以在應用處於前景時檢測到信標,則您也應該在後臺檢測它們。你的代碼看起來沒問題。但是,清單文件可能會導致此錯誤。你可以分享嗎? –

0

我得到了同樣的問題,花了一天多的時間來解決這個問題。代碼沒有問題。

(作爲開發人員犯了一個愚蠢的錯誤)。

這將充分利用在一定的..

在我的測試設備我並未啓用位置services.so能不能接收燈塔背景mode..so啓用位置服務,在得到信號通知背景,對對方的前景並不需要此設置..

謝謝..

相關問題