Exemplo n.º 1
0
    @Override
    public void doConnectionHandover(NdefMessage handoverRequest, int handover, int record)
        throws IOException {

      byte[] remoteCollision = handoverRequest.getRecords()[handover + 1].getPayload();
      if (remoteCollision[0] == mCollisionResolution[0]
          && remoteCollision[1] == mCollisionResolution[1]) {
        return; // They'll have to try again.
      }
      boolean amServer =
          (remoteCollision[0] < mCollisionResolution[0]
              || (remoteCollision[0] == mCollisionResolution[0]
                  && remoteCollision[1] < mCollisionResolution[1]));

      if (mAlwaysClient) {
        amServer = false;
      }

      if (!mConnectionStarted) {
        synchronized (BluetoothConnecting.this) {
          if (!mConnectionStarted) {
            mConnectionStarted = true;
            mmBtConnected.beforeConnect(amServer);
          }
        }
      }
      if (!amServer) {
        // Not waiting for a connection:
        mAcceptThread.cancel();
        Uri uri = Uri.parse(new String(handoverRequest.getRecords()[record].getPayload()));
        UUID serviceUuid = UUID.fromString(uri.getPath().substring(1));
        int channel = -1;
        String channelStr = uri.getQueryParameter("channel");
        if (null != channelStr) {
          channel = Integer.parseInt(channelStr);
        }

        BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(uri.getAuthority());
        new ConnectThread(remoteDevice, serviceUuid, channel).start();
      }
    }