Samsung Infrared

Charlie_M

Member
Licensed User
Longtime User
Hi everyone,

I just got a Samsung GalaxyS4 and I was wondering if B4A supports or if there are classes or libs for the infrared sensor and such? Thanks
 

ivan.tellez

Active Member
Licensed User
Longtime User
BAd News

I have an S4, and want to develop for IR, but found this on developer.samsung.com:


Hello,

I have bad news for you.
Unfortunately I can't help you, since we don't provide IR Api to the public.

Regards,
Adam Panasiuk
Samsung Developers




But, also found this:

https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java

http://www.haxd.me/index.php/2013/03/reversing-samsungs-irdamanager/

And an old post:

http://www.b4x.com/forum/basic4android-updates-questions/18324-samsung-galaxy-2-7-0-tablet.html

Sound like it's posible.


So, if someone expert in Java dould translate the https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java code to B4A, it will add support for the IR.
 
Last edited:
Upvote 0

Charlie_M

Member
Licensed User
Longtime User
Thanks for that post ivan.tellez I found a app that utilizes the temperature sensor, barometric sensor, and the humidity sensor on the 4S. I wish there were some libraries for those.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Does this work:

B4X:
public class IrdaManager {

   /**
    * IrdaManager exposes the hidden Samsung IrdaManager class.
    * http://www.haxd.me/index.php/2013/03/reversing-samsungs-irdamanager/
    * https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java
    */
   public static void LIBRARY_DOC(){}
   
   private Object mIrdaService=null;
   private Method mIrWrite=null;
   
   /**
    * Initialize the IrdaManager.
    * Returns True if successfully initialized, otherwise False.
    * If initialization fails the B4A LastException property will contain the related Exception.
    */
   public boolean Initialize(BA pBA){
      boolean success=false;
      mIrdaService=pBA.context.getSystemService("irda");
      Class<?> irdaServiceClass=mIrdaService.getClass();
      Class<?>[] p={String.class};
      try {
         mIrWrite=irdaServiceClass.getMethod("write_irsend", p);
         success=true;
      } catch (SecurityException e) {
         e.printStackTrace();
         pBA.setLastException(e);
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
         pBA.setLastException(e);
      }
      return success;
   }
   
   /**
    * Returns whether or not the IrdaManager is initialized.
    */
   public boolean IsInitialized(){
      return mIrWrite==null?false:true;
   }
   
   /**
    * Sends Data using the IrdaManager.
    * Returns True if successfully sent, otherwise False.
    * If send fails the B4A LastException property will contain the related Exception.
    */
   public boolean WriteIrSend(BA pBA, String Data){
      boolean success =false;
      try {
         mIrWrite.invoke(mIrdaService, Data);
         success=true;
      } catch (IllegalArgumentException e) {
         e.printStackTrace();
         pBA.setLastException(e);
      } catch (IllegalAccessException e) {
         e.printStackTrace();
         pBA.setLastException(e);
      } catch (InvocationTargetException e) {
         e.printStackTrace();
         pBA.setLastException(e);
      }
      return success;
   }
}

IrdaManager
Comment: IrdaManager exposes the hidden Samsung IrdaManager class.
RCE- und Codingblog
https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java
Version: 0.01
  • IrdaManager
    Methods:
    • Initialize As Boolean
      Initialize the IrdaManager.
      Returns True if successfully initialized, otherwise False.
      If initialization fails the B4A LastException property will contain the related Exception.
    • IsInitialized As Boolean
      Returns whether or not the IrdaManager is initialized.
    • WriteIrSend (Data As String) As Boolean
      Sends Data using the IrdaManager.
      Returns True if successfully sent, otherwise False.
      If send fails the B4A LastException property will contain the related Exception.

[edit]Library can now be found here: http://www.b4x.com/forum/additional...ial-updates/30552-irdamanager.html#post177515[/edit]

Martin.
 
Last edited:
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
Testing

Hi Martin.

Thanks for the new lib, Looks really nice.




B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   MyIR.Initialize
End Sub

Sub CmdPower_Click
   Log("IrdaManager: " & MyIR.IsInitialized)
   Log(MyIR.WriteIrSend("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"))
End Sub



In the log:

IrdaManager: true
true



And a blue led in the Galaxy S2 Bliks, the same as when using the Sammsung app.


I got the code from:

RC: Infrared Hex Code Database: Samsung TV Functions Commands (Page 1)



Its not working :(, twsted with Power, Ch+, Ch-, Vol+, and Vol-


I'm I doing something wrong in the code?


T
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look here: RCE- und Codingblog.

Search the page for write.invoke, you'll see an example:

B4X:
write.invoke(irdaService, "38400,173,171,24,62,24,61,24,62,24,17,24,17,24,18,24,17,24,17,24,62,24,61,24,62,24,17,24,19,23,17,24,17,24,17,24,62,24,61,25,61,24,62,24,18,23,17,24,17,25,17,24,17,24,17,24,18,24,17,24,62,24,61,24,62,24,61,24,1880");

Now take a look here: https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java.

See protected String hex2dec(String irData) {}?
I'd guess that's a helper function to convert IR codes from one format to another.

Can you test again with the comma separated values and report back if it works?

I can add the hex2dec method to the library if it is required or useful.

Martin.
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
its working

Yea, your right, there was a missing part; The hex needs be converted.

I changed This:

B4X:
Log(MyIR.WriteIrSend("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"))

For this:

B4X:
Log(MyIR.WriteIrSend(hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e")))

And Now its working. :sign0060:
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Great!

You created your own dex2dec routine i'm guessing?
I can add it to the library or leave anyone using the library to do the conversion themselves.

Martin.
 
Upvote 0

Charlie_M

Member
Licensed User
Longtime User
You can use the Phone library to utilize the temperature sensor.

Thanks Erel. There is so much to learn about B4a and Android. Thanks for all the help.

Thanks Martin, I will have to give your Lib a try.
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
custom hex2dec

yes, i just translate the hex2dec function from:

https://github.com/rngtng/IrDude/blob/master/src/com/rngtng/irdude/MainActivity.java


It converts the hex Data:

B4X:
"0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015
 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f
 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f
 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015
 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015
 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"

Into the string:

B4X:
"38028,169,168,21,63,21,63,21,63,21,21,21,21,21,21,21,21,21,21,21,63,21,
63,21,63,21,21,21,21,21,21,21,21,21,21,21,21,21,63,21,21,21,21,21,21,21,
21,21,21,21,21,21,64,21,21,21,63,21,63,21,63,21,63,21,63,21,63,21,1794,
169,168,21,21,21,3694"


In the lib, the method: WriteIrSend (Data As String) As Boolean,
works with the second string.

Im not really a Java expert, in the original code of hex2dec, i think there is a , at the end of the strig, is this correct?

I tested without this last , and it works.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Nice work Martin. Saves me a task as this was my next...
 
Upvote 0

Gary Miyakawa

Active Member
Licensed User
Longtime User
Guys,

This is GREAT work ! I was able to trigger, using your code, from my Galaxy Tablet. I'm working on the conversion of the codes from Hex to Dec and I'm good with everything except the very first value.

I figured it out after looking at the other source... Not terrible obvious but we'll see if it works tomorrow !

Thanks for the hard work here guys !

Gary M
 
Last edited:
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
hex2dec In B4A

Well, actually converting from Hex its easy:

B4X:
 Sub hex2dec(irData As String)As String
   Dim sparts() As String
   Dim MyList As List
   Dim frequency As Int
   Dim i As Int
   
   
   sparts = Regex.Split(" ", irData)
   MyList.Initialize()

   For i = 0 To sparts.Length -1
      MyList.Add(sparts(i))
   Next
   
   MyList.removeat(0) 'dummy
   frequency = Bit.ParseInt(MyList.Get(0),16)
   MyList.removeat(0)
   MyList.removeat(0) ' seq1
   MyList.removeat(0) ' seq2


   For i = 0 To MyList.Size -1
      MyList.Set(i, Bit.ParseInt(MyList.get(i), 16))
   Next

   frequency = (1000000 / (frequency * 0.241246))
   MyList.InsertAt(0, frequency)

   irData = ""
   
   For i = 0 To MyList.Size -1
      irData = irData & MyList.Get(i) 
      If i < MyList.Size -1 Then irData = irData & ","
   Next
   
   Log(irData)
   
   Return irData
End Sub


But, the real interesting part, would be to parse lirc config files in B4A and output a string to use with the Samsung IR



More info:

Project:
LIRC - Linux Infrared Remote Control

Remotes available:
Index of /remotes

Configuration File Format
Technical Details

Lirc on android:

Lirc howtos | Irdroid™
androidinfrared - Android Infrared Remote Control - Google Project Hosting



Any ideas?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I was just about to upload a library update with a new Hex2Dec method but shall upload it anyway.

IrdaManager
Version: 0.02
  • IrdaManager
    Methods:
    • Hex2Dec (HexData As String, Separator As String) As String
      Converts a String of Hex IR codes into a String of comma separated decimal codes suitable for use with WriteIrSend.
      Separator denotes the separator character used in the HexData input.
    • Initialize As Boolean
      Initialize the IrdaManager.
      Returns True if successfully initialized, otherwise False.
      If initialization fails the B4A LastException property will contain the related Exception.
    • IsInitialized As Boolean
      Returns whether or not the IrdaManager is initialized.
    • WriteIrSend (DecimalData As String) As Boolean
      Sends Data using the IrdaManager.
      Returns True if successfully sent, otherwise False.
      If send fails the B4A LastException property will contain the related Exception.

And a b4a example:

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim IrdaManager1 As IrdaManager
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If IrdaManager1.Initialize Then
      Log("IrdaManager successfully initialized")
      Dim HexData As String="0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"
      Dim DecData As String=IrdaManager1.Hex2Dec(HexData, " ")
      Log("DecData: "&DecData)
      
      If IrdaManager1.WriteIrSend(DecData) Then
         Log("WriteIrSend succeeded")
      Else
         Log("WriteIrSend failed: "&LastException)
      End If
   Else
      Log("Failed to initialize the IrdaManager: "&LastException)
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Strangely enough this example runs on my Galaxy Tab2!
The IrdaManager Initialize and WriteIrSend methods both return True.
Maybe Samsung include the IrdaManager API in all of their firmwares regardless of whether the device has the required IR hardware?

I don't have time to look at the Lirc stuff now, i'll try and find time later to have a look.

[edit]Library can now be found here: http://www.b4x.com/forum/additional...ial-updates/30552-irdamanager.html#post177515[/edit]

Martin.
 
Last edited:
Upvote 0

Gary Miyakawa

Active Member
Licensed User
Longtime User
I'm running the demo on the Galaxy 10.x Tab 2 (which, atleast in the US) has the IR Blaster... Also, the newly announced S4 Active and Mini BOTH claim to have the IR Blaster....

Thanks for the help guys !

Gary M
 
Upvote 0
Top