Example usage for android.bluetooth BluetoothAdapter STATE_TURNING_OFF

List of usage examples for android.bluetooth BluetoothAdapter STATE_TURNING_OFF

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter STATE_TURNING_OFF.

Prototype

int STATE_TURNING_OFF

To view the source code for android.bluetooth BluetoothAdapter STATE_TURNING_OFF.

Click Source Link

Document

Indicates the local Bluetooth adapter is turning off.

Usage

From source file:ac.robinson.bettertogether.hotspot.HotspotManagerService.java

private void initialiseHotspots(@Nullable ConnectionOptions connectionOptions)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

    mHotspotMode = true;//from   w  w w  .j  a va2s .co  m

    if (CREATE_WIFI_HOTSPOT_SUPPORTED) {
        configureAndStartWifiHotspot(connectionOptions);
    }

    switch (mBluetoothAdapter.getState()) {
    case BluetoothAdapter.STATE_ON:
        configureAndStartBluetoothHotspot(connectionOptions);
        break;

    case BluetoothAdapter.STATE_TURNING_ON:
        break; // finish configuration in receiver

    case BluetoothAdapter.STATE_TURNING_OFF:
    case BluetoothAdapter.STATE_OFF:
    default:
        mBluetoothAdapter.enable(); // finish configuration in receiver
    }
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

public synchronized String debugString() {
    if (mBluetoothAdapter == null) {
        return "Not available";
    }/*from  w  w  w.  j  a v  a2 s.com*/
    StringBuilder b = new StringBuilder().append("BluetoothAdapter: ");
    switch (mBluetoothAdapter.getState()) {
    case BluetoothAdapter.STATE_ON:
        b.append("ON");
        break;
    case BluetoothAdapter.STATE_TURNING_ON:
        b.append("Turning on");
        break;
    case BluetoothAdapter.STATE_OFF:
        b.append("OFF");
        break;
    case BluetoothAdapter.STATE_TURNING_OFF:
        b.append("Turning off");
        break;
    default:
        b.append("Unknown state");
        break;
    }
    b.append("\n");
    b.append("ENABLED: ").append(mEnabled).append("\n");
    if (mServices.size() > 0) {
        b.append("ADVERTISING ").append(mServices.size()).append(" services\n");
    }
    if (mLeScanCallback != null) {
        b.append("SCANNING\n");
    }
    b.append("OnServiceReadCallbacks: ").append(mOnServiceReadCallbacks).append("\n");
    return b.toString();
}

From source file:git.egatuts.nxtremotecontroller.activity.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    final MainActivity self = this;
    super.onCreate(savedInstanceState);
    super.setActiveTheme(super.getPreferenceTheme());
    super.setContentView(R.layout.main_layout);
    toolbar = (Toolbar) super.findViewById(R.id.toolbar);
    this.setSupportToolbar();
    this.drawerFragment = (NavigationDrawerFragment) fragmentManager.findFragmentById(R.id.drawer_fragment);
    this.drawerFragment.setup(R.id.drawer_fragment, (DrawerLayout) super.findViewById(R.id.drawer_element),
            this.toolbar);
    this.appKillerReceiver = new AppKillerReceiver(this, new AppKillerListener() {
        @Override//from w ww.j a va2s  .c om
        public void onAppNeedsRestart(Context context, Intent intent) {
            self.selfDestroyed = true;
            self.finish();
        }
    });
    this.progressDialogOnDismiss = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            self.getShortProgressDialog().setOnDismissListener(null);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    self.replaceFragmentWith(self.selectedFragment, self.activeFragment);
                }
            }, 200);
        }
    };
    this.bluetoothEnableReceiver = new BluetoothEnableReceiver(this, new BluetoothEnableListener() {
        @Override
        public void onStateChange(Context context, Intent intent) {
            int state = (int) self.bluetoothEnableReceiver.getIntentData(intent);
            final BaseIndeterminateProgressDialog progress = self.getShortProgressDialog();
            Integer text = null;
            int hasToChange = 0;
            boolean hasToShow = false;

            /*
             *  We define if we have to change the fragment or we have to show a progress dialog.
             */
            switch (state) {
            case BluetoothAdapter.STATE_OFF:
                hasToChange = 2;
                break;
            case BluetoothAdapter.STATE_ON:
                hasToChange = 1;
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                hasToShow = true;
                text = R.string.bluetooth_disabling;
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                hasToShow = true;
                text = R.string.bluetooth_enabling;
                break;
            }

            /*
             *  Here we have to show a progress dialog indicating that the bluetooth state is changing.
             */
            if (hasToShow && text != null) {
                self.showingTime = System.currentTimeMillis();
                progress.show();
                progress.setText(text);
            } else if (hasToChange != 0) {
                if (hasToChange == 1)
                    self.selectedFragment = self.lastFragment;
                if (hasToChange == 2)
                    self.selectedFragment = new BluetoothFragment();
                if (!hasToShow && progress.isShowing()) {
                    progress.setOnDismissListener(self.progressDialogOnDismiss);
                    if (System.currentTimeMillis() - self.showingTime < 500) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                progress.dismiss();
                            }
                        }, 750);
                    } else {
                        progress.dismiss();
                    }
                }
            }
        }
    });
    this.rippleViewListener = new RippleView.AnimationFinishListener() {
        @Override
        public void onFinish() {
            self.startControlDevice(self.controlIntent);
        }
    };
}

