Mobile application using IR emitter with Delphi XE5 or higher

Asked

Viewed 834 times

3

I would like to know if there is the possibility of developing a mobile application in Delphi XE5 or higher, where it could send Infrared (IR) signal to any receiver, for example a TV.

  • Since there is hardware dependency, you will need to know if the device has such a feature and if the operating system makes such an API available. Your question is very general.

  • It may even be generic, but responsive, I wonder if anyone has ever done any application in Delphi XE5+ that uses the IR sensor, whether there is already component or some library for the devices that contemplate this sensor.

1 answer

3

I think I get your idea, you want to make a kind of universal control for your home and operate it by cell phone.

If it is to sell the application, I suggest you make an infrared emitter that works with network and that you operate the same by Wifi phone, because most devices today do not have this hardware included.

But if it is for specific use this has already been done just search the Play Store you find several of them.

Regarding Delphi, I believe there is more literature and documentation for Java.

Take a look at this and look for more videos about it. https://www.youtube.com/watch?v=3lqEzpZeBwo

In the matter of Java I have:

Infrared transmitters

When running on a device that includes an infrared (IR) transmitter, you can now transmit IR signals using the Consumerirmanager Apis. To get an instance of Consumerirmanager, call getSystemService() with CONSUMER_IR_SERVICE as argument. You can then query supported IR frequencies from the device with getCarrierFrequencies() and transmit signals by passing your desired frequency and signal pattern with Transmit().

You should always check first if the device includes an IR transmitter by calling the hasIrEmitter() method, but if your application is only compatible with devices that have one, you should include an element in your manifest for "android.hardware.consumerir"( FEATURE_CONSUMER_IR ).

Original "Infrared Transmitters

When running on a device that includes an Infrared (IR) Transmitter, you can now Transmit IR Signals using the Consumerirmanager Apis. To get an instance of Consumerirmanager, call getSystemService() with CONSUMER_IR_SERVICE as the argument. You can then query the device’s supported IR Frequencies with getCarrierFrequencies() and Transmit Signals by Passing your desired Frequency and Signal Pattern with Transmit().

You should Always first check whether a device includes an IR Transmitter by Calling hasIrEmitter(), but if your app is compatible only with Devices that do have one, you should include a element in your manifest for "android.hardware.consumerir" (FEATURE_CONSUMER_IR)."

Source https://developer.android.com/about/versions/android-4.html#Wireless

The Consumerirmanager class: https://developer.android.com/reference/android/hardware/ConsumerIrManager.html

I believe it only works on Kitkat but it’s not certain. link Kitkat: https://developer.android.com/about/versions/kitkat.html

Documentation of XDA developpers http://forum.xda-developers.com/showthread.php?t=1670289

I found this code example

    package com.rngtng.irdude;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;

public class MainActivity extends Activity {
    Object irdaService;
    Method irWrite;
    SparseArray<String> irData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        irData = new SparseArray<String>();
        irData.put(
                R.id.buttonPower,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 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 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonVolDown,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 0015 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonMute,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonVolUp,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonTuner,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonPhono,
                hex2dec("0000 006d 0022 0002 0154 00aa 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0041 0014 0016 0014 0016 0014 0016 0014 0016 0014 0041 0013 0041 0014 0041 0014 0016 0014 0016 0014 0041 0013 0017 0013 0017 0013 0017 0013 0017 0013 0041 0014 0041 0014 0041 0013 0017 0013 0041 0014 0041 0014 0041 0013 0041 0014 0016 0014 0016 0014 06bf 0154 0056 0014 0e6b"));
        irData.put(
                R.id.buttonCD,
                hex2dec("0000 006d 0022 0002 0152 00aa 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0696 0152 0055 0015 0e23"));
        irData.put(
                R.id.buttonAux,
                hex2dec("0000 006d 0022 0002 0154 00aa 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0016 0014 0041 0014 0016 0014 0016 0014 0016 0014 0016 0014 0041 0013 0041 0014 0041 0014 0016 0014 0016 0014 0041 0013 0041 0014 0041 0014 0016 0014 0016 0014 0041 0013 0041 0014 0041 0014 0016 0014 0016 0014 0016 0014 0041 0013 0041 0014 0016 0014 0016 0014 06bf 0154 0057 0013 0e6b"));

