Java code convert to B4A

D

Deleted member 103

Guest
Hi guys,

can someone convert this source code to B4A?
I know it's a lot, but maybe someone has some time for it.

B4X:
   /**
    * BroadcastReceiver zum Empfangen und Auswerten der USB Erlaubnis aus
    * UsbManager.requestPermission(). Wenn die Erlaubnis vom Benutzer erteilt
    * wurde, wird die USB Verbindung hergestellt. 
    */
   private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

      public void onReceive(Context context, Intent intent) {
         Log.d(TAG, "mUsbReceiver::onReceive");
         String action = intent.getAction();
         if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
               UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

               if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                  Log.d(TAG, "USB permission granted");
                  if(device != null) {
                     setDevice(device);
                     connect();
                  }
               }
               else {
                  Log.d(TAG, "permission denied for device " + device);
               }
            }
         }
      }
   };

   public void onResume() {
      super.onResume();
      Log.d(TAG, "onResume");

      mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
      IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
      registerReceiver(mUsbReceiver, filter);

      Intent intent = getIntent();
      Log.d(TAG, "intent: " + intent);
      String action = intent.getAction();

      UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
      if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
         setDevice(device);
         connect();
      } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
         if (mDevice != null && mDevice.equals(device)) {
            disconnect();
         }
      }
   }

Thank you very much
Filippo
 
D

Deleted member 103

Guest
Hi Erel,

I have problem with the BroadCastReceiver, I do not know how to convert this Java code.
It works with security with the library "Reflection", but I do not know how.

B4X:
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);

    Intent intent = getIntent();
    Log.d(TAG, "intent: " + intent);
    String action = intent.getAction();
 
Upvote 0
D

Deleted member 103

Guest
I've done it, it works.
B4X:
Sub Globals
     Dim Broadcast As BroadCastReceiver
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Broadcast.Initialize("Broadcast")

   Broadcast.addAction("com.android.example.USB_PERMISSION")
   Broadcast.SetPriority(999)
   Broadcast.registerReceiver("") 'here you can add the main action (intent)

End Sub

Sub Broadcast_OnReceive(Action As String, i As Object)
   Dim i2 As Intent
   i2 = i
   
   If i2.Action.Contains(Action) Then
      ToastMessageShow("USB-PERMISSION allowed!", False)
   End If
   
   'can only abort when sendOrderedbroadcast is called.
   Broadcast.AbortBroadcast
End Sub
 
Upvote 0
Top