コード例 #1
0
  @Override
  public void connect(String address) {

    if (dialog != null && dialog.isShowing()) {
      dialog.dismiss();
    }

    currentBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (currentBluetoothAdapter == null) {
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_not_supported), "", view, true, null);
      return;
    }

    if (currentBluetoothAdapter.isEnabled()) {
      createConnection(address);
    } else {
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
    }
  }
コード例 #2
0
 // creamos el socket para conectarnos
 private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
   if (Build.VERSION.SDK_INT >= 10) {
     try {
       final Method m =
           device
               .getClass()
               .getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {UUID.class});
       return (BluetoothSocket) m.invoke(device, MY_UUID);
     } catch (Exception e5) {
       Log.e(kdUINOApplication.TAG, "Could not create Insecure RFComm connection", e5);
       DisplayUtilities.ShowLargeMessage(
           this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
     }
   }
   return device.createRfcommSocketToServiceRecord(MY_UUID);
 }
コード例 #3
0
  public void createConnection(String address) {

    BluetoothDevice device = currentBluetoothAdapter.getRemoteDevice(address);

    try {
      btSocket = createBluetoothSocket(device);

    } catch (IOException e1) {

      Log.e(
          kdUINOApplication.TAG,
          "Fatal Error,In onResume() and socket create failed,TRY TO CONNECT AGAIN!",
          e1);
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
    }

    // cancelamos la búsqueda
    if (currentBluetoothAdapter.isDiscovering()) {
      currentBluetoothAdapter.cancelDiscovery();
    }

    // establecemos la conexión
    try {

      btSocket.connect();

    } catch (IOException exception) {

      try {

        Log.e(
            kdUINOApplication.TAG,
            "Fatal Error,In onResume() and socket create failed, TRY AGAIN!",
            exception);
        DisplayUtilities.ShowLargeMessage(
            this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);

        btSocket.close();
        return;

      } catch (IOException e3) {

        Log.e(
            kdUINOApplication.TAG,
            "Fatal Error,In onResume() and unable to close socket during connection failure",
            e3);
        DisplayUtilities.ShowLargeMessage(
            this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
        return;
      }

    } catch (Exception ex) {
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
      return;
      // btSocket.close();
    }
    // una vez creada la conexión creamos un outputStrem i inputStream para operar con el
    // posteriormente
    try {
      outputStream = btSocket.getOutputStream();
      inputStream = btSocket.getInputStream();

    } catch (IOException exception) {
      Log.e(
          kdUINOApplication.TAG,
          "Fatal Error,In onResume() and output/input stream creation failed",
          exception);
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);

    } catch (Exception ex) {
      DisplayUtilities.ShowLargeMessage(
          this.context.getString(R.string.bluetooth_cant_connect), "", view, false, null);
      return;
      // btSocket.close();
    }

    //  bus.post(new KdUinoMessageBusEvent(KdUINOMessages.BLUETOOTH_CONNECT_TO_KDUINO, address));

    // una vez conectados vamos al menu control

  }