B4A Library IrdaManager

The IrdaManager library is exclusively for use with Samsung devices.
It exposes the hidden Samsung IrdaManager system servcie, enabling you to use the device's IR transmitter to send IR codes.

For more info take a look at this thread: http://www.b4x.com/forum/basic4android-updates-questions/29902-what-hardware-does-b4a-support.html

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.

Here's a simple example showage usage syntax:

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

As you can see the WriteIrSend (DecimalData As String) As Boolean method accepts a String and this String should be a decimal IR code sequence.
If you get a code from somewhere that is a hexadecimal IR sequence then you will need to use the Hex2Dec (HexData As String, Separator As String) As String method to convert the hex sequence to a decimal sequence suitable for use with WriteIrSend.

The library files, example project and library source code are attached.

Martin.
 

Attachments

  • IrdaManager_0.02.zip
    14.7 KB · Views: 652

scsjc

Well-Known Member
Licensed User
Longtime User
I tried this one works:

B4X:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim button1,button2 As Button
  Dim IrdaHelper1 As IrdaHelper
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
  button1.Initialize("button1")
  button2.Initialize("button2")
  Activity.AddView(button1,0,0,100%x,100dip)
  Activity.AddView(button2,0,150dip,100%x,100dip)
IrdaHelper1.Initialize

End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
  Log("send test")
Dim frequency As Int
Dim Pattern() As Int
Dim IrObject As TransmitData
frequency=38400
'Pattern already in microseconds
Pattern=Array As Int(9000, 4500, 500,500, 500,1300,500,500, 500, 500, 500,4000)
IrObject=IrdaHelper1.GetTransmitData(frequency, Pattern)
IrdaHelper1.Transmit(IrObject)


End Sub
Sub Button2_Click
  Log("send test")
Dim frequency As Int
Dim Pattern1() As Int
Dim IrObject As TransmitData
frequency=38400
'Pattern in pulse, calculate to convert into microseconds
Pattern1=Array As Int(346,173,20,20,20,50,20,20,20,20,20,154)
Dim Pattern2(Pattern1.Length) As Int


For i=0 To Pattern1.Length-1
Dim i2 As Int
i2=Round(1000000*Pattern1(i)/frequency)
Pattern2(i)=i2
Next
IrObject=IrdaHelper1.GetTransmitData(frequency, Pattern2)
IrdaHelper1.Transmit(IrObject)


End Sub



one question more:

i have this patern...
0000 0073 0000 000c 0020 0020 0020 0020 0040 0020 0020 0020 0020 0020 0020 0020 0020 0020 0020 0040 0040 0020 0020 0020 0020 0020 0020 0cce

and work perfectly like:
frequency: 38000 (i put manual!!!)
patern: 0020 0020 0020 0020 0040 0020 0020 0020 0020 0020 0020 0020 0020 0020 0020 0040 0040 0020 0020 0020 0020 0020 0020 0cce (making your formula)

but the first 4 hexcodes " 0000 0073 0000 000c " can i convert to 38.000 from any calculate ???

thanks !!!!
 

aleung

Member
Licensed User
Longtime User
one question more:

i have this patern...
0000 0073 0000 000c 0020 0020 0020 0020 0040 0020 0020 0020 0020 0020 0020 0020 0020 0020 0020 0040 0040 0020 0020 0020 0020 0020 0020 0cce

and work perfectly like:
frequency: 38000 (i put manual!!!)
patern: 0020 0020 0020 0020 0040 0020 0020 0020 0020 0020 0020 0020 0020 0020 0020 0040 0040 0020 0020 0020 0020 0020 0020 0cce (making your formula)

but the first 4 hexcodes " 0000 0073 0000 000c " can i convert to 38.000 from any calculate ???

thanks !!!!

The pattern you have is pronto format. You can google search and find how the format descript or this is one of the examples: http://www.remotecentral.com/features/irdisp2.htm
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

Thank you for this lib !

I have struggled a little but finally it works like a charm (galaxy S5 + lollipop) :)

Here are the two lines to respectively trigger the shutter of any EOS Canon DSLR and same thing with 2s delay :

B4X:
Sub BtnShoot_Click
        'IrdaHelper1.Transmit(IrdaHelper1.GetTransmitData2("32898,16,240,16,240"))         'freq, nb pulses before lollipop
        IrdaHelper1.Transmit(IrdaHelper1.GetTransmitData2("32898,352,7330,352,7330"))     'freq, µs for lollipop
End Sub
Sub BtnShoot2s_Click
            IrdaHelper1.Transmit(IrdaHelper1.GetTransmitData2("32898,352,5360,352,5360"))  
End Sub

Canon EOS remote info here : http://www.doc-diy.net/photo/rc-1_hacked/
 

Juan Marrero

Active Member
Licensed User
Longtime User
in case someone need this.
I had problems using this library with my HTC One M8. It didn't work at all. Then I found something interesting on the web.
B4X:
#If JAVA
import android.os.Build;

