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.huiwu.temperaturecontrol.bluetooth.BluetoothService.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   ww  w .  j  a v  a2s . c o  m*/
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        TestLog.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        TestLog.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) {
        TestLog.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);
    TestLog.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

From source file:com.example.UART.UartService.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  . ja  va2s  .co m
 */
@SuppressLint("NewApi")
public boolean connect(final String 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, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

From source file:com.aprilbrother.blueduino.UartService.java

/**
 * Connects to the GATT server hosted on the Bluetooth LE device.
 * //from w w w .  ja v  a 2  s  .c  om
 * @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.
 */
public boolean connect(final String 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, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void connect(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "connect");
    String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
    if (connectedDevice.get(deviceAddress) != null) {
        Tools.sendSuccessMsg(callbackContext);
        return;/* w  ww .  j  a va  2  s.c  o  m*/
    }
    connectCC.put(deviceAddress, callbackContext);
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
    mBluetoothGatts.put(device.getAddress(), device.connectGatt(mContext, false, mGattCallback));
}

From source file:com.github.vgoliveira.panificadora.UartService.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 ww  w. j av a 2s.c om
 */
public boolean connect(final String 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;
    } else {
        Log.w(TAG, "Device founded.");
    }
    Log.v(TAG, "Device founded");
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    mBluetoothGatt = device.connectGatt(this, true, mGattCallback); //com TRUE resolveu problema, mas pode demorar + 10 seg
    return true;
}

From source file:com.sdingba.su.alphabet_demotest.view.lanya.UartService.java

/**
 * ?// w ww . j a v a  2  s  .  com
 * 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.
 *
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "SDingBaLanYan 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, "SDingBaLanYan Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    ////     TODO: 16-6-5 xcx   ?
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    if (device == null) {
        Log.w(TAG, "SDingBaLanYan 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.

    ////     TODO: 16-6-5 xcx   ?
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

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

From source file:com.example.android.bluetoothlegatt.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./*  w ww  . j  ava 2s  . c  o  m*/
 */
public boolean connect(final String 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, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

From source file:com.ble.BLService.java

/**
 * Connects to the GATT server hosted on the Bluetooth LE device.
 * //  w  w  w . jav a  2 s  . c  o  m
 * @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.
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        BLE_STATUS_CONNECTION_STRING = "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.");
        BLE_STATUS_CONNECTION_STRING = "Trying to use an existing mBluetoothGatt for connection.";

        return mBluetoothGatt.connect();

    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        BLE_STATUS_CONNECTION_STRING = "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.");
    BLE_STATUS_CONNECTION_STRING = "Trying to create a new connection.";

    mBluetoothDeviceAddress = address;

    return true;
}

From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.BluetoothCustomManager.java

/**
 * Connect to device's GATT server/*from   w  w  w . j  a va 2s.c o m*/
 */
@SuppressLint("NewApi")
public boolean connect(String address) {

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

    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    boolean alreadyInList = false;

    if (bluetoothConnectionList.containsKey(address)) {
        alreadyInList = true;
    }

    if (alreadyInList) {

        Log.v(TAG, "reusing same connection");

        BluetoothDeviceConn conn = (BluetoothDeviceConn) bluetoothConnectionList.get(address);

        conn.setGatt(device.connectGatt(context, false, conn.getGattCallback()));

    } else {

        BluetoothDeviceConn conn = new BluetoothDeviceConn(address, device.getName(), this);

        bluetoothConnectionList.put(address, conn);

        Log.v(TAG, "new connection");
        //connect to gatt server on the device
        conn.setGatt(device.connectGatt(context, false, conn.getGattCallback()));
    }

    return true;
}

From source file:net.kenevans.android.blecardiacmonitor.BCMBleService.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)}//from w w  w  .  ja  v a 2s .c  om
 * callback.
 */
public boolean connect(final String address) {
    Log.d(TAG, "connect");
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "connect: 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, "connect: Trying to use an existing mBluetoothGatt for " + "connection");
        if (mBluetoothGatt.connect()) {
            mConnectionState = BluetoothProfile.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 = BluetoothProfile.STATE_CONNECTING;
    return true;
}