Exemplo n.º 1
0
    /** 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);
    }
Exemplo n.º 2
0
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   Log.wtf(TAG, "Connection State Change: " + status + " -> " + connectionState(newState));
   if (status == BluetoothGatt.GATT_SUCCESS
       && newState == BluetoothProfile.STATE_CONNECTED) {
     /*
      * Once successfully connected, we must next discover all the services on the
      * device before we can read and write their characteristics.
      */
     gatt.discoverServices();
     // mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Discovering Services..."));
   } else if (newState == BluetoothProfile.STATE_DISCONNECTED
       && status != BluetoothGatt.GATT_SUCCESS) {
     /*
      * If at any point we disconnect, send a message to clear the weather values
      * out of the UI
      */
     Log.wtf("Status: ", "Disconnected");
     Integer[] values = new Integer[] {-1, -1, -1, -1, -1, -1, -1, -1};
     DownloadWebPageTask task = new DownloadWebPageTask();
     task.execute(values);
     // mConnectedGatt.close();
     // gatt.close();
     if (!isScanRunning) {
       runOnUiThread(mStartRunnable);
     }
   }
 }
Exemplo n.º 3
0
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   if (status != BluetoothGatt.GATT_SUCCESS) {
     close();
   } else if (newState != BluetoothProfile.STATE_DISCONNECTED) {
     bluetoothGatt.discoverServices();
   }
 }
        @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??");
        }
Exemplo n.º 5
0
 @Override
 public void onConnectionStateChange(
     BluetoothGatt gatt, int status, int newState) { // Change in connection state
   if (newState == BluetoothProfile.STATE_CONNECTED) { // See if we are connected
     broadcastUpdate(
         ACTION_GATT_CONNECTED); // Go broadcast an intent to say we are connected
     Log.i(TAG, "Connected to GATT server, starting service discovery");
     mBluetoothGatt.discoverServices(); // Discover services on connected BLE device
   } else if (newState
       == BluetoothProfile.STATE_DISCONNECTED) { // See if we are not connected
     broadcastUpdate(
         ACTION_GATT_DISCONNECTED); // Go broadcast an intent to say we are disconnected
     Log.i(TAG, "Disconnected from GATT server.");
   }
 }
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          if (newState == BluetoothProfile.STATE_CONNECTED) {

            if (DEBUG) Log.d(TAG, "Device connected");

            // Attempts to discover services after successful connection.
            mBluetoothGatt.discoverServices();

          } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnected = false;
            telemService.toastMessage("Bluetooth device disconnected");
            if (DEBUG) Log.d(TAG, "ACTION_GATT_DISCONNECTED");
          }
        }
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   Log.i("onConnectionStateChange", "Status: " + status);
   switch (newState) {
     case BluetoothProfile.STATE_CONNECTED:
       Log.i("gattCallback", "STATE_CONNECTED");
       gatt.discoverServices();
       break;
     case BluetoothProfile.STATE_DISCONNECTED:
       Log.e("gattCallback", "STATE_DISCONNECTED");
       break;
     default:
       Log.e("gattCallback", "STATE_OTHER");
   }
 }
Exemplo n.º 8
0
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

      if (newState == BluetoothProfile.STATE_CONNECTED) {

        Log.i(TAG, "Connected to GATT server.");
        broadcastConnStatus(ACTION_GATT_CONNECTED, gatt.getDevice().getName());
        // Attempts to discover services after successful connection.
        Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

      } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        broadcastConnStatus(ACTION_GATT_DISCONNECTED, gatt.getDevice().getName());
        Log.i(TAG, "Disconnected from GATT server.");
      }
    }