From source file:ac.robinson.bettertogether.hotspot.HotspotManagerService.java

private void connectBluetoothHotspot(@NonNull ConnectionOptions options) {
    Log.d(TAG, "Attempting connection via Bluetooth");
    switch (mBluetoothAdapter.getState()) {
    case BluetoothAdapter.STATE_ON:
        Log.d(TAG, "Starting Bluetooth discovery");
        mBluetoothAdapter.cancelDiscovery();
        mBluetoothAdapter.startDiscovery();
        setBluetoothErrorTimeout();//from w ww .j a  v a  2 s. co  m
        break;

    case BluetoothAdapter.STATE_TURNING_ON:
        Log.d(TAG, "Waiting for Bluetooth to be enabled");
        break; // start discovery in receiver

    case BluetoothAdapter.STATE_OFF:
    case BluetoothAdapter.STATE_TURNING_OFF:
    default:
        Log.d(TAG, "Enabling Bluetooth / waiting for receiver to enable");
        mBluetoothAdapter.enable(); // start discovery in receiver
    }
}

From source file:com.evothings.BLE.java

private void reset(final CordovaArgs args, final CallbackContext cc) throws JSONException {
    mResetCallbackContext = null;//from  w  w  w .  ja  va  2s  .c  o m
    BluetoothAdapter a = BluetoothAdapter.getDefaultAdapter();
    if (mScanCallbackContext != null) {
        a.stopLeScan(this);
        mScanCallbackContext = null;
    }
    int state = a.getState();
    //STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF.
    if (state == BluetoothAdapter.STATE_TURNING_ON) {
        // reset in progress; wait for STATE_ON.
        mResetCallbackContext = cc;
        return;
    }
    if (state == BluetoothAdapter.STATE_TURNING_OFF) {
        // reset in progress; wait for STATE_OFF.
        mResetCallbackContext = cc;
        return;
    }
    if (state == BluetoothAdapter.STATE_OFF) {
        boolean res = a.enable();
        if (res) {
            mResetCallbackContext = cc;
        } else {
            cc.error("enable");
        }
        return;
    }
    if (state == BluetoothAdapter.STATE_ON) {
        boolean res = a.disable();
        if (res) {
            mResetCallbackContext = cc;
        } else {
            cc.error("disable");
        }
        return;
    }
    cc.error("Unknown state: " + state);
}

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

/**
 * Handles intents ACTION_CONNECTION_STATE_CHANGED, ACTION_STATE_CHANGED,
 * ACTION_BOND_STATE_CHANGED, ACTION_KEYBOARD_UPDATE_CONFIRMED.
 * <p/>/* w w  w . j a  va 2  s.  co m*/
 * [ACTION_STATE_CHANGED]
 * This action is used to keep track of ON/OFF state change on the system Bluetooth adapter.
 * The
 * purpose is to synchronize the local Bluetooth connectivity with system Bluetooth state.
 * <p/>
 * [ACTION_CONNECTION_STATE_CHANGED]
 * This action is used to keep track of the connection change on the target device. The purpose
 * is to synchronize the connection cycles of the local GATT connection and the system
 * Bluetooth
 * connection.
 * <p/>
 * [ACTION_BOND_STATE_CHANGED]
 * This action is used to keep track of the bond state change on the target device. The purpose
 * is to the connection cycles of the local GATT connection and the system Bluetooth
 * connection.
 * <p/>
 * [ACTION_KEYBOARD_UPDATE_CONFIRMED]
 * This action is used to receive the update confirmation from the user. The purpose is to
 * trigger DFU process.
 */
