Help with reflection!

Kevin

Well-Known Member
Licensed User
Longtime User
I'm trying to access an API (available only on select phones) and I think it can be done with reflection but I'm not too familiar with it and haven't had any success yet. Any reflection gurus out there?

I was made to sign an NDA to get information on this API so I am going to change some details to avoid potential legal troubles. If I am missing anything important, please let me know. :D

I tried the following but receive runtime errors. For now I am just trying to use the transmitIRCmd (or transmitIR) method to send an IR command, and would be nice to get the result back. Eventually I'd also like to use the learn method as well. I am trying to simulate the abcIrData constructor using my own type (ABCData) but I don't even know if that will work. Unfortunately this stuff is way over my head. :confused:

Although it isn't shown in the code, ABCDat gets declared as Type ABCData and the proper values are assigned as necessary.

B4X:
Type ABCData (rc As Int, freq As Int, irframe() As Int) ' irframe should be int array

      'Send command using built-in IR port
      Dim r As Reflector, xCount As Int
      r.Target = r.CreateObject("com.abc.circontrol.CIRControl")
      'r.Target = r.CreateObject("com.abc.htcircontrol.abcIrInterface")

      For xCount = 1 To RepeatCount
         r.RunMethod3 ("transmitIRCmd", ABCDat, "java.lang.object", "False", "java.lang.boolean")
         'r.RunMethod2 ("transmitIR", ABCDat, "java.lang.object")
      Next
      Catch
         Log (LastException)
         ToastMessageShow ("Failed to send IR command.  Is your device supported?", True)
   End Try


The API docs:



 

agraham

Expert
Licensed User
Longtime User
I am trying to simulate the abcIrData constructor using my own type (ABCData)
You can't, it won't work.

Your reflection code is miles off I'm afraid.

You need a Handler and a Context instance to pass to the CIRControl constructor.
You can create the Handler by reflection and use the Reflection.getContext for the context.
You need an abcIrData instance to pass to transmitIRCmd

Something like the following which won't work because I can't test it. It would be easier to knock up a library which in turn would be easier to add more functionality to.
B4X:
'Send command using built-in IR port
Dim rc, rh, rd As Reflector

' Handler
rh.Target = rh.CreateObject("android.os.Handler")

'com.abc.circontrol.CIRControl
Dim types(2) As String
Dim args(2) As Object
types(0) = "android.content.Context"
args(0) = rh.GetContext
types(1) = "android.os.Handler"
args(1) = rh.Target
rc.Target = rc.CreateObject2("com.abc.circontrol.CIRControl", args, types)

'com.abc.circontrol.abcIrData 
Dim types(3 As String
Dim args(3) As Object
types(0) = "I"
args(0) = rcValueorVar ' probably need an int var not a literal to get an int type
types(1) = "I"
args(1) = freqValueOrVar ' probably need an int var not a literal to get an int type
types(1) = "[I"
args(1) = intArrayVar
rd.Target = rd.CreateObject2("com.abc.circontrol.abcIrData",args, types)

'run transmitIRCmd method
Dim types(2) As String
Dim args(2) As Object
types(0) = "com.abc.circontrol.abcIrData"
args(0) = rd.Target
types(1) = "Z"
args(1) = boolvar ' probably need a boolean var not a literal to get a boolean type
rc.RunMethod4("transmitIRCmd", args, types)
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Thanks!

I have the correct phone to test this on but I am receiving the following error when running the app:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException:

This occurs on the following line:

B4X:
rd.Target = rd.CreateObject2("com.abc.abcircontrol.abcIrData", args, types)

(and for the record, yes I changed the actual code to use the correct package name).

The docs say that abcIrData is in fact part of that package, so I don't know why the error is occurring.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Still no joy...



Just not having much luck with this one...
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
B4X:
' Handler
Dim rh As Reflector
rh.Target = rh.CreateObject("android.os.Handler")

'com.abc.circontrol.CIRControl
Dim rc As Reflector
Dim types(2) As String
Dim args(2) As Object
types(0) = "android.content.Context"
args(0) = rh.GetContext
types(1) = "android.os.Handler"
args(1) = rh.Target
rc.Target = rc.CreateObject2("com.abc.circontrol.CIRControl", args, types)

'com.abc.circontrol.abcIrData
Dim rd As Reflector
Dim types(3) As String
Dim args(3) As Object
types(0) = "I"
args(0) = abcDat.rc
types(1) = "I"
args(1) = abcDat.freq
types(1) = "[I"
args(1) = irArray
rd.Target = rd.CreateObject2("com.abc.abcircontrol.CIRControl$abcIrData", args, types)

'run transmitIRCmd method
Dim types(2) As String
Dim args(2) As Object
types(0) = "com.abc.abcircontrol.CIRControl$abcIrData"
args(0) = rd.Target
types(1) = "Z"
args(1) = False ' might need a boolean var not a literal to get a boolean type
rc.RunMethod4("transmitIRCmd", args, types)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I see a cut and paste error in the indices for types(2) and args(2). Try
B4X:
Dim types(3 As String
Dim args(3) As Object
types(0) = "I"
args(0) = rcValueorVar ' probably need an int var not a literal to get an int type
types(1) = "I"
args(1) = freqValueOrVar ' probably need an int var not a literal to get an int type
types(2) = "[I"
args(2) = intArrayVar
rd.Target = rd.CreateObject2("com.abc.circontrol.abcIrData",args, types)
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Darn cut & paste errors will get you every time! I missed that as well.

Unfortunately even after fixing that, I am still getting the class not found errors. I'm really not sure why though.

I don't want to take any more of your time on it though. thedesolatesoul was able to make a library for me and we got it working this morning.

I did learn a bit about reflection though so it wasn't all for naught. Hopefully others might learn from it as well.

Thanks!
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
I know, its a really old post, but, if some one have a similar issue, the solution its really simple.


Your "abc" (htc) object is not part of Android, you have to put the JAR in the libs folder and the include it with the AdditionalJar


B4X:
#AdditionalJar: HTCIR
 
Upvote 0