Example usage for android.bluetooth BluetoothDevice connectGatt

List of usage examples for android.bluetooth BluetoothDevice connectGatt

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice connectGatt.

Prototype

public BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) 

Source Link

Document

Connect to GATT Server hosted by this device.

Usage

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

private boolean connectToKeyboard() {
    if (!isBluetoothEnabled() || !isUpdateServiceInUse()) {
        Log.w(TAG, "connectToKeyboard: Bluetooth connectivity not enabled or associated keyboard not found.");
        return false;
    }// ww w .  jav a2s.  c o  m

    final BluetoothDevice keyboard = mBluetoothAdapter.getRemoteDevice(mKeyboardAddress);
    if (keyboard == null) {
        Log.w(TAG, "connectToKeyboard: " + getKeyboardString() + " not found. Unable to connect.");
        return false;
    }

    Log.d(TAG, "connectToKeyboard: Trying to create a new connection to " + getKeyboardString());
    mBluetoothGattClient = keyboard.connectGatt(this, false, mGattCallback);
    changeGattState(GATT_STATE_CONNECTING);
    mGattOperationStatus = BluetoothGatt.GATT_SUCCESS;

    return true;
}

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java

/**
 * Connects to the BLE device with given address. This method is SYNCHRONOUS, it wait until the connection status change from {@link #STATE_CONNECTING} to {@link #STATE_CONNECTED_AND_READY} or an
 * error occurs.//from  w  w  w . java2 s .  c o m
 * 
 * @param address
 *            the device address
 * @return the GATT device
 */
private BluetoothGatt connect(final String address) {
    mConnectionState = STATE_CONNECTING;

    logi("Connecting to the device...");
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    BluetoothGatt gatt = device.connectGatt(this, false, normalCallback);

    // We have to wait until the device is connected and services are discovered
    // Connection error may occur as well.
    try {
        synchronized (mLock) {
            while (((mConnectionState == STATE_CONNECTING || mConnectionState == STATE_CONNECTED)
                    && mErrorState == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }

    mConnectionState = STATE_CONNECTING;
    refreshDeviceCache(gatt);
    gatt = device.connectGatt(this, false, mGattCallback);
    try {
        synchronized (mLock) {
            while (((mConnectionState == STATE_CONNECTING || mConnectionState == STATE_CONNECTED)
                    && mErrorState == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }
    return gatt;
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * 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.//from w w  w . j a  v a  2 s  .c om
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    //        if (mScanedList.get(address) == null) {
    //            Iterator<String> iter = mScanedList.keySet().iterator();
    //            while (iter.hasNext()) {
    //                Log.d(TAG, "scanned address = " + iter.next());
    //            }
    //            Log.w(TAG, "Received address = " + address);
    //            Log.w(TAG, "This device is not activated...... check device status");
    //            return false;
    //        }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);

    // Previously connected device.  Try to reconnect.
    if (bluetoothGatt != null) {
        close(address);
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection address = " + address);
        if (bluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    bluetoothGatt = device.connectGatt(this, false, mGattCallback);
    if (bluetoothGatt == null) {
        Log.w(TAG, "device.connectGatt failed");
        return false;
    }
    mConnectedBluetoothGattMap.put(address, bluetoothGatt);

    Log.d(TAG, "Trying to create a new connection.");
    mConnectionState = STATE_CONNECTING;
    return true;
}

From source file:com.umundus.service.NCallServiceOld.java

/**
 * 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.//from   w ww .  j  a  va 2 s .c  o m
 */
public boolean connect(final String address) {
    Log.d(TAG, "[*]Service-connect:" + address);

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

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;

    Log.d(TAG, "[*]Service-connect:" + address + ",state:" + mConnectionState);
    return true;
}

From source file:com.ec.android.module.bluetooth40.BluetoothLeService.java

/**
 * 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.//from w  ww.j  a  v a2  s . c  om
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    //address??mac??
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        Log.w(TAG, "address??mac??");
        return false;
    }
    ////FIXME
    /*if (mBluetoothGatt != null) {
    disconnect();
    close();
    }*/
    if (mBluetoothGatt != null && mBluetoothManager != null) {
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

        if (connectionState == BluetoothGatt.STATE_CONNECTED) {
            //
            if (DEBUG) {
                Toast.makeText(getApplicationContext(), "?", Toast.LENGTH_SHORT)
                        .show();
            }
            Log.d(TAG, "?");
            return false;
        }
    }
    ////
    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }
    //
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;

}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void connectToDevice(BluetoothDevice device) {
    try {/*www.  j  a va2  s.  c  om*/
        int connState = mBluetoothManager.getConnectionState(device, BluetoothGatt.GATT);

        switch (connState) {
        case BluetoothGatt.STATE_CONNECTED: {
            if (mBluetoothAdapter == null)
                return;

            final BluetoothDevice leDevice = mBluetoothAdapter.getRemoteDevice((String) null);
            int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

            if (mGatt != null) {
                if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
                    mGatt.disconnect();
                }
            }
        }
            break;
        case BluetoothGatt.STATE_DISCONNECTED:
            if (mGatt == null) {
                scanLeDevice(false);
                mGatt = device.connectGatt(this, false, gattCallback);
            }
            break;
        default:
            Log.i("connect to device : ", "device busy");
            break;
        }
    } catch (Exception ex) {
        Log.i("Exception", "connectToDevice");
    }
}

From source file:com.mbientlab.metawear.api.MetaWearBleService.java

/**
 * Connect to the GATT service on the MetaWear device.  This version of the function is for the old 
 * single MetaWear mode.  /*from  w  ww .j a va  2s . c om*/
 * @param metaWearBoard MetaWear board to connect to
 * @deprecated As of v1.3.  Use {@link MetaWearController#connect()} and retrieve a MetaWearController 
 * with {@link #getMetaWearController(BluetoothDevice)}
 */
@Deprecated
public void connect(BluetoothDevice metaWearBoard) {
    if (singleMwState == null) {
        singleMwState = new MetaWearState(metaWearBoard);
    } else {
        singleMwState.mwBoard = metaWearBoard;
    }

    if (singleMwState.mwController == null) {
        singleMwState.mwController = (MetaWearControllerImpl) getMetaWearController();
    }

    if (!metaWearStates.containsKey(metaWearBoard)) {
        close(singleMwState);
        metaWearStates.clear();
        metaWearStates.put(metaWearBoard, singleMwState);
        singleMwState.mwGatt = metaWearBoard.connectGatt(this, false, singleMwState.mwController);
    } else {
        reconnect();
    }

}