private void onHandleIntent(Context context, Intent intent) {
    final String action = intent.getAction();
    Log.d(TAG, "onHandleIntent: Received action: " + action);

    if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {

        if (!isBluetoothEnabled()) {
            Log.w(TAG, "onHandleIntent: Bluetooth connectivity not enabled");
            return;
        }

        // Match the connected device with the default keyboard name.
        Bundle extras = intent.getExtras();
        if (extras == null)
            return;
        final BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
        final int deviceConnectionState = extras.getInt(BluetoothAdapter.EXTRA_CONNECTION_STATE);

        Log.d(TAG, "onHandleIntent: " + device.getName() + " [" + device.getAddress() + "] change to state: "
                + deviceConnectionState);

        // Match the name of the target keyboard.
        if (!isTargetKeyboard(device))
            return;

        if (deviceConnectionState == BluetoothAdapter.STATE_CONNECTED) {
            // Prevent the second keyboard from using the service.
            if (isUpdateServiceInUse())
                return;

            obtainKeyboardInfo(device.getName(), device.getAddress());

            if (mDfuStatus != DFU_STATE_INFO_READY) {
                Log.w(TAG, "onHandleIntent: DFU preparation failed");
                changeDfuStatus(DFU_STATE_OBTAIN_INFO_ERROR);
                return;
            }

            showUpdateNotification();
        } else if (deviceConnectionState == BluetoothAdapter.STATE_DISCONNECTING) {
            handleGattDisconnection();
        }

    } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        final int adapterState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        if (adapterState == BluetoothAdapter.STATE_ON) {
            if (!isBluetoothEnabled())
                enableBluetoothConnectivity();
        } else if (adapterState == BluetoothAdapter.STATE_TURNING_OFF) {
            // Terminate update process and disable Bluetooth connectivity.
            disableBluetoothConnectivity();

            // Since BluetoothAdapter has been disabled, the callback of disconnection would not
            // be called. Therefore a separate clean-up of GATT connection is need.
            cleanUpGattConnection();
        }

    } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        Bundle extras = intent.getExtras();
        if (extras == null)
            return;
        final BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
        final int deviceBondState = extras.getInt(BluetoothDevice.EXTRA_BOND_STATE);

        Log.d(TAG, "onHandleIntent: state change on device " + device.getName() + " [" + device.getAddress()
                + "], bond state: " + deviceBondState);

        if (!isTargetKeyboard(device))
            return;

        if (deviceBondState == BluetoothDevice.BOND_NONE) {
            handleGattDisconnection();
        }

    } else if (ACTION_KEYBOARD_UPDATE_CONFIRMED.equals(action)) {
        dismissUpdateNotification();

        if (mDfuStatus != DFU_STATE_INFO_READY || mDfuStatus == DFU_STATE_UPDATING) {
            Log.w(TAG, "onHandleIntent: DFP preparation not ready or DFU is in progress. ");
            changeDfuStatus(DFU_STATE_UPDATE_ABORTED);
            return;
        }

        String keyboardName = intent.getStringExtra(EXTRA_KEYBOARD_NAME);
        String keyboardAddress = intent.getStringExtra(EXTRA_KEYBOARD_ADDRESS);
        if (!mKeyboardName.equals(keyboardName) || !mKeyboardAddress.equals(keyboardAddress)) {
            Log.w(TAG, "onHandleIntent: No DFU service associated with " + keyboardName + " [" + keyboardAddress
                    + "]");
            return;
        }

        Log.d(TAG, "onHandleIntent: Start update process on " + keyboardName + " [" + keyboardAddress + "]");
        changeDfuStatus(DFU_STATE_SWITCHING_TO_DFU_MODE);

    } else if (ACTION_KEYBOARD_UPDATE_POSTPONED.equals(action)) {
        dismissUpdateNotification();
        // TODO(mcchou): Update the preference when the Settings keyboard entry is available.
    }
}

From source file:org.deviceconnect.android.deviceplugin.health.fragment.HealthCareDeviceSettingsFragment.java

/**
 * ?????.//w  w w.  ja  v  a 2 s. c om
 * @param state Bluetooth??.
 */
public void stateChanged(int state) {
    if (mBluetoothSwitch != null) {
        if (state == BluetoothAdapter.STATE_ON) {
            mBluetoothSwitch.setChecked(true);
            mBluetoothSwitch.setEnabled(true);
        } else if (state == BluetoothAdapter.STATE_OFF) {
            mBluetoothSwitch.setChecked(false);
            mBluetoothSwitch.setEnabled(true);
        }
    }
    if (state == BluetoothAdapter.STATE_ON) {
        // ????
        mRootView.findViewById(R.id.text_bluetooth_settings).setVisibility(View.GONE);
        mRootView.findViewById(R.id.device_list_view).setVisibility(View.VISIBLE);
    } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
        if (mIsScanning) {
            onClickScanButton();
        }
        // Bluetooth OFF?
        mRootView.findViewById(R.id.text_bluetooth_settings).setVisibility(View.VISIBLE);
        mRootView.findViewById(R.id.device_list_view).setVisibility(View.GONE);
    }
}