Android Question Acessing an external jar

JTmartins

Active Member
Licensed User
Longtime User
I've been reading and testing about acessing an external jar.

I'm lost to implement this USB SmartCard Reader.

I have to read the USB host and pass parameters to the package regarding the device, so that I can open it, read etc, I can't figure out how to do this.

Can some one guide me a little so that I can digg a little more into it ?

I can get the device Info in B4A using the USB library, but I do not have a clue of how to pass this information to the package.

For the time being I would be happy just to make sure that reader is connected and a card inserted.

Then I will try to solve the rest of my problem.

Attached is the JAR and Docs.

Many thanks in advance.
 

Attachments

  • Card.zip
    198 KB · Views: 268

JTmartins

Active Member
Licensed User
Longtime User
After trying lots of different ways to get the usbdevice I always endup with an error as below..or similar ones when trying to pass the device to one of the methods.

I still haven't understood how to change the usbdevice to a java object.

java.lang.IllegalArgumentException: argument 2 should have type java.lang.Object[], got android.hardware.usb.UsbDevice

the question now in my mind is : Is it possible ?
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
I'm not sure without seeing your code. Are you perhaps using JavaObject and calling a method like this? JO.RunMethod("SomeMethod",Device)
Try instead to format it like this. JO.RunMethod("SomeMethod",Array As Object(Device))
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Please forgive any silly things I say, as this is the first time I'm trying to connect with an external jar and my java knowledge is quite low and I still haven't figured out lots of things.

I only have one USB device attached (an ACS smart Card reader), so the code is not iteracting trough devices, as is not a problem at this stage.

All help is important, as I really want to learn how to use external jars (this one and others) to be able to take my B4A skills to a higher level.


The code I have so far in my testing to handle this card reader is :

B4X:
#Region  Project Attributes
    #ApplicationLabel: CCtestes
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalJar: acssmc-1.1.2

B4X:
Sub Process_Globals
    Dim manager As UsbManager
End Sub

Sub Globals
    Dim ud As UsbDevice
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    manager.Initialize
    CheckUSB
    abrereader
End Sub

Sub CheckUSB

     Private usbdevices() As UsbDevice
     Private usbInterf As UsbInterface
         usbdevices = manager.GetDevices
  For i = 0 To usbdevices.Length - 1
      ud = usbdevices(i)
      usbInterf=ud.GetInterface (i)
Next
    If manager.HasPermission(ud) = False Then
            manager.RequestPermission(ud)
    End If
End Sub

Sub abrereader As JavaObject
Dim jo As JavaObject
jo.InitializeStatic ("com.acs.smartcard.Reader").RunMethod("open",Array As Object  (ud))

End Sub

I get :java.lang.IllegalArgumentException: expected receiver of type com.acs.smartcard.Reader, but got java.lang.Class<com.acs.smartcard.Reader>

many thanks.
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Erel, Maybe you can help me to get started here.

I've checked all examples I could find in the forum, as for bluetooth, etc...And I did play with them, and understood them.

Currently I would be happy just to be able to create a new instance of the public class reader. Documentation says :

To create and initialize a Reader object, get an instance of UsbManager by calling Context.getSystemService().
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
Reader reader = new Reader(manager);

Also per documentation the constructor is : Reader(UsbManager manager)


I've tryed using JavaObject and reflector...
and I always get Constructor not found, so I think I' passing the parameters incorretcly.

B4X:
mReader=jo.InitializeNewInstance ("com.acs.smartcard.Reader",Array As Object (Manager))

being that Manager is defined as an UsbManager initialized, and detects the connected device.

I think what I want to do is the B4A equivalent of the java

B4X:
private UsbManager mManager;
private Reader mReader;

  // Get USB manager
  mManager = (UsbManager) getSystemService(Context.USB_SERVICE);

  // Initialize reader
  mReader = new Reader(mManager);
