@SuppressLint("MissingPermission") // We check before this is called
    public boolean connect(final String address) {

        final boolean retVal;
        if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            retVal = false;

            // Previously connected device.  Try to reconnect.
        } else if (mBluetoothDeviceAddress != null
                && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {

            Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
            if (mBluetoothGatt.connect()) {
                Log.d(TAG, "Connection attempt OK.");
                setConnectionState(State.CONNECTING, true);
                retVal = true;
            } else {
                Log.w(TAG, "Connection attempt failed.");
                setConnectionState(State.DISCONNECTED, true);
                retVal = false;
            }
        } else {

            final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
            if (device == null) {
                Log.w(TAG, "Device not found.  Unable to connect.");
                retVal = false;
            } else {
                // We want to directly connect to the device, so we are setting the autoConnect
                // parameter to false.

                Log.d(TAG, "Trying to create a new connection.");
                mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
                mBluetoothDeviceAddress = address;
                setConnectionState(State.CONNECTING, true);
                retVal = true;
            }
        }

        return retVal;
    }
Connects to the GATT server hosted on the Bluetooth LE device.

@param address The device address of the destination device.
@return Return true if the connection is initiated successfully. The connection result
is reported asynchronously through the
{@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
callback.
sample_app/src/main/java/uk/co/alt236/btlescan/services/BluetoothLeService.java