/** Callback invoked by Android framework and a LE connection state change occurs */
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
      Log.d(
          MainActivity.TAG,
          "onConnectionStateChange(): address=="
              + gatt.getDevice().getAddress()
              + ", status = "
              + status
              + ", state="
              + newState);
      boolean isConnected = (newState == BluetoothAdapter.STATE_CONNECTED);

      boolean isOk = (status == 0);
      if (isConnected && isOk) {
        // Discover services, and return connection state = connected
        // after services discovered
        isOk = gatt.discoverServices();
        if (isOk) {
          return;
        }
      }

      // If we got here, this is a disconnect with or without error
      // close gatt connection
      if (!isOk) {
        gatt.close();
      }
      processConnectionStateChanged(false, !isOk);
    }
 /**
  * After using a given BLE device, the app must call this method to ensure resources are released
  * properly.
  */
 public void close() {
   if (mBluetoothGatt == null) {
     return;
   }
   mBluetoothGatt.close();
   mBluetoothGatt = null;
 }
 public void disconnect() {
   for (BluetoothGatt bg : gatts.values()) {
     if (bg != null) {
       bg.disconnect();
       bg.close();
     }
   }
 }
Example #4
0
 // ----------------------------------------------------------------------------------------------------------------
 // An activity has unbound from this service
 @Override
 public boolean onUnbind(Intent intent) {
   if (mBluetoothGatt != null) { // Check for existing BluetoothGatt connection
     mBluetoothGatt.close(); // Close BluetoothGatt coonection for proper cleanup
     mBluetoothGatt = null; // No longer have a BluetoothGatt connection
   }
   return super.onUnbind(intent);
 }
 @Override
 protected void onDestroy() {
   if (mGatt == null) {
     return;
   }
   mGatt.close();
   mGatt = null;
   super.onDestroy();
 }
Example #6
0
 @Override
 protected void onStop() {
   super.onStop();
   // Disconnect from any active tag connection
   if (mConnectedGatt != null) {
     mConnectedGatt.close();
     mConnectedGatt = null;
   }
 }
 private void reportConnectFailed(String string) {
   System.err.println("reportConnectFailed(" + string + ")");
   if (btGatt != null) {
     btGatt.close();
     btGatt = null;
   }
   btDevice = null;
   reportConnected(false);
 }
Example #8
0
 public synchronized void close() {
   if (bluetoothGatt != null) {
     bluetoothGatt.disconnect();
     bluetoothGatt.close();
     bluetoothGatt = null;
     bluetoothGattService = null;
     callBack.onDisconnected();
   }
 }
 /**
  * After using a given BLE device, the app must call this method to ensure resources are released
  * properly.
  */
 public void close() {
   LogUtil.info(TAG, "mBluetoothGatt is close  before");
   if (mBluetoothGatt == null) {
     return;
   }
   mBluetoothGatt.close();
   mBluetoothGatt = null;
   broadcastUpdate(ACTION_GATT_CLOSE);
   LogUtil.info(TAG, "mBluetoothGatt is close");
 }
 @Override
 public void disconnect() {
   Log.d();
   if (mBluetoothGatt != null) {
     mBluetoothGatt.close();
     mBluetoothGatt.disconnect();
     mBluetoothGatt = null;
   }
   mBluetoothDevice = null;
   onDisconnect();
 }
 public void disconnect() {
   connectCallback = null;
   connected = false;
   if (gatt != null) {
     gatt.close();
     gatt = null;
     Log.d(LOG_TAG, "Disconnect");
     WritableMap map = Arguments.createMap();
     map.putString("peripheral", device.getAddress());
     sendEvent("BleManagerDisconnectPeripheral", map);
   } else Log.d(LOG_TAG, "GATT is null");
 }
 // OnStop, called right before the activity loses foreground focus.  Close the BTLE connection.
 @Override
 protected void onStop() {
   super.onStop();
   if (gatt != null) {
     // For better reliability be careful to disconnect and close the connection.
     gatt.disconnect();
     gatt.close();
     gatt = null;
     tx = null;
     rx = null;
   }
 }
Example #13
0
 private void startScan() {
   Log.wtf(TAG, "Starting Scan");
   BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
   mBluetoothAdapter = manager.getAdapter();
   if (mConnectedGatt != null) {
     mConnectedGatt.close();
   }
   mDevices.clear();
   mBluetoothAdapter.startLeScan(this);
   setProgressBarIndeterminateVisibility(true);
   mHand.postDelayed(mStopRunnable, 5000);
 }
Example #14
0
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  public void disconnect() {
    if (bluetoothGatt != null) {

      Intent intent = new Intent();
      intent.setAction(BLEClass.DISCONNECTED);
      ctx.sendBroadcast(intent);
      bluetoothGatt.disconnect();
      bluetoothGatt.close();
      isConnected = false;
    }
  }
 /** Closes and releases resources. May be also used to unregister broadcast listeners. */
 public void close() {
   try {
     mContext.unregisterReceiver(mBondingBroadcastReceiver);
     mContext.unregisterReceiver(mPairingRequestBroadcastReceiver);
   } catch (Exception e) {
     // the receiver must have been not registered or unregistered before
   }
   if (mBluetoothGatt != null) {
     mBluetoothGatt.close();
     mBluetoothGatt = null;
   }
   mUserDisconnected = false;
 }
        @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??");
        }
  @Override
  public void close() {
    stopScan();
    disconnect();

    if (btGatt != null) {
      btGatt.close();
      btGatt = null;
    }

    if (btAdapter == null) {
      btAdapter = null;
      return;
    }
  }
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   String intentAction;
   //			isStateChange = true;
   LogUtil.info(TAG, "蓝牙连接状态发生变化!onConnectionStateChange()");
   if (newState == BluetoothProfile.STATE_CONNECTED) {
     intentAction = ACTION_GATT_CONNECTED;
     mConnectionState = STATE_CONNECTED;
     mBluetoothGatt.discoverServices();
     broadcastUpdate(intentAction);
   } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
     intentAction = ACTION_GATT_DISCONNECTED;
     mConnectionState = STATE_DISCONNECTED;
     mBluetoothGatt.close();
     broadcastUpdate(intentAction);
   }
 }
  /**
   * 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());
  }
 public void disconnect(String address) {
   if (gatts.containsKey(address)) {
     BluetoothGatt bg = gatts.get(address);
     if (bg != null) {
       bg.disconnect();
       bg.close();
       humidity.put(address, 0.00);
       temperature.put(address, 0.00);
       setChanged();
       notifyObservers(getSensorData(bg));
     } else {
       System.out.println("Null gatts!");
     }
     gatts.remove(address);
   } else {
     System.out.println("No such Key! " + address);
   }
 }
 /** Callback invoked by Android framework when LE service discovery completes */
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   if (status != 0) {
     // Error occurred. close the ocnnection and return a
     // disconnected status
     gatt.close();
     try {
       processConnectionStateChanged(false, true);
     } catch (Throwable t) {
       Log.e(TAG, "error", t);
     }
   } else {
     try {
       processConnectionStateChanged(true, false);
     } catch (Throwable t) {
       Log.e(TAG, "error", t);
     }
   }
 }
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    Log.d(
        LOG_TAG,
        "onConnectionStateChange da "
            + status
            + " a "
            + newState
            + " peripheral:"
            + device.getAddress());

    this.gatt = gatt;

    if (newState == BluetoothGatt.STATE_CONNECTED) {

      connected = true;
      gatt.discoverServices();

    } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {

      if (connected) {
        connected = false;

        if (gatt != null) {
          gatt.close();
          gatt = null;
        }

        WritableMap map = Arguments.createMap();
        map.putString("peripheral", device.getAddress());
        sendEvent("BleManagerDisconnectPeripheral", map);
        Log.d(LOG_TAG, "BleManagerDisconnectPeripheral peripheral:" + device.getAddress());
      }
      if (connectFailCallback != null) {
        connectFailCallback.invoke();
        connectFailCallback = null;
        connectCallback = null;
      }
    }
  }
 private void closeDevice() {
   if (mPickedDeviceGatt != null) {
     mPickedDeviceGatt.close();
     mPickedDeviceGatt = null;
   }
 }
  @Override
  public void disconnect() {
    if (btGatt == null) return;

    if (btDevice == null) {
      return;
    }

    boolean isConnected = mIsConnected;
    if (mIsConnecting == false && mIsConnected == false) return;

    if (mIsDisconnecting == true) return;

    mIsConnected = false;
    mIsConnecting = false;
    mIsDisconnecting = true;

    do {
      BluetoothGattService mHRP = btGatt.getService(HRP_SERVICE);
      if (mHRP == null) {
        reportDisconnectFailed("HRP service not found!");
        break;
      }

      BluetoothGattCharacteristic mHRMcharac =
          mHRP.getCharacteristic(HEART_RATE_MEASUREMENT_CHARAC);
      if (mHRMcharac == null) {
        reportDisconnectFailed("HEART RATE MEASUREMENT charateristic not found!");
        break;
      }

      if (!enableNotification(false, mHRMcharac)) {
        reportDisconnectFailed("disableNotfication");
        break;
      }
    } while (false);

    btGatt.disconnect();

    if (isConnected) {
      System.out.println("close btGatt in onConnectionState");
      // close btGatt in onConnectionState
      synchronized (this) {
        long end = System.currentTimeMillis() + 2000; // wait max 2
        // seconds
        while (btGatt != null && System.currentTimeMillis() < end) {
          System.out.println("waiting for btGatt to become null");
          try {
            this.wait(500);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        BluetoothGatt copy = btGatt;
        if (copy != null) {
          System.out.println("close btGatt in disconnect() after waiting 2 secs");
          copy.close();
          btGatt = null;
        }
      }
    } else {
      System.out.println("close btGatt here in disconnect()");
      btGatt.close();
      btGatt = null;
    }

    btDevice = null;
    mIsDisconnecting = false;
  }