I would thank you a million if you put me in the proper direction, as altough I'm learning Java in every free minute I still have a lot to learn and understand.
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Many thanks Erel. I will try

I was messing around with

B4X:
Dim r As Reflector
Dim Umanager As Object
r.Target = r.GetContext
Umanager = r.RunMethod2("getSystemService", "usb", "java.lang.String")

that looks similar, but havent tested it as I was too tired, and needed some sleep...I'm dying to try it, but can't do it at the moment. I will come back later.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Erel, Thank You...Thank You..Thank You... With your guidance and my litle knowledge I can now set the reader, open the reader & detect if a card is present....

Maybe it helps someone, so here it goes what I have tested so far. This is draft code, so it's not very prety, but for me is a masterpiece.. eh eh eh.

Lot's of work ahed and a lot to learn to make this complete, but it's a start.


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim uManager As UsbManager
    Dim uDevice As UsbDevice


    ' ************
    ' JAVA VARS
    '*************
    Dim mReader As JavaObject
    Dim device As JavaObject
    Dim Manager As JavaObject


    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    verificaUsbDevice
    NewReader ' Create new instance
    CreateEvent ' Create an event
    OpenReader ' open reader

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub NewReader As JavaObject
Private jo As JavaObject
Manager= GetContext.RunMethod("getSystemService", Array("usb"))
mReader=jo.InitializeNewInstance ("com.acs.smartcard.Reader",Array As Object (Manager))
End Sub

Sub CreateEvent As JavaObject

Private callback As Object

callback=mReader.CreateEvent  ("com.acs.smartcard.Reader.OnStateChangeListener", "status", Null)
Private cObject As Object = mReader.RunMethod("setOnStateChangeListener",Array As Object  (callback))


End Sub

Sub OpenReader As JavaObject
device=mReader.RunMethod ("open",Array  (uDevice))
End Sub


Sub status_event(MethodName As String, Args() As Object) As Object

Private slotNum As Int=Args(0)
Private prevState As Int=Args(1)
Private currState As Int=Args(2)
Private getState As Object
Private Unknown As Int=mReader.GetField ("CARD_UNKNOWN")
Private Absent As Int =mReader.GetField ("CARD_ABSENT")
Private Present As Int =mReader.GetField ("CARD_PRESENT")
Private Undefined As Int=mReader.GetField ("PROTOCOL_UNDEFINED")

getState=mReader.RunMethod ("getState", Array As Object (slotNum))

If currState=Unknown Then
    Msgbox("Card Unknown","Status")
End If
If prevState > currState AND currState<>Unknown Then
     Msgbox ("Card Removed","Status")
Else
    If currState<>Unknown Then
     Msgbox ("Cartão Inserted","Status")
    End If
End If

End Sub

Sub verificaUsbDevice As Object
    uManager.Initialize
    Dim usbdevices() As UsbDevice
    usbdevices = uManager.GetDevices
    For i = 0 To usbdevices.Length - 1
     ' Dim ud As UsbDevice
      uDevice = usbdevices(i)
    Next
    If uManager.HasPermission(uDevice) = False Then
         uManager.RequestPermission(uDevice)
    End If
    Return uDevice
End Sub

Sub Button1_Click
    'CheckUSB2
End Sub

Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO ("processBA")
End Sub
 
Last edited:
Upvote 0

dannythevii

Member
Licensed User
hi to all
i want to read/write a nfc ntag or mifare ultralight with an acs external reader.

this code work well, but i don't understant how to read/write cards

someone can help me with a little demo ?

please :oops:

thanks
 
Last edited:
Upvote 0

zmrcic

Member
Licensed User
@JTmartins
Hi there...I'm trying to implement the communication with card reader/card....APDU.
Can not configure your last code, it's not working for me. Can you please post complete code because I get error on
device=mReader.RunMethod ("open",Array (udevice))
maybe even before that if device is null?

thx
 
Upvote 0
Top