/**
   * 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, "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;
  }
 public boolean isConnect() {
   boolean isRet = true;
   if (mBluetoothGatt == null || !mBluetoothGatt.connect()) {
     isRet = false;
   }
   return isRet;
 }
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

          System.err.println(
              "onConnectionStateChange: "
                  + gatt
                  + ", status: "
                  + status
                  + ", newState: "
                  + newState);
          System.err.println("STATUS_SUCCESS:" + BluetoothGatt.GATT_SUCCESS);
          System.err.println(
              "STATE_CONNECTED: "
                  + BluetoothProfile.STATE_CONNECTED
                  + ", STATE_DISCONNECTED: "
                  + BluetoothProfile.STATE_DISCONNECTED);

          if (gatt != btGatt) {
            System.err.println("gatt(" + gatt + ") != btGatt (" + btGatt + ")");
            return;
          }

          if (mIsConnecting) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
              boolean res = btGatt.discoverServices();
              System.err.println("discoverServices() => " + res);
              return;
            } else {
              boolean res = btGatt.connect();
              System.err.println("disconnect while connecting => btGatt.connect() => " + res);
              return;
            }
          }

          if (mIsDisconnecting) {
            System.err.println("mIsDisconnecting => notify");
            synchronized (this) {
              btGatt.close();
              btGatt = null;
              this.notifyAll();
              return;
            }
          }

          if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            reportDisconnected();
            return;
          }
          System.err.println("onConnectionStateChange => WHAT TO DO??");
        }
  public boolean reConnect() {
    if (mBluetoothAdapter == null || mDevice == null) {
      LogUtil.info(TAG, "BluetoothAdapter not initialized or unspecified address.");
      return false;
    }
    // if (mBluetoothDeviceAddress != null &&
    // address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
    // LogUtil.info(TAG,
    // "Trying to use an existing mBluetoothGatt for connection.");
    // if (mBluetoothGatt.connect()) {
    // mConnectionState = STATE_CONNECTING;
    // return true;
    // } else {
    // return false;
    // }
    // }

    // We want to directly connect to the device, so we are setting the
    // autoConnect
    // parameter to false.

    mBluetoothGatt = mDevice.connectGatt(BluetoothLeService.this, true, mGattCallback);
    LogUtil.info(TAG, "Trying to reConnect.");
    if (mDevice.getName().equals(BLEConstants.TYPE_FATSCALE_DEVICE_NAME)) {
      DEVICE_TYPE = 1;
    } else if (mDevice.getName().equals(BLEConstants.TYPE_BLOODPRESSURE_DEVICE_NAME)) {
      DEVICE_TYPE = 2;
    } else if (mDevice.getName().equals(BLEConstants.TYPE_BLOODSUGAR_DEVICE_NAME)) {
      DEVICE_TYPE = 3;
    }

    if (mBluetoothGatt != null) {
      if (mBluetoothGatt.connect()) {
        mConnectionState = STATE_CONNECTING;
        LogUtil.info(TAG, "Trying to reConnect. ok.");
        return true;
      } else {
        return false;
      }
    }
    return false;
  }
  // ----------------------------------------------------------------------------------------------------------------
  // Open a BluetoothGatt connection to a BLE device given its address
  public boolean connect(final String address) {
    if (mBluetoothAdapter == null
        || address == null) { // Check that we have a Bluetooth adappter and device address
      Log.w(
          TAG,
          "BluetoothAdapter not initialized or unspecified address."); // Log a warning that
                                                                       // something went wrong
      return false; // Failed to connect
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null
        && address.equals(mBluetoothDeviceAddress)
        && mBluetoothGatt != null) { // See if there was previous connection to the device
      Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
      if (mBluetoothGatt
          .connect()) { // See if we can connect with the existing BluetoothGatt to connect
        return true; // Success
      } else {
        return false; // Were not able to connect
      }
    }

    final BluetoothDevice device =
        mBluetoothAdapter.getRemoteDevice(
            address); // No previous device so get the Bluetooth device by referencing its address
    if (device == null) { // Check whether a device was returned
      Log.w(TAG, "Device not found.  Unable to connect."); // Warn that something went wrong
      return false; // Failed to find the device
    }

    mBluetoothGatt =
        device.connectGatt(
            this, false, mGattCallback); // Directly connect to the device so autoConnect is false
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress =
        address; // Record the address in case we bneed to reconnect with the existing BluetoothGatt
    return true;
  }