        irInit();
    }

    public void irInit() {
        irdaService = this.getSystemService("irda");
        Class c = irdaService.getClass();
        Class p[] = { String.class };
        try {
            irWrite = c.getMethod("write_irsend", p);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void irSend(View view) {
        String data = irData.get(view.getId());
        if (data != null) {
            try {
                irWrite.invoke(irdaService, data);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    protected String hex2dec(String irData) {
        List<String> list = new ArrayList<String>(Arrays.asList(irData
                .split(" ")));
        list.remove(0); // dummy
        int frequency = Integer.parseInt(list.remove(0), 16); // frequency
        list.remove(0); // seq1
        list.remove(0); // seq2

        for (int i = 0; i < list.size(); i++) {
            list.set(i, Integer.toString(Integer.parseInt(list.get(i), 16)));
        }

        frequency = (int) (1000000 / (frequency * 0.241246));
        list.add(0, Integer.toString(frequency));

        irData = "";
        for (String s : list) {
            irData += s + ",";
        }
        return irData;
    }

}

Another code that you can use as an Open Sourse basis from google

    /*
 * Copyright (C) 20013The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.apis.hardware;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.hardware.ConsumerIrManager;
import android.view.View;
import android.widget.TextView;
import android.util.Log;
import com.example.android.apis.R;
/**
 * App that transmit an IR code
 *
 * <p>This demonstrates the {@link android.hardware.ConsumerIrManager android.hardware.ConsumerIrManager} class.
 *
 * <h4>Demo</h4>
 * Hardware / Consumer IR
 *
 * <h4>Source files</h4>
 * <table class="LinkTable">
 *         <tr>
 *             <td>src/com.example.android.apis/hardware/ConsumerIr.java</td>
 *             <td>Consumer IR demo</td>
 *         </tr>
 *         <tr>
 *             <td>res/any/layout/consumer_ir.xml</td>
 *             <td>Defines contents of the screen</td>
 *         </tr>
 * </table>
 */
public class ConsumerIr extends Activity {
    private static final String TAG = "ConsumerIrTest";
    TextView mFreqsText;
    ConsumerIrManager mCIR;
    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
        // Get a reference to the ConsumerIrManager
        mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);
        // See assets/res/any/layout/consumer_ir.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.consumer_ir);
        // Set the OnClickListener for the button so we see when it's pressed.
        findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
        findViewById(R.id.get_freqs_button).setOnClickListener(mGetFreqsClickListener);
        mFreqsText = (TextView) findViewById(R.id.freqs_text);
    }
    View.OnClickListener mSendClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (!mCIR.hasIrEmitter()) {
                Log.e(TAG, "No IR Emitter found\n");
                return;
            }
            // A pattern of alternating series of carrier on and off periods measured in
            // microseconds.
            int[] pattern = {1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625, 442, 625, 442, 625,
                468, 625, 442, 625, 494, 572, 1614, 625, 1588, 625, 1614, 625, 494, 572, 442, 651,
                442, 625, 442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442, 625, 494, 598,
                442, 625, 442, 625, 520, 572, 442, 625, 442, 625, 442, 651, 1588, 625, 1614, 625,
                1588, 625, 1614, 625, 1588, 625, 48958};
            // transmit the pattern at 38.4KHz
            mCIR.transmit(38400, pattern);
        }
    };
    View.OnClickListener mGetFreqsClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            StringBuilder b = new StringBuilder();
            if (!mCIR.hasIrEmitter()) {
                mFreqsText.setText("No IR Emitter found!");
                Log.e(TAG, "No IR Emitter found!\n");
                return;
            }
            // Get the available carrier frequency ranges
            ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR.getCarrierFrequencies();
            b.append("IR Carrier Frequencies:\n");
            for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
                b.append(String.format("    %d - %d\n", range.getMinFrequency(),
                            range.getMaxFrequency()));
            }
            mFreqsText.setText(b.toString());
        }
    };
}

I found this more documentation: http://www.irdroid.com/

Here’s a full project on github https://github.com/Irdroid/Irdroid

Use this one as inspiration http://www.myuremote.com/website/how-to-add-ir-commands-to-the-myuremote-device-database/

  • Yes I want to make a universal control for my home, but unfortunately air conditioning for example does not come with wifi, so I can not control with my smartphone, I know there are already application in google play, but there are specific hardware that I manufacture with IR receiver, what I’m looking for is a component or DLL that I can use in Delphi to resolve my situation.

  • 1

    Then last year I had this same idea that you, I researched a lot, I found some interesting things even. but I came to the conclusion that it would be much easier to create an infrared emitter system with those diffusers (same presence sensor) and a WIFI or Bluetooth module and put one by commode. at the time I had quoted this, I think it would come out something around 10 more finish, so you can always issue IV to all the equipment of your house being in your room for example

  • 1

    I added some documentation I found

  • I was just reading about the Apis, and it’s really only going to work with Kitkat, but it doesn’t matter.

  • 1

    I added a code I found on the net (I don’t know if it works) and a documentation from XDA devellopers

  • 1

    added other information and a source of inspiration for your project design

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.