0

フォアグラウンド/バックグラウンドでビーコンを検出している Android アプリがあります。デバイスのBluetoothをオフにする場合を除いて、すべて正常に動作します。この場合、OnExitRegion が呼び出されますが、ユーザーが何をしているのか本当にわからないので無視する必要がありますが、ビーコンから離れて Bluetooth を再度オンにすると、onExitRegion は再度呼び出されず、私がその地域を出たことを知りません。

これは私のコードの一部です。

public class MyApplication extends Application implements BootstrapNotifier {
public void onCreate() {
    super.onCreate();
    ...
    mBeaconManager = BeaconManager.getInstanceForApplication(this);
    mBeaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(Constants.BEACON_LAYOUT));
    mBeaconRegion = new Region(Constants.BEACON_BACKGROUND_REGION, Identifier.parse(Constants.BEACON_UDID), null, null);
    regionBootstrap = new RegionBootstrap(this, mBeaconRegion);
    backgroundPowerSaver = new BackgroundPowerSaver(this);
    mBeaconManager.setBackgroundScanPeriod(Constants.BEACON_BACKGROUND_SCAN_PERIOD);       
    mBeaconManager.setBackgroundBetweenScanPeriod(Constants.BEACON_BACKGROUND_BETWEEN_SCAN_PERIOD);
    mBeaconManager.setAndroidLScanningDisabled(true);
    ...

}

Bluetoothがオフまたはオンになっていることを検出するためのBroadcastReceiverを作成しようとしました

public class BluetoothBroadcastReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_OFF) {
            Log.w("BLUETOOTH", "Bluetooth is disconnected");
        } else {
            Log.w("BLUETOOTH", "Bluetooth is connected");
        }
    }
}
}

私が必要とするのは、ブルートゥースがオンのときに、このブロードキャストレシーバーをチェックインすることです。まだその地域にいる場合、または UI を変更しない場合です。

私の説明が十分に明確であることを願っています。

よろしくお願いします!

4

1 に答える 1

0

Bluetooth 無線がオフになっている場合、Android ビーコン ライブラリはビーコン領域を実際に離れたかどうかを検出できません。ただし、Bluetooth が再びオンになったときに終了動作をシミュレートするためにできることのアイデアを次に示します。

  1. 2 つのアプリケーション レベルの変数を保持します。

    Set<Region> regionsActive = new HashSet<Region>();
    Set<Region> regionsActiveWhenBluetoothDisabled = new HashSet<Region>();
    
  2. 変数にリージョンを追加/削除するためのコードをdidExitRegion追加します。didEnterRegionregionsActive

  3. Bluetoothがオフになっていることを検出したコードで、次のようにします。

    regionActiveWhenBluetoothDisabled = new HashSet(regionsActive);

  4. Bluetooth がオンになったコールバックを取得するコードで、10 秒程度のタイマーを開始します。このタイマーの最後に、次のようなものを実行します。

    for (Region region: regionsActiveWhenBluetoothDisabled) {
        if (!regionsActive.contains(region)) {
            // We know we are no longer in a region that we were in when bluetooth was last turned off
            // execute code to say we are out of this region
        }
    }
    
于 2015-03-18T21:59:59.187 に答える