MCPcopy Index your code
hub / github.com/OneButtonDeveloper/AndroidInfraRed

github.com/OneButtonDeveloper/AndroidInfraRed @v3.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.3 ↗ · + Follow
136 symbols 302 edges 28 files 3 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Android Infra Red

Open source project for transmitting signals via IR-blasters on Android devices

Created in Android Studio

Supports Samsung, HTC, some LG devices with IR

Contains: - SampleApplication - InfraRed library

Email: one.button.developer@gmail.com

Menu

Basis

Basic theory

In order to remotely control a device via IR, you require a transmitter (mobile phone with IR blaster) and a control signal sequence that you want to send. This sequence is the key to the function that the device is supposed to perform (e.g. the function of a TV to turns itself on). The built-in IR receiver of the controlled device analyzes all incoming signals. When they match a pattern of a particular function, the devices calls that function.

The control signal sequence consists of two parameters: The frequency (in Hertz) and the pattern, which is a time sequence of ON/OFF signals on the specified carrier frequency. Such patterns can be found on the Internet.

Sequence types

The pattern is pictured by a sequence of numbers either of the hexadecimal (HEX) or the decimal (DEC) numeral system.

Here's an example:

Frequency:
  33000 Hertz (33KHz)
Patterns:
  0) 0x01f4 0x1c84 0x01f4 0x00c8 or (01f4 1c84 01f4 00c8)
  1) 500 7300 500 200
  2) 17 241 17 7

The main difference between the patterns in this example is their notation. Each number in the pattern describes the duration of a single ON/OFF signal, only that patterns 0 and 1 describe it in microseconds, while patterns 2 describes it in cycles.

One cycle always consists of an an ON and an OFF signal (the image below shows cycles with a frequency of 40000 Hz).

Difference between sequences

Converting the patterns

The length of a single period T in microseconds equals 1 000 000 (1 second in microseconds) divided by the frequency f (in Hertz)

T = 1 000 000 ms / f

For example

T = 1 000 000 ms/ 33000 Hz ≈ 30.3 ms

To covert a microsecond pattern to a cycle pattern, divide each value by T

To convert a cycle pattern to a microsecond pattern, simply multiply each value by T

How to use

0. Include the InfraRed library to your project

  • Download the AndroidInfraRed project
  • Run the project on a device or an emulator
  • Find the infrared-release.aar file in the project folder (infrared/build/outputs/aar/infrared-release.aar)
  • Import this file into your own project in Android Studio (File > New > New Module... > Import .JAR/.AAR package)

Tip! Your can download the infrared-release.aar file from releases

Known issue! One of the project files contains broken characters. Just rename it again.

1. Add the following permissions to your AndroidManifest.xml

    <uses-permission android:name="android.permission.TRANSMIT_IR" android:required="false"/>
    <uses-feature android:name="android.hardware.consumerir" android:required="false"/>

2. Initialize logging

You can choose between printing your logs to an EditText view (LogToEditText), the LogCat console (LogToConsole) and not logging at all (LogToAir)

    // print log messages to EditText
    EditText console = (EditText) this.findViewById(R.id.console);
    log = new LogToEditText(console, TAG);

    // print Log messages with Log.d(), Log.w(), Log.e() (LogCat)
    // LogToConsole log = new LogToConsole(TAG);

    // Turn off logs
    // LogToAir log = new LogToAir(TAG);

LogToEditText is the default option

3. Initialize the InfraRed class

Create a new InfraRed object with the parameters Context and Logger

    infraRed = new InfraRed(this, log);

4. Initialize the transmitter

    TransmitterType transmitterType = infraRed.detect();

    // initialize transmitter by type
    infraRed.createTransmitter(transmitterType);

The "detect()" method returns the TransmitterType. Possible values:

  • Actual - The signals will be transmitted by the ConsumerIrManager
  • Obsolete - The signals will be transmitted by a Samsung IR service
  • HTC - The signals will be transmitted by the HTC IR SDK
  • LG, LG_WithOutDevice - The signals will be transmitted by the QRemote SDK
  • Undefined - the "empty" implementation will be used to transmitt the signals

5. Initialize the raw patterns

    // initialize raw patterns
    List<PatternConverter> rawPatterns = new ArrayList<>();
    // Canon
    // rawPatterns.add(new PatternConverter(PatternType.Intervals, 33000, 500, 7300, 500, 200));
    // Nikon D7100 v.1
    rawPatterns.add(new PatternConverter(PatternType.Cycles, 38400, 1, 105, 5, 1, 75, 1095, 20, 60, 20, 140, 15, 2500, 80, 1));
    // Nikon D7100 v.2
    rawPatterns.add(new PatternConverter(PatternType.Cycles, 38400, 77, 1069, 16, 61, 16, 137, 16, 2427, 77, 1069, 16, 61, 16, 137, 16));
    // Nikon D7100 v.3
    rawPatterns.add(new PatternConverter(PatternType.Intervals, 38000, 2000, 27800, 400, 1600, 400, 3600, 400, 200));
    // Nikon D7100 v.3 fromString
    rawPatterns.add(PatternConverterUtils.fromString(PatternType.Intervals, 38000, "2000, 27800, 400, 1600, 400, 3600, 400, 200"));
    // Nikon D7100 v.3 fromHexString
    rawPatterns.add(PatternConverterUtils.fromHexString(PatternType.Intervals, 38000, "0x7d0 0x6c98 0x190 0x640 0x190 0xe10 0x190 0xc8"));
    // Nikon D7100 v.3 fromHexString without 0x
    rawPatterns.add(PatternConverterUtils.fromHexString(PatternType.Intervals, 38000, "7d0 6c98 190 640 190 e10 190 c8"));

