/**
   * 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 btleConnect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
      if (WARN) 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) {
      if (DEBUG) Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
      if (mBluetoothGatt.connect()) {
        mConnectionState = STATE_CONNECTING;
        return true;
      } else {
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        mBluetoothGatt = device.connectGatt(telemService, false, mGattCallback);
        mBluetoothDeviceAddress = address;
        return false;
      }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
      if (WARN) 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(telemService, false, mGattCallback);
    if (DEBUG) Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
  }
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
          if (hrClient == null) return;

          if (hrClientHandler == null) return;

          String address = device.getAddress();
          if (mIsConnecting && address.equals(btDevice.getAddress())) {
            stopScan();
            btGatt = btDevice.connectGatt(context, false, btGattCallbacks);
            return;
          }

          if (mScanDevices.contains(address)) return;

          mScanDevices.add(address);

          hrClientHandler.post(
              new Runnable() {
                @Override
                public void run() {
                  if (mIsScanning) { // NOTE: mIsScanning in user-thread
                    hrClient.onScanResult(NAME, device);
                  }
                }
              });
        }
Пример #3
0
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
 public boolean connect(final BluetoothDevice device) {
   Log.i(tag, "connect");
   bluetoothGatt = device.connectGatt(ctx, false, btleGattCallback);
   long start = System.currentTimeMillis();
   while (!connected && (System.currentTimeMillis() - start) < 5000) ;
   return characFound;
 }
Пример #4
0
 @Override
 public CommConnection connect(
     Context context, boolean autoConnect, CommConnectionCallback callback, Handler handler) {
   BleConnection connection =
       new BleConnection(this, context, (BleConnectionCallback) callback, handler);
   BluetoothGatt gatt = btDevice.connectGatt(context, autoConnect, connection);
   connection.setGatt(gatt);
   return connection;
 }
 public void connectTo(BluetoothDevice device) {
   if (device != null) {
     BluetoothGatt bg = device.connectGatt(context, false, btleGattCallback);
     gatts.put(device.getAddress(), bg);
     temperature.put(device.getAddress(), 0.0);
     humidity.put(device.getAddress(), 0.0);
   } else {
     System.out.println("null device!");
   }
 }
 public void connect(Callback connectCallback, Callback failCallback, Activity activity) {
   if (!connected) {
     BluetoothDevice device = getDevice();
     this.connectCallback = connectCallback;
     this.connectFailCallback = failCallback;
     gatt = device.connectGatt(activity, false, this);
   } else {
     connectCallback.invoke();
   }
 }
Пример #7
0
  /** Connect to the picked device */
  private void connect() {
    if (mPickedDevice == null) {
      showMessage(
          getString(R.string.error_connect, mPickedDevice.getName(), mPickedDevice.getAddress()));
      return;
    }

    mPickedDeviceGatt = mPickedDevice.connectGatt(this, false, mGattCallback);

    if (mPickedDeviceGatt == null) {
      showMessage(
          getString(R.string.error_connect, mPickedDevice.getName(), mPickedDevice.getAddress()));
    }
  }
  @Override
  public void setBluetoothDevice(BluetoothDevice bluetoothDevice) {
    Log.d();
    mStatus = Status.CONNECTING;
    // Inform listeners
    mListeners.dispatch(
        new Dispatcher<HeartRateListener>() {
          @Override
          public void dispatch(HeartRateListener listener) {
            listener.onConnecting();
          }
        });

    mBluetoothDevice = bluetoothDevice;
    mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, true, mBluetoothGattCallback);
  }