/*
 * preforms some calculations on the codesets we have in order To make them work with certain models of phone.
 *
 * HTC devices need formula 1
 * Samsungs want formula 2
 *
 * Samsung Pre-4.4.3 want nothing, so just Return the input data
 *
 */
private static int[] string2Dec(int[] irData, int frequency) {
  int formula = shouldEquationRun();

  //Should we run any computations on the irData?
  if (formula != 0) {
  for (int i = 0; i < irData.length; i++) {
  if (formula == 1) {
  irData[i] = irData[i] * (1000000/frequency);//the brackets should avoid an arithmetic overflow
  } else if (formula == 2) {
  irData[i] = (int) Math.ceil(irData[i] * 26.27272727272727); //this is the samsung formula as per http://developer.samsung.com/android/technical-docs/Workaround-to-solve-issues-with-the-ConsumerIrManager-in-Android-version-lower-than-4-4-3-KitKat
  }
  }
  }
  return irData;
}

private static int shouldEquationRun() {
  //Some notes on what Build.X will Return
  //System.out.println(Build.MODEL); //One M8
  //System.out.println(Build.MANUFACTURER); //htc
  //System.out.println(Build.VERSION.SDK_INT); //19

  //Samsung's way of finding out if their OS is too new to work without a formula:
  //int lastIdx = Build.VERSION.RELEASE.lastIndexOf(".");
  //System.out.println(Build.VERSION.RELEASE.substring(lastIdx+1)); //4

  //handle HTC
  if (Build.MANUFACTURER.equalsIgnoreCase("HTC")) {
  return 1;
  }
  //handle Lollipop (Android 5.0.1 == SDK 21) / beyond
  if (Build.VERSION.SDK_INT >= 21) {
  return 1;
  }
  //handle Samsung PRE-Android 5
  if (Build.MANUFACTURER.equalsIgnoreCase("SAMSUNG")) {
  if (Build.VERSION.SDK_INT >= 19) {
  int lastIdx = Build.VERSION.RELEASE.lastIndexOf(".");
  int VERSION_MR = Integer.valueOf(Build.VERSION.RELEASE.substring(lastIdx + 1));
  if (VERSION_MR < 3) {
  // Before version of Android 4.4.2
  //Note: NO formula here, Not even the other one
  return 0;
  } else {
  // Later version of Android 4.4.3
  //run the special samsung formula here
  return 2;
  }
  }
  }
  //If something Else...
  return 0;
}
#End If
There is a formula for HTC (same as Android Version 21+) and a formula for Samsung with version 4.4.3 or later. Previous to these exceptions no formula is needed.
So I translated these functions to b4a.
B4X:
Sub string2Dec(irData() As Int, freq As Int) As Int()
   Dim formula As Int = shouldEquationRun
   
   If formula <> 0 Then
     For i = 0 To irData.Length - 1
       If formula = 1 Then
         irData(i) = irData(i) * (1000000/freq)
       Else
         irData(i) = Ceil(irData(i) * 26.27272727272727)
       End If
     Next
   End If
   
   Return irData
End Sub

Sub shouldEquationRun() As Int
   Dim ph As Phone
   
   If ph.Manufacturer.ToUpperCase = "HTC" Then
     Return  1
   End If
   
   If ph.SdkVersion >= 21 Then
     Return 1
   End If
   
   Return 0
End Sub
I did not include the "If" part of samsung because this code is for a personal app to handle my remotes at home but you can add it. Also you can use JavaObject to access the Java code posted first (#If JAVA).
 

Licht2002

Member
Licensed User
Longtime User
Hi,

is it possible to use this lib with an external IR

28159146je.jpg


Thx
Tom
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Learned string.

Trying to send this Pronto Learned code:
"0000 007e 0022 0000 000a 0046 000a 0046 000a 0046 000a 001e 000a 001e 000a 001e 000a 0046 000a 001e 000a 001e 000a 0046 000a 001e 000a 001e 000a 001e 000a 001e 000a 001e 000a 001e 000a 036a 000a 0046 000a 0046 000a 0046 000a 001e 000a 001e 000a 001e 000a 0046 000a 001e 000a 001e 000a 0046 000a 001e 000a 001e 000a 001e 000a 001e 000a 001e 000a 001e 0009 0000"

And keep getting java.lang.IllegalArgumentException: Non-positive IR slice

All my other learned codes work fine. Not sure what this message is telling me


OK: Update, found this website: http://irdb.tk/convert/ that lets me put in the Pronto Code and it converts it somehow. Put in the pronto code click Get Protocol Information then click on ProntoHex and the hex code there works without any problems (or at least the two I have tried)
 
Last edited:

Acuario

Member
Licensed User
Longtime User
I know this is an old thread but it might help anyone looking for an IrdaManager.
The library works correctly on a Xiaomi (mi) Redmi Note 9 Pro - I imagine it will work on any of their phones with IR.
 
Top