Mobile application using IR emitter with Delphi XE5 or higher

3

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

    
asked by anonymous 17.10.2014 / 21:49

1 answer

3

I think I understood 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 the network and that you operate the same by the WiFi of the phone, because most of the devices today do not have this hardware included.

But if it's for specific use this has already been done just look in 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. link

In 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 the getSystemService () method with CONSUMER_IR_SERVICE as the argument. You can then query for supported IR frequencies of the device with getCarrierFrequencies () and transmit signals by passing their desired frequency and signal pattern with transmit ().

You should always first check if your device includes an IR transmitter by calling the hasIrEmitter () method, but if your application is only compatible with devices that have one, you must 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 check if a device includes IR transmitter by calling hasIrEmitter (), but if your app is compatible with devices that have one, you should include an element in your manifest for "android.hardware.consumerir "(FEATURE_CONSUMER_IR)."

Source link

The ConsumerIrManager class: link

I think it only works on KitKat but it's not sure. link kitkat: link

XDA developpers documentation link

I found this example code

    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 you can use as a google sourse Open

    /*
 * 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: link

Here a complete project in github link

Use this here for inspiration link

    
30.10.2014 / 12:54