2016-09-22 5 views
0

これは正しい方法ですか?だから、基本的に周りのビーコンを聞いていたアプリケーションを拡張し、そのクラスを、このサンプル 私のアンドロイドをビーコンにチューニングして検出する

public class App extends Application 
    implements BootstrapNotifier, BeaconConsumer, RangeNotifier { 
private final String TAG = "Application "; 
protected static final Region beaconRegion = new Region("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", null, null, null); 
protected BeaconManager beaconManager = null; 
private RegionBootstrap regionBootstrap; 
private BackgroundPowerSaver backgroundPowerSaver; 
protected static String sLog = ""; 
@Override 
public void onCreate() { 
    super.onCreate(); 
    logIt(TAG, beaconRegion.getId1()+"onCreate - In"+beaconRegion.getUniqueId()); 
    beaconManager = org.altbeacon.beacon.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"));//iBeacon (tutti) 
    //--- wake up the app when a beacon is seen 
    regionBootstrap = new RegionBootstrap(this, beaconRegion); 
    //--- activate power saver 
    backgroundPowerSaver = new BackgroundPowerSaver(this); 
    beaconManager.bind(this); 
    logIt(TAG, "onCreate - Out"); 
} 
private void logIt (String TAG, String msg) { 
    sLog += TAG + msg + "\n"; 
    Log.w(TAG, msg); 
} 
//-------------------------// 
//--- BootstrapNotifier ---// 
//-------------------------// 
@Override 
public void didDetermineStateForRegion(int state, Region region) { 
    String msg = "didDetermineStateForRegion "; 
    switch(state) { 
     case MonitorNotifier.INSIDE: 
      msg +="(INSIDE)"; 
      break; 
     case MonitorNotifier.OUTSIDE: 
      msg +="(OUTSIDE)"; 
      break; 
     default: 
      msg +="(state=" +state +")"; 
      break; 
    } 
    logIt(TAG, msg); 
} 
@Override 
public void didEnterRegion(Region arg0) { 
    logIt(TAG, "didEnterRegion - In"); 
    try { 
     beaconManager.startRangingBeaconsInRegion(beaconRegion); 
     logIt(TAG,"dER - startRangingBeaconsInRegion OK"); 
    } catch (RemoteException e) { 
     logIt(TAG, "dER - startRangingBeaconsInRegion Err " +e); 
    } 
    logIt(TAG, "didEnterRegion - Out"); 
} 
@Override 
public void didExitRegion(Region region) { 
    logIt(TAG, "didExitRegion - In"); 
    try { 
     beaconManager.stopRangingBeaconsInRegion(beaconRegion); 
     logIt(TAG,"dXR - stopRangingBeaconsInRegion OK"); 
    } catch (RemoteException e) { 
     logIt(TAG, "dXR - stopRangingBeaconsInRegion Err " +e); 
    } 
    logIt(TAG, "didExitRegion - Out"); 
} 
//----------------------// 
//--- BeaconConsumer ---// 
//----------------------// 
@Override 
public void onBeaconServiceConnect() { 
    logIt(TAG, "onBeaconServiceConnect - In"); 
    beaconManager.setRangeNotifier(this); 
    logIt(TAG, "onBeaconServiceConnect - Out"); 
} 
//---------------------// 
//--- RangeNotifier ---// 
//---------------------// 
@Override 
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
    logIt(TAG, "didRangeBeaconsInRegion - " +beacons.size() +" beacons"); 
    Toast.makeText(getApplicationContext(), beaconRegion.getId1()+" beacon detected "+beacons.size(), 
      Toast.LENGTH_SHORT).show(); 
    for(Beacon beac: beacons) 
    { 
     System.out.println(beac.getId1()+"id 1"+TAG); 
     if(beac.getId1().equals("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")/send notification 
    } 
} 
} 

https://altbeacon.github.io/android-beacon-library/samples.htmlを使用しています。以下は、私の携帯電話をビーコンに変える方法です。ボタンをクリックするだけで、これをやっています。だから私はアプリケーションのクラスを拡張して実装して以来、私は他の携帯電話でそれを検出したいボタンを1つのアプリケーションのボタンをクリックすると、両方のアプリケーションをダウンロードして2つの携帯電話があります。

ビーコンコードにアンドロイドを回します。

   Beacon beacon = new Beacon.Builder() 
         .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6") // UUID for beacon 
         .setId2("1") // Major for beacon 
         .setId3("5") // Minor for beacon 
         .setManufacturer(0x004C) // Radius Networks.0x0118 Change this for other beacon layouts//0x004C for iPhone 
         .setTxPower(-56) // Power in dB 
         .setDataFields(Arrays.asList(new Long[]{0l})) // Remove this for beacon layouts without d: fields 
         .build(); 
       BeaconParser beaconParser = new BeaconParser() 
         .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"); 
       beaconTransmitter = new BeaconTransmitter(MenuActivity.this, beaconParser); 
       beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() { 
        @Override 
        public void onStartFailure(int errorCode) { 
         Log.e("tag", "Advertisement start failed with code: " + errorCode); 
        } 
        @Override 
        public void onStartSuccess(AdvertiseSettings settingsInEffect) { 
         Log.i("tag", "Advertisement start succeeded."); 
         Toast.makeText(MenuActivity.this, "advertisement start succeeded", 
           Toast.LENGTH_SHORT).show(); 
         System.out.println("startedddddddddddd"); 
        } 
       }); 
       // beaconTransmitter.stopAdvertising(); 
      } 
      catch(Exception o) 
      { 
       System.out.println("affda "+o.getMessage()); 
      } 

私も問題をhavigています、そのdidenterregionとdidRangeBeaconsInRegionは何度も発射されるので、ユーザーに複数回、多くの通知を送信しています。そのユーザーフレンドリーではありません。

+0

はそれを説明しようとしているあなた自身の質問にコメントを投稿しないでください。質問に追加する情報がさらにある場合は、質問を編集して追加してください。 – azurefrog

答えて

2

didRangeBeaconsInRegionコールバックは何度も呼ばれることになっています。これがレンジングの仕組みです。これは、ビーコンが検出されたときに1秒間に約1回呼び出され、ビーコンが存在していることを伝え、距離の見積りを行います。

このメソッドを初めて呼び出すときに通知を1回だけ発生させたい場合は、この特定のビーコンのフラグを設定できます。ここで

は、あなたがそれを行うために使用するかもしれないいくつかのコードの例です。

// Add this to the top of your class 
private HashMap<String,Boolean> mAlreadySentNotification = new HashMap<String,Boolean>(); 

... 

// Add this to the inside of your didRangeBeaconsInRegion method 
if (mAlreadySentNotification.get(beacon.toString())) { 
    mAlreadySentNotification.put(beacon.toString(), true); 
    // Send notification here. 
} 
+0

答えをありがとう。あなたの解決策は良いですが、バグがあります。例えば、ビーコン1を検出しました。このビーコン1をtrueに設定して、通知を1回だけ送信しました。しかし、ビーコン1を偽に戻す必要があるので、30分後、または1日後などに通知を再度送信しましたか?そのような場合はどうすれば処理できますか? –

関連する問題