Пример #9
0
  /**
   * Connects to the Bluetooth Smart device
   *
   * @param device a device to connect to
   */
  public void connect(final BluetoothDevice device) {
    if (mConnected) return;

    if (mBluetoothGatt != null) {
      Logger.d(mLogSession, "gatt.close()");
      mBluetoothGatt.close();
      mBluetoothGatt = null;
    }

    final boolean autoConnect = shouldAutoConnect();
    mUserDisconnected =
        !autoConnect; // We will receive Linkloss events only when the device is connected with
                      // autoConnect=true
    Logger.v(mLogSession, "Connecting...");
    Logger.d(mLogSession, "gatt = device.connectGatt(autoConnect = " + autoConnect + ")");
    mBluetoothGatt = device.connectGatt(mContext, autoConnect, getGattCallback());
  }
        @Override
        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
          Log.d(TAG, "discovered device: " + bluetoothDevice.getName());
          Log.d(TAG, "device MAC: " + bluetoothDevice.getAddress());
          Home home = ((Alexandra) getApplicationContext()).getHome();
          if (home != null) {
            for (Gadget gadget : home.getGadgets()) {
              Log.d(TAG, "gadget MAC: " + gadget.getMAC());

              if (gadget.getMAC().equals(bluetoothDevice.getAddress())) {
                Log.d(TAG, "trying to connect GATT");
                gadget.setBluetoothGatt(
                    bluetoothDevice.connectGatt(
                        getApplicationContext(), true, gadget.getBluetoothGattCallback()));
              }
            }
          }
        }
Пример #11
0
 /**
  * The following functions perform BLE GATT related operations Connect, disconnect,
  * Characteristics R/W, descriptors R/W
  */
 public boolean connect(int index, BluetoothDevice device) {
   if (mBluetoothAdapter == null || device == null) {
     Log.w(TAG, "BluetoothAdapter not initialized or unspecified device.");
     return false;
   }
   // reconnect a device
   if (mBluetoothGatt[index] != null) {
     Log.i(TAG, "Trying to use an existing mBluetoothGatt for connection.");
     return mBluetoothGatt[index].connect();
   }
   // Establish new GATT connection
   mBluetoothGatt[index] = device.connectGatt(this, false, new BleGattCallback());
   if (mBluetoothGatt[index] != null) {
     Log.i(TAG, "GATT connected.");
     return true;
   }
   Log.e(TAG, "Fail to connect GATT");
   return false;
 }
Пример #12
0
  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;
  }
Пример #13
0
  // ----------------------------------------------------------------------------------------------------------------
  // 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;
  }
        @Override
        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {

          System.out.println(bluetoothDevice.getName());
          // writeLine("Found device: " + bluetoothDevice.getAddress());

          // Check if the device has the UART service.
          if (parseUUIDs(bytes).contains(UART_UUID)) {
            // Found a device, stop the scan.
            adapter.stopLeScan(scanCallback);
            // writeLine("Found UART service!");
            connectButton.setBackgroundColor(0xff00ff00);
            connectButton.setText("Connected");
            TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 550.0f);
            animation.setDuration(2000);
            animation.setFillAfter(true);
            connectButton.startAnimation(animation);

            //                .startIndeterminate();animatedCircleLoadingView
            // Connect to the device.
            // Control flow will now go to the callback functions when BTLE events occur.
            gatt = bluetoothDevice.connectGatt(getApplicationContext(), false, callback);
          }
        }
  @Override
  public void connect(BluetoothDevice dev, String btDeviceName) {
    stopScan();

    if (mIsConnected) return;

    if (mIsConnecting) return;

    mIsConnecting = true;
    btDevice = dev;
    if (btDeviceName == null
        || dev.getName() == null
        || !dev.getName().contentEquals(btDevice.getName())) {
      /** If device doesn't match name, scan for before connecting */
      System.err.println("Scan before connect");
      startScan();
      return;
    }
    System.err.println("connectGatt");
    btGatt = btDevice.connectGatt(context, false, btGattCallbacks);
    if (btGatt == null) {
      reportConnectFailed("connectGatt returned null");
    }
  }
 public void connectToDevice(BluetoothDevice device) {
   if (mGatt == null) {
     mGatt = device.connectGatt(this, false, gattCallback);
     scanLeDevice(false); // will stop after first device detection
   }
 }