Example #1
0
  @Override
  public Boolean isConnected() {
    if (btSocket == null) {
      return false;
    }

    return btSocket.isConnected();
  }
  @Override
  public byte[] getByteArray(String locator, String hash) {

    byte[] data = new byte[3000000];
    int connectAttemptsCounter = 0;

    if (locator.contains(NIMACBT)) {
      if (D) Log.d(TAG, " Connection process   - Starting connection process!");
      String mac = getMacAddressFromLocator(locator);
      BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac);

      BluetoothSocket mmSocket = null;
      BluetoothSocket tmp = null;

      try {
        tmp = device.createInsecureRfcommSocketToServiceRecord(BO_TRANSFER_UUID);
      } catch (IOException e) {
        if (D) Log.e(TAG, "create() failed - (createInsecureRfcommSocketToServiceRecord) ", e);
      }
      mmSocket = tmp;

      // Always cancel discovery because it will slow down a connection
      mBluetoothAdapter.cancelDiscovery();

      while (connectAttemptsCounter < retryAttempts) {

        connectAttemptsCounter++;
        // Make a connection to the BluetoothSocket
        try {
          // This is a blocking call and will only return on a
          // successful connection or an exception

          mmSocket.connect();
          if (D) Log.d(TAG, " Connection process   - Connection established - Socket connected");
          if (D) Log.d(TAG, " Transferring Process - Starting file transfer");
          data = getByteArrayFromRemotePeer(mmSocket, hash);
          if (data != null) {
            if (D) Log.d(TAG, " Transferring Process - File transferring done!");
            return data;
          }

        } catch (IOException e) {
          if (D)
            Log.e(
                TAG,
                "failure while connecting to the socket - (createInsecureRfcommSocketToServiceRecord) ",
                e);
        } finally {
          try {
            if (mmSocket.isConnected()) {
              mmSocket.close();
            }
          } catch (IOException e2) {
            Log.e(TAG, "unable to close()  socket during connection failure", e2);
          }
        }
      }
      return null;
    }
    return null;
  }