B4J Question How to get input value from USB/HID ?

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I have install a laser bar code reader in Ubuntu. I try it open a google home page and then I can get/scan a real bar code into its input box. But i don't know how to get code from B4J ? Please help me to get the code !
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Sorry, It is NO-UI APP, Could i use Timer1_tick to poll it and then get it ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub Process_Globals
  Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
  Dim sys As JavaObject
  sys.InitializeStatic("java.lang.System")
  astream.Initialize(sys.GetField("in"), Null, "astream")
  StartMessageLoop
End Sub

Sub astream_NewData (Buffer() As Byte)
   Log("Received: " & BytesToString(Buffer, 0, Buffer.Length, "utf8").Trim)
End Sub
You will probably need to manually run the jar file (not with B4J-Bridge).
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
1. I test it in windows7. it always give me following exeception when i run
astream.Initialize(sys.GetField("in"), Null, "astream"). Why ?

2. I test it in Ubuntu. No exception and No respone when i scan a bar code. ???

3. the system has /dev/input/event3 When i plug bar code reader and scan a bar. Could i access from "/dev/input/event3" ?

B4X:
Waiting for debugger to connect...
Program started.
arg(0)=192.168.1.168
2016-10-11 08:21:45.595:INFO::main: Logging initialized @729ms
2016-10-11 08:21:45.691:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-10-11 08:21:45.769:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@843fce{/,file:///D:/Customer/%E4%B9%85%E5%85%81/case%2020160413/asia%2020161007M/asia_iot1/Objects/www/,AVAILABLE}
2016-10-11 08:21:45.779:INFO:oejs.AbstractNCSARequestLog:main: Opened D:\Customer\銋?\case 20160413\asia 20161007M\asia_iot1\Objects\logs\b4j-2016_10_11.request.log
2016-10-11 08:21:45.835:INFO:oejs.ServerConnector:main: Started ServerConnector@41eaa2{HTTP/1.1,[http/1.1]}{0.0.0.0:8001}
2016-10-11 08:21:45.835:INFO:oejs.Server:main: Started @972ms
Emulated network latency: 100ms
java.io.IOException: ?批隞?Ⅳ?⊥???
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:255)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:185)
    at java.lang.Thread.run(Thread.java:745)
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
How would the jNativeHookB4J library help? I think the jColorLogger library might help you capture Standard In without the EOL character. Or you could use the jFileWatcher library to monitor /dev/input/event3. These are both wild guesses.
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Oh, I see. I misunderstood how the scanner works. If it's behaving as a keyboard then, yes, jNativeHookB4J should be able to listen to its events. However, you'll get a series of individual key press events and release events that you'll have to put together into a String.

You can also use the jAWTRobot library to programmatically press the "Enter" key after the scanner had inputted its String into the console input.
 
Upvote 0
Top