コード例 #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);
    }
コード例 #2
0
ファイル: SICActivity.java プロジェクト: dhosford/testApp
 @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);
     }
   }
 }
コード例 #3
0
ファイル: BleConnection.java プロジェクト: tyrantgit/Ble
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   if (status != BluetoothGatt.GATT_SUCCESS) {
     close();
   } else if (newState != BluetoothProfile.STATE_DISCONNECTED) {
     bluetoothGatt.discoverServices();
   }
 }
コード例 #4
0
        @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??");
        }
コード例 #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.");
   }
 }
コード例 #6
0
        @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");
          }
        }
コード例 #7
0
 @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");
   }
 }
コード例 #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.");
      }
    }
コード例 #9
0
ファイル: BLEClass.java プロジェクト: dutrieuc/EBikeNav2
        @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());
          }
        }
コード例 #10
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) {
     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());
   }
 }
コード例 #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);
   }
 }
コード例 #12
0
        @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;
          }
        }
コード例 #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;
          }
        }
コード例 #14
0
 // 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);
          }
        }
コード例 #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;
      }
    }
  }
コード例 #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());
          }
        }