Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi Android how to use infrared sensor



Permlink Replies: 8 - Last Post: Feb 24, 2017 10:57 AM Last Post By: Ruslan Gilmanov
Guest
Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2016 12:56 AM
Delphi Android how to use infrared sensor

Hello everyone,
I need help to make an Android apk in Delphi XE7 to control a device through infrared .

I searched the net and found :

ConsumerIrManager mCIR = (ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);

But I could not find anything on how to implement it in delphi..

Thanks for your kind help

Gs
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2016 9:58 AM   in response to: Guest in response to: Guest
giorgio wrote:

I could not find anything on how to implement it in delphi..

Try something like this:

uses
  ...,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  FMX.Helpers.Android;
 
type
  JConsumerIrManager_CarrierFrequencyRange = interface;
 
  JConsumerIrManager_CarrierFrequencyRangeClass = interface(JObjectClass)
    ['{191A7F08-B93B-40D4-9B6E-3D908B37578E}']
    {class} function init(min, max: Integer): JConsumerIrManager_CarrierFrequencyRange; cdecl;
  end;
 
  [JavaSignature('android/hardware/ConsumerIrManager$CarrierFrequencyRange')]
  JConsumerIrManager_CarrierFrequencyRange = interface(JObject)
    ['{5AC0C73D-9DA2-49B0-A072-55EA03FE3B78}']
    function getMaxFrequency: Integer; cdecl;
    function getMinFrequency: Integer; cdecl;
  end; 
  TJConsumerIrManager_CarrierFrequencyRange = class(TJavaGenericImport<JConsumerIrManager_CarrierFrequencyRangeClass, JConsumerIrManager_CarrierFrequencyRange>) end;
 
  JConsumerIrManagerClass = interface(JObjectClass)
    ['{C8AB7EEA-046F-429C-8585-EF69EBB53718}']
  end;
 
  [JavaSignature('android/hardware/ConsumerIrManager')]
  JConsumerIrManager = interface(JObject)
    ['{FF469731-5B57-46AB-9C83-4FB51CA6F2BA}']
    function getCarrierFrequencies: TJavaArray<JConsumerIrManager_CarrierFrequencyRange>; cdecl;
    function hasIrEmitter: Boolean; cdecl;
    procedure transmit(carrierFrequency: Integer; pattern: TJavaArray<Integer>); cdecl;
  end;
  TJConsumerIrManager = class(TJavaGenericImport<JConsumerIrManagerClass, JConsumerIrManager>) end;
 
...
 
function MakeJavaIntArray(arr: array of Integer): TJavaArray<Integer>;
var
  I: Integer;
begin
  Result := TJavaArray<Integer>.Create(Length(arr));
  for I := Low(arr) to High(arr) do 
    Result.Items[I] := arr[I];
end;
 
var
  mCIR: JConsumerIrManager;
  mRanges: TJavaArray<JConsumerIrManager_CarrierFrequencyRange>;
  mRange: JConsumerIrManager_CarrierFrequencyRange;
  I, DesiredFrequency: Integer;
  HasDesiredFrequency: Boolean;
begin
  mCIR := TJConsumerIrManager.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.CONSUMER_IR_SERVICE) 
as ILocalObject).GetObjectID);
  if mCIR.hasIrEmitter then
  begin
    // use mCIR as needed...
 
    DesiredFrequency := ...;
    HasDesiredFrequency := False;
 
    mRanges := mCIR.getCarrierFrequencies;
    for I := 0 to mRanges.Length-1 do
    begin
      mRange := mRanges.Items[x];
      if (DesiredFrequency >= mRange.getMinFrequency) and (DesiredFrequency <= mRange.getMaxFrequency) then
      begin
        HasDesiredFrequency := True;
        Break;
      end;
    end;
 
    if HasDesiredFrequency then
      mCIR.transmit(DesiredFrequency, MakeJavaIntArray([pattern values here]));
  end;
end;


--
Remy Lebeau (TeamB)
Guest
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2016 4:23 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy ,
many thanks to the ' great help.

I tried the code but also giving all permissions when sending a command of infrared receive the java error eXcepiton : Require Transmit_ir permission.
I followed this code:
https://android.googlesource.com/platform/development/+/438ea813f1846f88205ff98436568aaa34f06845/samples/ApiDemos/src/com/example/android/apis/hardware/ConsumerIr.java

and I made a few attempts but still no reult.

GS
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2016 9:38 AM   in response to: Guest in response to: Guest
giorgio wrote:

I tried the code but also giving all permissions when sending a
command of infrared receive the java error eXcepiton : Require
Transmit_ir permission.

That is not a code issue, that is a permission issue. You need to edit your
project's AndroidManifest.xml file:

App Manifest
http://developer.android.com/guide/topics/manifest/manifest-intro.html

Android Application Manifest File (AndroidManifest.xml)
http://docwiki.embarcadero.com/RADStudio/en/Android_Application_Manifest_File

