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:
- Discovery
- Initialization
- Registration
- Remote Access
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.
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.
foreach (SonyAPI_Lib.SonyDevice discoverDev in fDev)
{
if (discoverDev.Name.Contains("KDL"))
{
myTV = discoverDev;
break;
}
}
Once the device is identified, the Device needs to be initialized.
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.initialize(myTV);
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.
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.
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
class RegisterDevice
{
public SonyAPI_Lib.SonyDevice DeviceInitialization()
{
SonyAPI_Lib mySonyLib = new SonyAPI_Lib();
SonyAPI_Lib.SonyDevice myTV = null;
List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
foreach (SonyAPI_Lib.SonyDevice discoverDev in fDev)
{
if (discoverDev.Name.Contains("KDL"))
{
myTV = discoverDev;
break;
}
}
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
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.