Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / WPF
Tip/Trick

Wi-Fi Remote for Sony TV

Rate me:
Please Sign up or sign in to vote.
4.78/5 (4 votes)
6 Mar 2021CPOL2 min read 18.5K   18   4
This tip describes an application to control with Sony TVs connected to your Home Wi-Fi network from PC.

 

Image 1

Introduction

In this tip, I will explain how I have used the SonyAPILib shared by HKHerron.

The article shared by HKHerron has well explained how to use the library. I am here creating a UI similar to the IR Remote device in Windows Forms and used the library to communicate to the TV.

The work is in a very new stage. Any suggestion for improvements will be welcomed.

Background

The basic idea behind working on this program was to control the TV from a separate room within home, since in my home the TV is also used as a Home Music system.

Using the Code

The API basically has four stages as you will find in the article sonyAPILib. Those are as follows:

  1. Discovery
  2. Initialization
  3. Registration
  4. Remote Access

Discovery

We need to be searching for all the devices in the network and storing the same in a List. This would include any Sony devices in the network be it a Smartphone or a Smart TV.

C#
SonyAPI_Lib mySonyLib = new SonyAPI_Lib();
SonyAPI_Lib.SonyDevice myTV = null;
List <SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);

Now, we need to find the exact device which needs to be connected. So far, I have seen that the Sony TV name starts with "KDL". Hence I am searching for any Device that has "KDL" in its name.

C#
foreach (SonyAPI_Lib.SonyDevice discoverDev in fDev)
{
    if (discoverDev.Name.Contains("KDL"))
    {
        myTV = discoverDev;
        break;
    }
}

Initialization

Once the device is identified, the Device needs to be initialized.

C#
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.initialize(myTV);

Registration

Since I am using a Generation 1 Sony TV, and had no option to test on Generation 2/3 devices, I am keeping the code simple. Once the device is initialized, the Generation 1 device will show a message on the TV screen in order to confirm the access. Once the confirmation is provided, the registration is complete.

Remote Access

Once the registration is complete, a Windows form pops-up. Here we have some buttons and the click events sending IRCC Name for commanding the connected device.

C#
private void butPower_Click(object sender, EventArgs e)
{
    try
    {
        com.ExecuteCommand(device, "PowerOff");
    }
    catch(Exception ea)
    {}
}

Classes in the Project

The following are the classes in the project.

RegisterDevice

C#
class RegisterDevice
{
    public SonyAPI_Lib.SonyDevice DeviceInitialization()
    {
        SonyAPI_Lib mySonyLib = new SonyAPI_Lib();
        SonyAPI_Lib.SonyDevice myTV = null;
        //Device Discovery
        List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);

        foreach (SonyAPI_Lib.SonyDevice discoverDev in fDev)
        {
            if (discoverDev.Name.Contains("KDL"))
            {
                myTV = discoverDev;
                break;
            }
        }

        //Initializing Device
        SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
        myDevice.initialize(myTV);

        return myDevice;
    }

    public bool DeviceResgitration(SonyAPI_Lib.SonyDevice myDevice)
    {
        bool RegComplete = myDevice.register();
        return RegComplete;
    }
}

RunCommand

C#
class RunCommand
{
    public string ExecuteCommand(SonyAPI_Lib.SonyDevice device, string command)
    {
        string CmdList = device.get_remote_command_list();
        string irccCmd = device.getIRCCcommandString(command);

        return (device.send_ircc(irccCmd));
    }
}

History

  • The current version is 1.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance Cognizant Technology Solutions India Pvt. Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionUsing namespace for better code Pin
KarstenK6-Mar-21 7:40
mveKarstenK6-Mar-21 7:40 
QuestionSamsung Wi-Fi API ? Pin
Member 1151324017-Aug-15 7:34
Member 1151324017-Aug-15 7:34 
AnswerRe: Samsung Wi-Fi API ? Pin
Avirup Das17-Aug-15 20:34
professionalAvirup Das17-Aug-15 20:34 
GeneralRe: Samsung Wi-Fi API ? Pin
Member 1151324018-Aug-15 10:22
Member 1151324018-Aug-15 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.