Customizing Your AndroidManifest.xml File
http://docwiki.embarcadero.com/RADStudio/en/Preparing_an_Android_Application_for_Deployment#Customizing_Your_AndroidManifest.xml_File

You need to enable the "TRANSMIT_IR" permission in order to use ConsumerIrManager.transmit(),
and you should also declare that your app uses the IR hardware so Google
Play and other services can filter your app accordingly:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    ...
    <uses-permission android:name="android.permission.TRANSMIT_IR" />
    <uses-feature android:name="android.hardware.consumerir" android:required="false" 
/>
    ...
</manifest>


--
Remy Lebeau (TeamB)
Guest
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2016 12:50 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
That is not a code issue, that is a permission issue. You need to edit your
project's AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >>     ...</div>
<%uses-permission%>
 <uses-permission android:name="android.permission.TRANSMIT_IR" />
    <uses-feature android:name="android.hardware.consumerir" android:required="false"/>
<div class="jive-quote"></manifest>

Hi,
i have enabled all permissions from Rad Sudio without success.
I took your advice and entered the permissions on the file manually AndroidManifest.xml.

Now with these two lines of code:

</div>
///////////////////////////////////////////////
procedure TForm1.Button2Click(Sender: TObject);
var
mCIR: JConsumerIrManager;
begin
 
// Get a reference to the ConsumerIrManager
 mCIR := TJConsumerIrManager.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.CONSUMER_IR_SERVICE)
as ILocalObject).GetObjectID);
 
Memo1.lines.Add('IR Emitter send');
 
mCIR.transmit(3800, MakeJavaIntArray([8950,4350,650,500,600,500,550,600,550,550,600,500,550,550,600,550,600,500,550,1650,600,1600,650,1600,550,1700,600,1600,600,1600,600,1650,600,1600,650,1600,550,1650,650,1600,600,550,550,550,550,550,550,550,600,500,600,550,550,550,550,550,550,1650,650,1600,600,1600,600,1650,600,1600,600])); //pattern values here
 
end;
 
/////////////////////////////////////////////////
 
<div class="jive-quote">

After a few attempts transmission my equipment was turned on..... I just have to perfect everything.

Remy, thanks so much for your precious and indispensable aid ....

GS
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2016 5:39 PM   in response to: Guest in response to: Guest
giorgio wrote:

i have enabled all permissions from Rad Sudio without success.

Do not enable permissions that you do not actually need.

I took your advice and entered the permissions on the file manually
AndroidManifest.xml.

The permission I mentioned ("TRANSMIT_IR") is not configurable in the Project
Options, so you have to enable it manually in the XML file directly. Same
with the hardware feature ("consumerir").

// Get a reference to the ConsumerIrManager

If you set "android:required=false" in the manifest XML for the consumerir
hardware feature, you MUST check ConsumerIrManager.hasIrEmitter() before
calling ConsumerIrManager.transmit(). Otherwise, set "android:required=true"
instead. You should probably check hasIrEmitter() either way, just as a
safety measure.

--
Remy Lebeau (TeamB)
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2016 10:16 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Am 29.01.2016 um 02:39 schrieb Remy Lebeau (TeamB):
giorgio wrote:

i have enabled all permissions from Rad Sudio without success.

Do not enable permissions that you do not actually need.

I took your advice and entered the permissions on the file manually
AndroidManifest.xml.

The permission I mentioned ("TRANSMIT_IR") is not configurable in the Project
Options, so you have to enable it manually in the XML file directly. Same
with the hardware feature ("consumerir").

// Get a reference to the ConsumerIrManager

If you set "android:required=false" in the manifest XML for the consumerir
hardware feature, you MUST check ConsumerIrManager.hasIrEmitter() before
calling ConsumerIrManager.transmit(). Otherwise, set "android:required=true"
instead. You should probably check hasIrEmitter() either way, just as a
safety measure.

Hello,

quite good answers from you.
Now I would have one wish for the OP: create a feature request type of
issue in quality.embarcadero.com and request that the non available
permissions are added to the list of permissions in the project options
dialogue. That would do everybody a favour I guess.

Greetings

Markus
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2016 9:45 AM   in response to: Guest in response to: Guest
Am 27.01.2016 um 13:23 schrieb giorgio santi:
Hi Remy ,
many thanks to the ' great help.

I tried the code but also giving all permissions when sending a command of infrared receive the java error eXcepiton : Require Transmit_ir permission.

Does this mean that the list of available permissions lacks the
Transmit_ir permission? if yes that would be a bug in Delphi and should
get reported by you to quality.embarcadero.com

Otherwise: Remy's advice looks really good!

Greetings

Markus

Ruslan Gilmanov

Posts: 1
Registered: 5/26/05
Re: Delphi Android how to use infrared sensor
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 21, 2017 7:00 AM   in response to: Guest in response to: Guest
giorgio santi wrote:

After a few attempts transmission my equipment was turned on..... I just have to perfect everything.


You should do Clean project.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02