Android Question NVIDIA SHIELD! Analog JoyStick Access

bbonilla

Member
Licensed User
Hello Erel

Any news on analog joystick support in B4A. I need to write the control app for android on the Nvidia Shield.
Thank you for the good work.
BB
 

PaulR

Active Member
Licensed User
I don't have any joysticks to test with, but I had a look at this, which looks like it could be implemented with JavaObject in b4a.
B4X:
Sub Globals
     Dim pnl As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    pnl.Initialize("")
    Dim jo As JavaObject = pnl
    Dim e As Object = jo.CreateEvent("android.view.View.OnGenericMotionListener", _
                     "pnl", False)
    jo.RunMethod("setOnGenericMotionListener", Array(e))
    Activity.AddView(pnl, 0, 0, 100%x, 100%y)
End Sub

Sub pnl_Event(methodName As String, args() As Object) As Boolean
    Dim motionEvent As JavaObject = args(1)
    Dim action As Int = motionEvent.RunMethod("getAction", Array())
    Dim deviceId As Int = motionEvent.RunMethod("getDeviceId", Array())
    Log("action: "&action)
    Log("deviceId: "&deviceId)
    Return True
End Sub
 
Last edited:

bbonilla

Member
Licensed User
I don't have any joysticks to test with, but I had a look at this, which looks like it could be implemented with JavaObject in b4a.
B4X:
Sub Globals
    Dim pnl As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    pnl.Initialize("")
    Dim jo As JavaObject = pnl
    Dim e As Object = jo.CreateEvent("android.view.View.OnGenericMotionListener", _
                     "pnl_OnGenericMotionListener", False)
    jo.RunMethod("setOnGenericMotionListener", Array(e))
    Activity.AddView(pnl, 0, 0, 100%x, 100%y)
End Sub

Sub pnl_OnGenericMotionListener(v As View, motionEvent As Object) As Boolean
    Log("GenericMotionEvent received")
    Return True
End Sub
********************************************************************************************
Thank you for your quick response, I'm very new at the B4A so I don't know a lot of things, My strengths are in the ATMEL firmware development.

I copy the your code and I got this error, what I'm missing? Please picture thank you.
P.S I moved the DIM pnl As Panel to the proper section.
 

Attachments

PaulR

Active Member
Licensed User
Sorry, that's my fault - the error is because b4a v3.8 has allowed 'Array' to be used as shorthand for 'Array as Object'. So it should be....
B4X:
jo.RunMethod("setOnGenericMotionListener", Array as Object(e))
.... for users of versions <3.8.
 

bbonilla

Member
Licensed User
Sorry, that's my fault - the error is because b4a v3.8 has allowed 'Array' to be used as shorthand for 'Array as Object'. So it should be....
B4X:
jo.RunMethod("setOnGenericMotionListener", Array as Object(e))
.... for users of versions <3.8.

Thank you so much for your time, it now compiles, but there are no events being capture by the pnl_OnGenericMotionListener sub.
So there is not much that can be done.
Thank you
 

Erel

Administrator
Staff member
Licensed User
Download the attached jar file and copy it to the additional libraries folder.

Add these lines to the activity:
B4X:
#AdditionalJar: ActivityEx
#Extends: anywheresoftware.b4a.objects.ActivityEx
This will allow you to handle the activity onGenericMotionEvent:
B4X:
Sub Activity_OnGenericMotionEvent(MotionEvent As Object)
   Log(MotionEvent)
End Sub
You can now use JavaObject to access MotionEvent.
 

Attachments

PaulR

Active Member
Licensed User
I've edited the code in post #2, which now works (at least it is producing action and device ids in Genymotion) - the event sub did not have the correct name so was never called. Sorry about the confusion.
 

bbonilla

Member
Licensed User
Download the attached jar file and copy it to the additional libraries folder.

Add these lines to the activity:
B4X:
#AdditionalJar: ActivityEx
#Extends: anywheresoftware.b4a.objects.ActivityEx
This will allow you to handle the activity onGenericMotionEvent:
B4X:
Sub Activity_OnGenericMotionEvent(MotionEvent As Object)
   Log(MotionEvent)
End Sub
You can now use JavaObject to access MotionEvent.

Thank you Erel, I now can log events and I see x and y values, how can I get access to these values.
thank you in advanced.
 

bbonilla

Member
Licensed User
Thank you Erel, I was able to get the x and y values for only one of the joysticks. But this gives me a good place to start.
 

bbonilla

Member
Licensed User
No I'm sorry but I had to stop, I was only able to get X&Y for one joystick, and it was taking to long, so I had to stop for now.
Thank you asking.
 
Top