Exemplo n.º 9
0
        @Override
        public void onConnectionStateChange(
            final BluetoothGatt gatt, final int status, final int newState) {

          // this will get called when a device connects or disconnects
          if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(tag, "newState");
            bluetoothGatt.discoverServices();
          } else if (newState == BluetoothProfile.STATE_DISCONNECTED
              && status == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(tag, "disconnected");
            if (isConnected()) disconnect();
            else connect(bluetoothGatt.getDevice());
          }
        }
 @Override
 public void onConnectionStateChange(
     final BluetoothGatt gatt, final int status, final int newState) {
   // this will get called when a device connects or disconnects
   if (newState == BluetoothProfile.STATE_CONNECTED) {
     System.out.println("Fick ja!");
     messageQueue = new LinkedList<>();
     gatt.discoverServices();
   } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
     System.out.println("Disconnected!");
     disconnect(gatt.getDevice().getAddress());
   } else {
     System.out.println("No Conenction:" + status);
     connectTo(gatt.getDevice());
   }
 }
Exemplo n.º 11
0
 @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);
   }
 }
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          Log.d(
              "status="
                  + LogUtil.getConstantName(BluetoothGatt.class, status, "GATT_")
                  + " newState="
                  + LogUtil.getConstantName(BluetoothProfile.class, newState, "STATE_"));
          switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
              mBluetoothGatt.discoverServices();
              break;

            case BluetoothProfile.STATE_DISCONNECTED:
              onDisconnect();
              break;
          }
        }
Exemplo n.º 13
0
        @Override
        public void onReceive(final Context context, final Intent intent) {
          final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
          final int previousBondState =
              intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);

          // Skip other devices
          if (mBluetoothGatt == null
              || !device.getAddress().equals(mBluetoothGatt.getDevice().getAddress())) return;

          Logger.d(
              mLogSession,
              "[Broadcast] Action received: "
                  + BluetoothDevice.ACTION_BOND_STATE_CHANGED
                  + ", bond state changed to: "
                  + bondStateToString(bondState)
                  + " ("
                  + bondState
                  + ")");
          DebugLogger.i(
              TAG,
              "Bond state changed for: "
                  + device.getName()
                  + " new state: "
                  + bondState
                  + " previous: "
                  + previousBondState);

          switch (bondState) {
            case BluetoothDevice.BOND_BONDING:
              mCallbacks.onBondingRequired();
              break;
            case BluetoothDevice.BOND_BONDED:
              Logger.i(mLogSession, "Device bonded");
              mCallbacks.onBonded();

              // Start initializing again.
              // In fact, bonding forces additional, internal service discovery (at least on Nexus
              // devices), so this method may safely be used to start this process again.
              Logger.v(mLogSession, "Discovering Services...");
              Logger.d(mLogSession, "gatt.discoverServices()");
              mBluetoothGatt.discoverServices();
              break;
          }
        }
 // Called whenever the device connection state changes, i.e. from disconnected to connected.
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   super.onConnectionStateChange(gatt, status, newState);
   if (newState == BluetoothGatt.STATE_CONNECTED) {
     // writeLine("Connected!");
     // Discover services.
     if (!gatt.discoverServices()) {
       // writeLine("Failed to start discovering services!");
       // connectButton.setBackgroundColor(0xffffff00);
       connectButton.setText("Start Scanning");
     }
   } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
     // writeLine("Disconnected!");
     connectButton.setBackgroundColor(0xffff0000);
     connectButton.setText("Disconnected");
   } else {
     // writeLine("Connection state changed.  New state: " + newState);
   }
 }
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          String intentAction;
          if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(
                TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());

          } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
          }
        }
Exemplo n.º 16
0
  @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;
      }
    }
  }
Exemplo n.º 17
0
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          String intentAction;
          System.out.println("=======status:" + status);
          if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(
                TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());

          } else if (status == 133) {
            mHandler.sendEmptyMessage(0);
          } else if (status != 133 && newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.e(TAG, "Disconnected from GATT server.");
            // broadcastUpdate(intentAction);
            // mHandler.sendEmptyMessage(0);
            broadcastDeviceBleUpdate(intentAction, gatt.getDevice().getAddress());
          }
        }