The PatternConverter can be used to easily define and convert IR patterns between the different patterns types (Cycles and Intervals in ms)

6. Adapt the patterns for the device that is used to transmit the patterns

    PatternAdapter patternAdapter = new PatternAdapter(log);

    // initialize TransmitInfoArray
    TransmitInfo[] transmitInfoArray = new TransmitInfo[rawPatterns.size()];
    for (int i = 0; i < transmitInfoArray.length; i++) {
        transmitInfoArray[i] = patternAdapter.createTransmitInfo(rawPatterns.get(i));
    }
    this.patterns = transmitInfoArray;

PatternAdapter automatically detects the device's manufacturer (LG/HTC/Samsung) and uses that information to determine how the raw patterns have to be converted in order to be compatible with the transmitter of the used device.

7. Implement the start and stop methods

    @Override
    protected void onResume() {
        super.onResume();
        infraRed.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        infraRed.stop();

        if (log != null) {
            log.destroy();
        }
    }

Always call the start() method before transmit IR signals and call the stop() method when you are done.

It is a good idea to call them in the onCreate/onDestroy or onResume/onPause methods.

8. Transmit the adapted patterns via IR

    private int currentPattern = 0;

    @Override
    public void onClick(View v) {
        TransmitInfo transmitInfo = patterns[currentPattern++];
        if (currentPattern >= patterns.length) currentPattern = 0;
        infraRed.transmit(transmitInfo);
    }

Now we can send IR signals (TransmitInfo) to the device we want to control

In addition

Examples for the individual steps can be found in this project

What's in the box?

Android Project - Sample Application

The sample project gives you an idea how the infrared library is supposed to be used. The project contains an activity (MainActivity.java) with a predefined layout (activity_main.xml). The layout contains a console (EditText) and a IR button.

The onCreate() method contains the full initialization of the InfraRed library

When you run the application, the console will show you what is happening inside the library. When you click on the button, the IR blaster on your device will send a IR signal, of course only if your device is supported by the library.

Please note that you wont get any runtime exceptions because your device is not supported.

Logging to a EditText can be quite useful when you want to understand what's happening on your customer's devices. I often send special .apk files to users in order to obtain a log from their devices.

Android Library - Infrared

This library allows you to easily create raw IR signals in code, to detect device types and adapt raw signals for specific devices, to detect the devices' IR transmitters and to use them to send the adapted IR signals to a devices with a IR receiver.

The Library consists of several packages that are explained below.

Logging package

Provides different ways to log data. All classes in this package inherit from the Logger class.

LogToConsole is useful when your can debug the application on a device that is connected to an IDE with LogCat.

LogToEditText can be used to to display the log messages on the user's screen.

LogToAir won't log anything (you can apply this in your application's release version)

Pattern package

Enum PatternType tetermines the type of the IR signals in an integer array (a single value in the array is either the number of cycles or

Extension points exported contracts — how you extend this code

IDetector (Interface)
(no doc) [8 implementers]
infrared/src/main/java/com/obd/infrared/detection/IDetector.java

Core symbols most depended-on inside this repo

log
called by 78
infrared/src/main/java/com/obd/infrared/log/Logger.java
error
called by 20
infrared/src/main/java/com/obd/infrared/log/Logger.java
isDevice
called by 4
infrared/src/main/java/com/obd/infrared/detection/DeviceDetector.java
convertDataTo
called by 4
infrared/src/main/java/com/obd/infrared/patterns/PatternConverter.java
toString
called by 3
infrared/src/main/java/com/obd/infrared/transmit/TransmitInfo.java
isLg
called by 3
infrared/src/main/java/com/obd/infrared/detection/DeviceDetector.java
getFrequency
called by 3
infrared/src/main/java/com/obd/infrared/patterns/PatternConverter.java
warning
called by 2
infrared/src/main/java/com/obd/infrared/log/Logger.java

Shape

Method 105
Class 27
Enum 3
Interface 1

Languages

Java100%

Modules by API surface

infrared/src/main/java/com/obd/infrared/transmit/concrete/HtcTransmitter.java11 symbols
infrared/src/main/java/com/obd/infrared/transmit/concrete/LgTransmitter.java10 symbols
infrared/src/main/java/com/obd/infrared/log/LogToEditText.java8 symbols
infrared/src/main/java/com/obd/infrared/log/Logger.java7 symbols
infrared/src/main/java/com/obd/infrared/InfraRed.java7 symbols
infrared/src/main/java/com/obd/infrared/transmit/Transmitter.java6 symbols
infrared/src/main/java/com/obd/infrared/patterns/PatternConverterUtils.java6 symbols
infrared/src/main/java/com/obd/infrared/patterns/PatternConverter.java6 symbols
app/src/main/java/com/obd/infrared/sample/MainActivity.java6 symbols
infrared/src/main/java/com/obd/infrared/transmit/concrete/LgWithDeviceTransmitter.java5 symbols
infrared/src/main/java/com/obd/infrared/patterns/PatternAdapter.java5 symbols
infrared/src/main/java/com/obd/infrared/log/LogToConsole.java5 symbols

For agents

$ claude mcp add AndroidInfraRed \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact