List of usage examples for android.bluetooth BluetoothAdapter EXTRA_PREVIOUS_STATE
String EXTRA_PREVIOUS_STATE
To view the source code for android.bluetooth BluetoothAdapter EXTRA_PREVIOUS_STATE.
Click Source Link
From source file:is.hello.buruberi.bluetooth.stacks.android.NativeBluetoothStack.java
@Override @RequiresPermission(allOf = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, }) public Observable<Void> turnOn() { if (adapter == null) { return Observable.error(new ChangePowerStateException()); }// w ww . jav a 2s. c om final ReplaySubject<Void> turnOnMirror = ReplaySubject.createWithSize(1); final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR); final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (oldState == BluetoothAdapter.STATE_OFF && newState == BluetoothAdapter.STATE_TURNING_ON) { logger.info(LOG_TAG, "Bluetooth turning on"); } else if (oldState == BluetoothAdapter.STATE_TURNING_ON && newState == BluetoothAdapter.STATE_ON) { logger.info(LOG_TAG, "Bluetooth turned on"); applicationContext.unregisterReceiver(this); turnOnMirror.onNext(null); turnOnMirror.onCompleted(); } else { logger.info(LOG_TAG, "Bluetooth failed to turn on"); applicationContext.unregisterReceiver(this); turnOnMirror.onError(new ChangePowerStateException()); } } }; applicationContext.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); if (!adapter.enable()) { applicationContext.unregisterReceiver(receiver); return Observable.error(new ChangePowerStateException()); } return turnOnMirror; }
From source file:is.hello.buruberi.bluetooth.stacks.android.NativeBluetoothStack.java
@Override @RequiresPermission(allOf = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, }) public Observable<Void> turnOff() { if (adapter == null) { return Observable.error(new ChangePowerStateException()); }// ww w. j a v a 2 s . c o m final ReplaySubject<Void> turnOnMirror = ReplaySubject.createWithSize(1); final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR); final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (oldState == BluetoothAdapter.STATE_ON && newState == BluetoothAdapter.STATE_TURNING_OFF) { logger.info(LOG_TAG, "Bluetooth turning off"); } else if (oldState == BluetoothAdapter.STATE_TURNING_OFF && newState == BluetoothAdapter.STATE_OFF) { logger.info(LOG_TAG, "Bluetooth turned off"); applicationContext.unregisterReceiver(this); turnOnMirror.onNext(null); turnOnMirror.onCompleted(); } else { logger.info(LOG_TAG, "Bluetooth failed to turn off"); applicationContext.unregisterReceiver(this); turnOnMirror.onError(new ChangePowerStateException()); } } }; applicationContext.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); if (!adapter.disable()) { applicationContext.unregisterReceiver(receiver); return Observable.error(new ChangePowerStateException()); } return turnOnMirror; }
From source file:com.commontime.plugin.LocationManager.java
private void initBluetoothListener() { //check access if (!hasBlueToothPermission()) { debugWarn("Cannot listen to Bluetooth service when BLUETOOTH permission is not added"); return;//from w w w . ja v a 2s .c om } //check device support try { iBeaconManager.checkAvailability(); } catch (Exception e) { //if device does not support iBeacons an error is thrown debugWarn("Cannot listen to Bluetooth service: " + e.getMessage()); return; } if (broadcastReceiver != null) { debugWarn("Already listening to Bluetooth service, not adding again"); return; } broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); // Only listen for Bluetooth server changes if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); final int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR); debugLog("Bluetooth Service state changed from " + getStateDescription(oldState) + " to " + getStateDescription(state)); switch (state) { case BluetoothAdapter.ERROR: beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusNotDetermined"); break; case BluetoothAdapter.STATE_OFF: case BluetoothAdapter.STATE_TURNING_OFF: if (oldState == BluetoothAdapter.STATE_ON) beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusDenied"); break; case BluetoothAdapter.STATE_ON: beaconServiceNotifier.didChangeAuthorizationStatus("AuthorizationStatusAuthorized"); break; case BluetoothAdapter.STATE_TURNING_ON: break; } } } private String getStateDescription(int state) { switch (state) { case BluetoothAdapter.ERROR: return "ERROR"; case BluetoothAdapter.STATE_OFF: return "STATE_OFF"; case BluetoothAdapter.STATE_TURNING_OFF: return "STATE_TURNING_OFF"; case BluetoothAdapter.STATE_ON: return "STATE_ON"; case BluetoothAdapter.STATE_TURNING_ON: return "STATE_TURNING_ON"; } return "ERROR" + state; } }; // Register for broadcasts on BluetoothAdapter state change IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); cordova.getActivity().registerReceiver(broadcastReceiver, filter); }