B4J Question How to get data from HID device?

bjfhs

Active Member
Licensed User
Longtime User
B4X:
#AdditionalJar: purejavahidapi
#AdditionalJar: jna-4.0.0

   Dim hdi As JavaObject
    hdi.InitializeStatic("purejavahidapi.HidDeviceInfo")
    'initial PureJavaHidApi
    Dim jo As JavaObject
    jo.InitializeStatic("purejavahidapi.PureJavaHidApi")
    'list all usb hid
    Dim lst As List
    lst = jo.RunMethodJO("enumerateDevices", Null)
    'open usb hid
    Dim obj As JavaObject
    obj.InitializeStatic("purejavahidapi.HidDeviceInfo")
    Dim tmp As Boolean
    Dim PD As Long ,VD As Long
    tmp=False
    For i=0 To lst.Size-1
        Log("lst:" & lst.Get(i))
        'ListView1.Items.Add("lst:"  & lst.Get(i))
        obj = lst.Get(i)
        PD=obj.RunMethod("getProductId",Null)
        VD=obj.RunMethod("getVendorId",Null)
        If  PD=0x1001 And VD=0x6816 Then
           tmp=True
           Exit
        End If
      
    Next
    If tmp=False Then
        fx.Msgbox(MainForm, "没有发现设备!", "警告")
        Return
    End If
  
    Dim getPath As String
    getPath = obj.RunMethod("getPath",Null)
    getPath=getPath.Trim
    Log(getPath)
    Dim hd As JavaObject
    hd.InitializeStatic("purejavahidapi.HidDevice")
    hd = jo.RunMethod("openDevice",  Array As String(getPath))
log:
B4X:
Waiting for debugger to connect...
Program started.
lst:purejavahidapi.windows.HidDeviceInfo@10c98a4
lst:purejavahidapi.windows.HidDeviceInfo@1f3046d
lst:purejavahidapi.windows.HidDeviceInfo@936f4f
\\?\hid#vid_6816&pid_1001#7&5dc41fe&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
Error occurred on line: 59 (Main)
java.lang.RuntimeException: Method: openDevice not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
    at b4j.example.main._appstart(main.java:200)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:625)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:168)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:94)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

some about purejavahidapi
https://github.com/nyholku/purejavahidapi/commit/4388fde3328ef5e8eb7d13ef82c91a79741f0b0a
 
Last edited:

bjfhs

Active Member
Licensed User
Longtime User
inline Java
B4X:
ba.raiseEventFromUI(this, "callback", s);;
B4J
B4X:
Public Sub callback (txt As String)
    Log (txt)
End Sub
 
Upvote 0

GabrielM

Member
Licensed User
Longtime User
inline Java
B4X:
ba.raiseEventFromUI(this, "callback", s);;
B4J
B4X:
Public Sub callback (txt As String)
    Log (txt)
End Sub

Many thanks bjfhs,

I am probably doing something wrong as the callback sub is not being called for some reason. The HID device I am testing is sending 8 bytes report each 500ms so I though it might get intercepted by the Listener somehow.

This is where I have attempted to add the raiseEvent:


Report Listener:
dev.setInputReportListener(new InputReportListener() {
    [USER=69643]@override[/USER]
    public void onInputReport(HidDevice source, byte Id, byte[] data, int len) {

        ba.raiseEventFromUI(this, "callback", s); // <<<************

        System.out.printf("onInputReport: id %d len %d data ", Id, len);
        //for (int i = 0; i < len; i++)
        //    System.out.printf("%02X ", data[i]);
        System.out.println();
    }
});
 
Upvote 0

GabrielM

Member
Licensed User
Longtime User
Got it to call now.
I have replaced the s string with a byte Array and passing it as parameter to callback sub, and it seems to be working Ok.

callback sub:
Public Sub callback (idata() As Byte)
    Dim myidata() As Byte = idata
    Log(myidata(0))  
    'Log ("got data")
End Sub

#if java
...
  ba.raiseEventFromUI(this, "callback", data); 
...
#End If

Now to find a way to pass a Byte Array Parameter from B4X to inline java.

Thanks for your help.
 
Upvote 0
Top