これは正しい方法ですか?だから、基本的に周りのビーコンを聞いていたアプリケーションを拡張し、そのクラスを、このサンプル 私のアンドロイドをビーコンにチューニングして検出する
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は何度も発射されるので、ユーザーに複数回、多くの通知を送信しています。そのユーザーフレンドリーではありません。
はそれを説明しようとしているあなた自身の質問にコメントを投稿しないでください。質問に追加する情報がさらにある場合は、質問を編集して追加してください。 – azurefrog