Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / XML

SonyAPILib

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
6 Apr 2015CPOL17 min read 40.8K   130   23   11
A C# API Library for Discovering, Registering and Controlling Sony devices equipped with a LAN or WiFi port.

Introduction

This C# API provides 4 stages of Methods and Properties used to Discover, Register and Control Sony Smart devices equipped with a LAN or WiFi port and has the IRCC UPnP service installed.

SonyAPILib Stages:
1 Discovery
2 Registration
3 Initialization
4 Remote Control

Sony Smart devices are loaded with 1 of 3 different generations of a Linux based web server API. Depending on the year of the device will determine the generation of the API loaded in the device. Generations 1 and 2 mainly use UPnP SOAP services to communicate or send commands. Generation 3 devices are now mainly using JSON to communicate or send commands.

To access ALL features of any generation device, a Registration process MUST be completed! (It has been determined by several users that registration is not required to send commands, however other features will not be available.) Depending on the Generation of the device will determine the correct Registration process to follow. For example, Generation 1 devices will automatically display a confirmation screen on the device(TV) to complete the registration. Generation 2 devices have to be put in to "Registration Mode" before a registration attempt is made. Generation 3 devices will display a PIN Code on the screen that has to be confirmed with the device before registration is complete.

All generations of devices uses an IRCC Name:Value pair for sending commands to the device. Retrieval of this list from the device is determined by the generation of the device. Generations 1 and 2 will have an actionList_Url that contains the URL method used to retrieve the remoteCommandList from the device. You MUST have completed the registration process before the device will return this information. Generations 1 & 2 devices will return an XML file (String) containing the Name:Value pairs. For example:<command name="VolumeUp" type="ircc" value="AAAAAQAAAAEAAAASAw==" />.Generation 3 devices uses a JSON command to retrieve the Command list and returns a JSON value like this: {"name":"VolumeUp","value":"AAAAAQAAAAEAAAASAw=="}

See Example Command List more information

Background

This API Library is a compilation from many different sources and users found on the internet and through months of my own personal testing and coding. Many thanks go out to Lance Clark and Pete Brutsch for help testing this API on Generation 2 and 3 devices. Also a huge jump in progress was possible with the help of Mendel, who has published his findings here: http://mendelonline.be/sony/

3rd Party Files

This API Library uses 2 additional 3rd party libraries.

The ManagedUPnP library provides the methods used to Discover and Initialize the devices.
This Libtary was written by: TheToid, and can be downloaded here: http://managedupnp.codeplex.com/

The NewtonSoft.Json library is used to prefrom all the JSON commands used in Generation 3 devices.
This library was written by: James Newton-King, and can be downloaded here: https://www.nuget.org/packages/Newtonsoft.Json/

SonyAPILib Stages

This API consist of 4 different stages.
Each stage has it's own importance and requirements.
Some stages require that other stages be completed before the next one will work properly!

Please See API Library Class for more information before continuing.

Discovery - Stage 1

This is the initial stage of the API.

This stage is used to send a UPnP broadcast across the LAN for devices loaded with the Sony IRCC service and then listens for any return responses. If a device does respond, a new SonyDevice is created containing information obtained from that device. Each SonyDevice found is then added to a list that is returned.

For Example:

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);

The list fDev above will contain all the Sony devices found on the LAN.

To determine how many devices were discovered by the API, use the Count() method on the list. For example:

int x = fDev.Count()

To extract a device from the list just specify the array element.
REMEMBER Arrays start at 0(Zero) not 1! So if the list only has 1 device, then the array is only 0. If the list contains 2 or more devices, then the array is 0, 1, 2, 3, ....

To retrieve the first device in the list use this example:

SonyAPI_Lib.SonyDevice myDevice = fDev[0]

or change the array to the item you wish.

Now you can Save the device's information in a database or text file or whatever medium your application uses. Saving the Device's Friendly Name and IP Address is all that is required to Initialize the device WITHOUT having to use the SonyDiscovery() method every time you execute the application.

For Example:

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
SonyAPI_Lib.SonyDevice myDevice = fDev[0];
string sql = "INSERT INTO Devices set Name = myDevice.Name, IP = myDevice.Device_IP_Address";

For more information see: API Class, Device Intitialize

Initialization - Stage 2

Initializes a new Sony Device Class Object.

This Stage is used to retrieve any UNKNOWN information required to connect and communicate with the selected device. The Initialization process can be achieved by providing ONLY 2 Device Properties (Name and IP Address). This information can be manually entered or can be retrieved from a database or text file as explained above in Stage 1. This provides the application a way to reuse devices WITHOUT having to run the SonyDiscover() method. However, even if the SonyDiscover() method is used, the device object MUST still be initialized.

To Initialize the first device found using SonyDiscover() use this example:

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.initialize(fDev[0]);

To Initialize a device previously found using SonyDiscover(), but saved to a database or text file use this example:

saved_Name = getinfofromfile('device_name');
saved_IP_Address = getinfofromfile('device_ip');
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.Name = saved_Name;
myDevice.Device_IP_Address = saved_IP_Address;
myDevice.initialize();

NOTE: When using the above method, the Name property must match exactly with what the Device returns!

For more information see: SonyDevice Initialize

Registration - Stage 3

Registers the controlling device (Computer, Phone, Tablet) with the Sony device to be controled.

All Generations of Sony devices use the IRCC UPnP service (By: Sony) to send commands to the device. UPnP Services do NOT require the controlling device to be registered. However, if a registration process is not completed, other certain functions and features may not be available. This API is written with the conception that a Registration Process WILL be used, otherwise the API may have undesirable results, errors or possibly cause an application crash. (Please report any bugs or issues here Issues).

Depending on the Generation of the device will determine the correct Registration process to follow. For example, Generation 1 devices will automatically display a confirmation screen on the device(TV) that the user must confirm to complete the registration. Generation 2 devices have to be put in to "Registration Mode" before a registration attempt is made. Generation 3 devices will display a PIN Code on the screen that has to be confirmed with the device before registration is able to be completed. So regardless of the device's generation, user interaction at the Sony device is required to complete the registration process.

The registration Process uses a Server Name to register with the device. For example, TVSideview uses your Computer's name along with their name like this:Your_Computer_Name(TVSideView). This API will automatically use the name:Your_Computer_Name(SonyAPILib). To use a different name or the name of your application, set the devices Server_Name before Initializing the device and registering it. Also, once registered with this name, you will need to set it every time you initialize the device with the same name!

For Example:

saved_Name = getinfofromfile('device_name');
saved_IP_Address = getinfofromfile('device_ip');
MyServer_Name = "The_Name_of_my_application"
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.Name = saved_Name;
myDevice.Device_IP_Address = saved_IP_Address;
myDevice.Server_Name = MyServer_Name;
myDevice.initialize();

NOTE: Before running the registration process, make sure the device has been Initialized!

NOTE: If registration returns true, this method does NOT have to be ran again on the same controling device. However, If you wish to run the application on another computer or controling device, it MUST complete the registration also!

For Generations 1 and 2 devices, use this example: (Older Devices)

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.initialize(fDev[0]);
bool registration = myDevice.register();
if(registration==true) { // Do whatever; }

For Generation 1, 2 & 3 devices, this requires an additional step. Use this example: (Any Device)

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
SonyAPI_Lib.SonyDevice myDevice = new SonyAPI_Lib.SonyDevice();
myDevice.initialize(fDev[0]);
bool registration = myDevice.register();
if(myDevice.Generation == 3)
{
string pin = Console.ReadLine(); // Allow user to enter PIN Code. Could be a Form in a Forms App
registration = myDevice.sendAuth(pin);
}
if(registration==true) { // Do whatever; }

For more information see: Sony Device Registration

Remote Control - Stage 4

Sends Remote Control commands to the device.

All Generations of Sony devices use the IRCC UPnP service (By: Sony) to send commands to devices. During the Initialization process, the SonyDevice's Command list was populated. These are accessible by using the following method: myDevice.Commands. Each command consist of a Name:Value pair. However, the device only needs the Value to execute. For Example, the Volume Up command value is AAAAAQAAAAEAAAASAw==. This is what needs to be sent to the device.

To retrieve the Command value string, use this example:

string irccCmd = myDevice.getIRCCcommandString("VolumeUp");

Then to send this value to the device, use this example:

string results = myDevice.send_ircc(irccCmd);

A shorter way to accomplish this is to use this example:

string results = myDevice.send_ircc(myDevice.getIRCCcommandString("VolumeUp"));

NOTE: The string results will contain any information returned by the device.

For more information see: Sony Device getIRCCcommandString() or send_ircc().

SonyAPI_Lib Class

requires .NET Framework 4.0

This is the Main class of the API.

References Required

SonyAPILib.dll

Syntax

public class SonyAPI_Lib()

Partial Classes

API - API Class used for Device Discovery.
LOG - Log Class used for API Logging information.
SonyDevice - Sony Device Class contains all properties and methods associated with the device.
mySony - Class that contains ALL UPnP services discovered on the LAN. (More to come!)

Constructors

Required: Create a new instance of the API Library like this:

SonyAPI_Lib mySonyLib = new SonyAPI_Lib();

This new instance will be used to set all logging information, UPnP services and Device Discovery methods.
For this API to function properly, this instance must be created!

.API Class

This class contains the methods and Properties used for Discovery of Sony IRCC Devices.

Syntax

public class SonyAPI_Lib.API()

Methods

sonyDiscovery - This method is only used to send a UPnP broadcast across the LAN to locate any devices that are loaded with the Sony UPnP IRCC service.

Syntax: SonyAPI_Lib.API.sonyDiscovery(service);

Param: service - The UPnP Service to be search for during the Discovery process.
Use "null" for the default IRCC:1 service.
NOTE: If another service is used other than "null", this API may not function properly! (To be used in future Upgrades)

Returns: A List of SonyDevice objects that were discovered during the process. SonyDevice objects returned with sonyDiscovery() will only contain the device's Name and IP Address. These are the only 2 properties require to complete a device Initialization. See SonyDevice Initialize for more information.

Example:

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null)

The list fDev will return a list of all SonyDevice's found on the LAN.

For more information see: SonyDevice Class

.LOG Class

The SonyAPI_Lib has it's own basic logging system.
This can be implemented in to your own program by setting the LOG object properties.

This class contains the methods and Properties used in the API Logging system.

Syntax

public class SonyAPI_Lib.LOG()

Methods

writetolog - Write a message line to the API Log file.

Syntax: mySonyLib.LOG.writetolog(message, oride);
Param message is a string containing the TEXT to write to the file.
Param oride is a Bool value that sets if the line should be logged regardless of theenableLogginglev value.
For example: mySonyLib.LOG.writetolog("This is my Text", true); will write this line to the log file NO matter what the logging level is set to.


clearLog - Clears the contents of the Log File.

Syntax: mySonyLib.LOG.clearLog(newName);
Param newName if not set to "null", will rename the existing logfile to newName before clearing. NOTE: Must be a text file (.txt)

This example will just clear the log:

mySonyLib.LOG.clearLog(null);

This example will copy the log file to a new file with the name provided, then clear the log file:

mySonyLib.LOG.clearLog("Old_LOG_File.txt");

Properties

enableLogging - Set to True or False to turn Logging On or Off.
Syntax: mySonyLib.LOG.enableLogging = true. (Default is set to FALSE).


enableLogginglev - Set the level of logging. "Basic" only logs minimal information where "All" logs all information.
Syntax:
mySonyLib.LOG.enableLogginglev = "All". (Default is set to "Basic").


loggingPath - Sets the location to save the Logging file.
Syntax:
mySonyLib.LOG.loggingPath = "c:\tempfolder" (Default is set to "C:\ProgramData\Sony").


loggingName - Sets the Log File Name to Use.
Syntax:
mySonyLib.LOG.loggingName = "MySonyLoggingFile.txt" (Default is set to "SonyAPILib_LOG.txt").

.SonyDevice Class

SonyDevice Class

This class contains all the methods and Properties used in Initializing, Registering, and Controlling Sony Devices.

Syntax

public class SonyAPI_Lib.SonyDevice()

Constructor

SonyAPI_Lib.SonyDevice SonyDevice = new SonyAPI_Lib.SonyDevice()

Methods

.initialize - This method is used to initialize the SonyDevice with all information required to Access and Control the Sony device.

Syntax 1: SonyDevice.initialize();
Syntax 2: SonyDevice.initialize(SonyDevice);

NOTE: This method requires the SonyDevice's "Name" and "Device_IP_Address" properties be set PRIOR to execution. This can be entered manually through user input, retrieved from a saved database or file, or retrieved using Discovery (See API Class).

The Initialize method will automatically perform the following task:
1. Determine what Generation the Device is.
2. Locate the Port number used in communications
3. Retrieve the Devices Mac Address if needed(Gen3)
4. Retrieve the Computers Mac Address(Used in Gen 1 & 2 registration and authentication)
5. Retrieve the IRCC Remote Command List from the device
6. Check to see if registration has been completed successfully Prior
7. If Gen 3 and Registration is TRUE, loads the saved cookie for authentication.
8. Checks the current Status of the device.

IMPORTANT You MUST initialize the device before performing any other task, such as registration, check status or sending commands.

The Initialize method has 2 overrides that provides 2 different ways to initialize the device.

This example initializes a new SonyDevice object based on another SonyDevice object or the one returned from the Discovery() method.

List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);  
SonyAPI_Lib.SonyDevice mySonyDev = new SonyAPI_Lib.SonyDevice();  
fDev[0].Server_Name = "TheNameofMyApplication";  
mySonyDev.initialize(fDev[0]);  

This example manually initializes a new SonyDevice object without using the Discovery() method.

SonyAPI_Lib.SonyDevice mySonyDev = new SonyAPI_Lib.SonyDevice();  
mySonyDev.Name = "Bravia KDL-48W600B";  
mySonyDev.Device_IP_Address = "192.168.0.222";  
mySonyDev.Server_Name = "TheNameofMyApplication";  
mySonyDev.initialize();  

.register - This method is used to perform the Registration process with the Sony Device.

Syntax: SonyDevice.register();

NOTE: You MUST execute an initialize method before attempting to execute the register method.

Returns: Will return a Bool value of True or False depending if the registration was successful of not.

IMPORTANT: Generation 3 devices will return a value of FALSE because there is an additional step required to complete the registration process. The Device will display a PIN code on the screen that needs to be Authenticated with the device. To do this, allow user to enter the PIN code displayed, then immediately executeSonyDevice.sendAuth(pincode). (See .sendAuth method)

Example:

SonyAPI_Lib.SonyDevice mySonyDev = new SonyAPI_Lib.SonyDevice();  
mySonyDev.Name = "Bravia KDL-48W600B";  
mySonyDev.Device_IP_Address = "192.168.0.222";  
mySonyDev.Server_Name = "TheNameofMyApplication";  
mySonyDev.initialize();  
if(mySonyDev.Registered == false)  
{  
    Bool isreg = mySonyDev.register();  
    if(mySonyDev.Generation == 3 && isreg == false)  
    {  
        Console.WriteLine("Enter PIN Code displayed on device screen.");  
        string ckii = Console.ReadLine();  
        isreg = mySonyDev.sendAuth(ckii);  
    }  
}  
if(isreg == true) { // do whatever; }

Note This method will also set the SonyDevice.Registered property to the corresponding value. (See Properties)


.sendAuth - This method is used to send the Authentication PIN code to the device for confirmation. This is ONLY used with generation 3 devices.

Syntax: SonyDevice.sendAuth(pincode);
Param: pincode is a string containg the PIN code entered by the user.

NOTE: This method must be used in conjunction with the SonyDevice.register method, and must be called with in 30 seconds after the device displays the PIN information on the screen, otherwise the device will "Time Out".

Example:

SonyAPI_Lib.SonyDevice mySonyDev = new SonyAPI_Lib.SonyDevice();  
mySonyDev.Name = "Bravia KDL-48W600B";  
mySonyDev.Device_IP_Address = "192.168.0.222";  
mySonyDev.Server_Name = "TheNameofMyApplication";  
mySonyDev.initialize(fDev[0]);  
if(mySonyDev.Registered == false)  
{  
    Bool isreg = mySonyDev.register();  
    if(mySonyDev.Generation == 3 && isreg == false)  
    {  
        string ckii = Console.ReadLine();  
        Console.WriteLine("Enter PIN Code displayed on screen.");  
        isreg = mySonyDev.sendAuth(ckii);  
    }  
}  
if(isreg == true) { // do whatever; }

Note This method will also set the SonyDevice.Registered property to the corresponding value. (See Properties)


.get_remote_command_list - This method is used to retrieve the IRCC Remote Command List from the device.
This is also preformed during SonyDevice.Initialize and will populate the SonyDevice.Commands list.

Syntax: SonyDevice.get_remote_command_list();

NOTE: Returns: This method will return the XML file containing the retrieved command list as a string for your own use.

Example:

string cmdList = mySonyDev.get_remote_command_list();  

For more information see:
.initialize above
Properties below
Example Remote Command List


.checkStatus - This method is used to Check the current Status of the Device.

Syntax: SonyDevice.checkStatus();

Note:
Returns: This method will return a string containing the devices response.

Important: Generation 1 & 2 devices will not respond if the device is OFF. Generation 2 & 3 devices have the ability to set the network port to stay active even when powered off.

Example:

string devStatus = mySonyDev.checkStatus()  
if(devStatus == "") { console.writeline("Device May be OFF!"); }

Example String Returned: viewing:HDMI1 or status:active


.getIRCCCommandString - This method is used to retrieve the actual IRCC Command value from the SonyDevice.Commands list.

Syntax: SonyDevice.getIRCCCommandString(CommandName);
Param: CommandName - A string containing the Command Name to convert to IRCC value.
returns: - This method will return the IRCC value for the Command Name provided. If Command Name is not found, returns an empty string ("").

Note: May be used in conjunction with .sendIRCC (see below)

Example:

string myCommand = mySonyDev.getIRCCCommandString("VolumeUp");  
if(myCommand == "") 
{  
    console.writeline("Command not found");  
}
else  
{  
    mySonyDev.sendIRCC(myCommand);  
}

Example String Returned: AAAAAQAAAAEAAAASAw==


.sendIRCC - This method is used to send an IRCC command value to the device

Syntax: SonyDevice.sendIRCC(CommandValue)
Param: CommandValue This is a string containing the Remote Command Value retrieved by using the getIRCCCommandString method, or retrieved from the SonyDevice.Commands list.
Returns: This method will return a string containing the device's response.

Note: May be used in conjunction with .getIRCCCommandString (see abaove)

Example:

string response = mySonyDev.sendIRCC(mySonyDev.getIRCCCommandString("VolumeUp"));  
if(response == "") { console.writeline("Command not found"); }

.send_text - This method is used to send text to the device's search screen.

Syntax: SonyDevice.send_text(usertext)
Param: usertext This is a string containing the text to send to the device.
Returns: This method will return a string containing the device's response.

NOTE: Search screen must be visible before executing this method!

Example:

string response = mySonyDev.send_text("Action Movies");  

.channel_set - This method is used to send a complete channel to the Device, instead of one number at a time.

Syntax: SonyDevice.channel_set(channel)
Param: channel This is a string containing a valid channel to send to the device.

NOTE: This method should ONLY be used on devices that have a ChannelUp or ChannelDown command! Home Theater Amplifiers and DVD/Blueray players will crash if this method is sent to them! This method will also update the SonyDevice.current_channel property.

Example:

mySonyDev.channel_set("47.1");

Properties

.Name - (string)Gets or Sets the Friendly Name retrieved from the Device.

Must be set manually or from Discovery, before Initializing.
Must match the Friendly Name return by the Device.
Example:

mySonyDevice.Name="BRAVIA KDL-48W600B";

or

String DeviceName = mySonyDevice.Name;

.Generation - (Integer)Gets or Sets the Generation of the Device.

Represents the Generation of the devices Built-In API (1, 2 or 3)
Will automatically be set during Initializing.
Manually entering an incorrect value may cause undesirable results.
Example:

mySonyDevice.Generation = 1;

or

int x = mySonyDevice.Generation;

.Device_IP_Address - (string)Gets or Sets the IP Address of the Device.

Must be set manually or from Discovery, before Initializing.
Example:

mySonyDeviec.Device_IP_Address = "192.168.1.160";

or

string deviceIP = mySonyDevice.Device_IP_Address;

.Device_Macaddress - (string)Gets or Sets the MAC Address of the Device.

This is only used with Generation 3 devices.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

mySonyDeivce.Device_Macaddress = "62:32:A5:F2:10:EA";

or

string deviceMac = mySonyDevice.Device_Macaddress;

.Device_Port - (Integer)Gets or Sets the Port number of the Device.

Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

mySonyDeivce.Device_Port = 8080;

or

int devicePort = mySonyDevice.Device_Port;

.Commands - (List< Command Objects >)Gets or Sets the Commands List for the Device.

This is a List of Sony Command objects retrieved from the device during initializing.
Each item will contain a Command Name and a Command Value.
The Command value is the IRCC string that is sent to the device. This list is provided your your use, and is used by the getIRCCCommandString method.
To perform a search of the commands in your own application use this Example:

string cmdname = "VolumeUp";
var results = mySonyDevice.Commands.FirstOrDefault(o => o.name == cmdname);
string cmdValue = results.value;
string results = mySonyDevice.send_IRCC(cmdValue);

.Server_Macaddress - (String)Gets or Sets the Mac Address of the controlling device.

This represents the computer, table or other device's Mac Address being used to control the Sony device.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

mySonyDeivce.Server_Macaddress = "62:32:A5:F2:10:EA";

or

string ServerMac = mySonyDevice.Server_Macaddress;

.Server_Name - (String)Gets or Sets the Server Name (Application Name) to be used in Registration and Authorization.

This needs to be set before initializing, otherwise Default will be Used
The default Server Name is the name of the computer plus the API: "MyComputer(SonyAPILib)"
This represents the computer or application Name used to control the Sony device.
Default Will automatically be set during Initializing.
Example:

mySonyDeivce.Server_Name = "MyApplicationName";

or

string ServerName = mySonyDevice.Server_Name;

.Cookie - (String)Gets or Sets the Cookie string used in Authentication for this Device.

Contains the contents of the Cookie.Json file saved during the Registration process.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

mySonyDeivce.Cookie = "[{"Comment":"","CommentUri":null,"HttpOnly":false,"Discard":false,"Domain":"192.168.1.160","Expired":false,"Expires":"\/Date(1422719955623-0700)\/","Name":"auth","Path":"/sony/","Port":"","Secure":false,"TimeStamp":"\/Date(1421510355623-0700)\/","Value":"7197b5e69d4e4fab8a24a5716d7aaadd3acfafc0ba05771c454d2c170fe00a37","Version":0}]";
or
string myCookie = mySonyDevice.Cookie;

.Registered - (Boolean)Gets or Sets if the Registration has been completed.

Represents the status of the Registration process between the controlling device and the Sony device.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

bool reg = mySonyDevice.Registered;

or

mySonyDevice.Registered = true;

.actionList_URL - Gets or Sets the URL to the Action_List for the Device.

Will contain the Action List URL used in Gen 1 & 2 devices.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

string actionList = mySonyDevice.actionlist_URL;

or

mySonyDevice.actionList_URL = "http://192.168.1.160:50001/cers/ActionList.xml";

.control_URL - Gets or Sets the URL to use for IRCC Commands to be sent to.

Will contain the IRCC Control URL.
Will automatically be set during Initializing.
Manual setting is NOT suggested!
Example:

string controlURL = mySonyDevice.control_URL;

or

mySonyDevice.control_URL = "http://192.168.1.160/upnp/control/IRCC";

.current_channel - Gets or Sets the Current Channel of the Device.

Contains the current channel when the channel_set method is used


.current_volume - Gets or Sets the Current Volume level of the Device.

NOT USED YET

Example Remote Command List

This is an example from a Generation 1 Bravia TV.

XML
<?xml version="1.0" ?>
<remoteCommandList>
<command name="Confirm" type="ircc" value="AAAAAQAAAAEAAABlAw==" />
<command name="Up" type="ircc" value="AAAAAQAAAAEAAAB0Aw==" />
<command name="Down" type="ircc" value="AAAAAQAAAAEAAAB1Aw==" />
<command name="Right" type="ircc" value="AAAAAQAAAAEAAAAzAw==" />
<command name="Left" type="ircc" value="AAAAAQAAAAEAAAA0Aw==" />
<command name="Home" type="ircc" value="AAAAAQAAAAEAAABgAw==" />
<command name="Options" type="ircc" value="AAAAAgAAAJcAAAA2Aw==" />
<command name="Return" type="ircc" value="AAAAAgAAAJcAAAAjAw==" />
<command name="Num1" type="ircc" value="AAAAAQAAAAEAAAAAAw==" />
<command name="Num2" type="ircc" value="AAAAAQAAAAEAAAABAw==" />
<command name="Num3" type="ircc" value="AAAAAQAAAAEAAAACAw==" />
<command name="Num4" type="ircc" value="AAAAAQAAAAEAAAADAw==" />
<command name="Num5" type="ircc" value="AAAAAQAAAAEAAAAEAw==" />
<command name="Num6" type="ircc" value="AAAAAQAAAAEAAAAFAw==" />
<command name="Num7" type="ircc" value="AAAAAQAAAAEAAAAGAw==" />
<command name="Num8" type="ircc" value="AAAAAQAAAAEAAAAHAw==" />
<command name="Num9" type="ircc" value="AAAAAQAAAAEAAAAIAw==" />
<command name="Num0" type="ircc" value="AAAAAQAAAAEAAAAJAw==" />
<command name="Num11" type="ircc" value="AAAAAQAAAAEAAAAKAw==" />
<command name="Num12" type="ircc" value="AAAAAQAAAAEAAAALAw==" />
<command name="Power" type="ircc" value="AAAAAQAAAAEAAAAVAw==" />
<command name="Display" type="ircc" value="AAAAAQAAAAEAAAA6Aw==" />
<command name="VolumeUp" type="ircc" value="AAAAAQAAAAEAAAASAw==" />
<command name="VolumeDown" type="ircc" value="AAAAAQAAAAEAAAATAw==" />
<command name="Mute" type="ircc" value="AAAAAQAAAAEAAAAUAw==" />
<command name="Audio" type="ircc" value="AAAAAQAAAAEAAAAXAw==" />
<command name="SubTitle" type="ircc" value="AAAAAgAAAJcAAAAoAw==" />
<command name="Yellow" type="ircc" value="AAAAAgAAAJcAAAAnAw==" />
<command name="Blue" type="ircc" value="AAAAAgAAAJcAAAAkAw==" />
<command name="Red" type="ircc" value="AAAAAgAAAJcAAAAlAw==" />
<command name="Green" type="ircc" value="AAAAAgAAAJcAAAAmAw==" />
<command name="Play" type="ircc" value="AAAAAgAAAJcAAAAaAw==" />
<command name="Stop" type="ircc" value="AAAAAgAAAJcAAAAYAw==" />
<command name="Pause" type="ircc" value="AAAAAgAAAJcAAAAZAw==" />
<command name="Rewind" type="ircc" value="AAAAAgAAAJcAAAAbAw==" />
<command name="Forward" type="ircc" value="AAAAAgAAAJcAAAAcAw==" />
<command name="Prev" type="ircc" value="AAAAAgAAAJcAAAA8Aw==" />
<command name="Next" type="ircc" value="AAAAAgAAAJcAAAA9Aw==" />
<command name="Replay" type="ircc" value="AAAAAgAAAJcAAAB5Aw==" />
<command name="Advance" type="ircc" value="AAAAAgAAAJcAAAB4Aw==" />
<command name="TopMenu" type="ircc" value="AAAAAgAAABoAAABgAw==" />
<command name="PopUpMenu" type="ircc" value="AAAAAgAAABoAAABhAw==" />
<command name="Eject" type="ircc" value="AAAAAgAAAJcAAABIAw==" />
<command name="Rec" type="ircc" value="AAAAAgAAAJcAAAAgAw==" />
<command name="SyncMenu" type="ircc" value="AAAAAgAAABoAAABYAw==" />
<command name="ClosedCaption" type="ircc" value="AAAAAgAAAKQAAAAQAw==" />
<command name="Teletext" type="ircc" value="AAAAAQAAAAEAAAA/Aw==" />
<command name="ChannelUp" type="ircc" value="AAAAAQAAAAEAAAAQAw==" />
<command name="ChannelDown" type="ircc" value="AAAAAQAAAAEAAAARAw==" />
<command name="Input" type="ircc" value="AAAAAQAAAAEAAAAlAw==" />
<command name="GGuide" type="ircc" value="AAAAAQAAAAEAAAAOAw==" />
<command name="EPG" type="ircc" value="AAAAAgAAAKQAAABbAw==" />
<command name="DOT" type="ircc" value="AAAAAgAAAJcAAAAdAw==" />
<command name="Analog" type="ircc" value="AAAAAgAAAHcAAAANAw==" />
<command name="Exit" type="ircc" value="AAAAAQAAAAEAAABjAw==" />
<command name="Digital" type="ircc" value="AAAAAgAAAJcAAAAyAw==" />
<command name="BS" type="ircc" value="AAAAAgAAAJcAAAAsAw==" />
<command name="CS" type="ircc" value="AAAAAgAAAJcAAAArAw==" />
<command name="BSCS" type="ircc" value="AAAAAgAAAJcAAAAQAw==" />
<command name="Ddata" type="ircc" value="AAAAAgAAAJcAAAAVAw==" />
<command name="InternetWidgets" type="ircc" value="AAAAAgAAABoAAAB6Aw==" />
<command name="InternetVideo" type="ircc" value="AAAAAgAAABoAAAB5Aw==" />
<command name="SceneSelect" type="ircc" value="AAAAAgAAABoAAAB4Aw==" />
<command name="Mode3D" type="ircc" value="AAAAAgAAAHcAAABNAw==" />
<command name="iManual" type="ircc" value="AAAAAgAAABoAAAB7Aw==" />
<command name="Wide" type="ircc" value="AAAAAgAAAKQAAAA9Aw==" />
<command name="Jump" type="ircc" value="AAAAAQAAAAEAAAA7Aw==" />
<command name="PAP" type="ircc" value="AAAAAgAAAKQAAAB3Aw==" />
<command name="MyEPG" type="ircc" value="AAAAAgAAAHcAAABrAw==" />
<command name="ProgramDescription" type="ircc" value="AAAAAgAAAJcAAAAWAw==" />
<command name="WriteChapter" type="ircc" value="AAAAAgAAAHcAAABsAw==" />
<command name="TrackID" type="ircc" value="AAAAAgAAABoAAAB+Aw==" />
<command name="TenKey" type="ircc" value="AAAAAgAAAJcAAAAMAw==" />
<command name="AppliCast" type="ircc" value="AAAAAgAAABoAAABvAw==" />
<command name="acTVila" type="ircc" value="AAAAAgAAABoAAAByAw==" />
<command name="DeleteVideo" type="ircc" value="AAAAAgAAAHcAAAAfAw==" />
<command name="EasyStartUp" type="ircc" value="AAAAAgAAAHcAAABqAw==" />
<command name="OneTouchTimeRec" type="ircc" value="AAAAAgAAABoAAABkAw==" />
<command name="OneTouchView" type="ircc" value="AAAAAgAAABoAAABlAw==" />
<command name="OneTouchRec" type="ircc" value="AAAAAgAAABoAAABiAw==" />
<command name="OneTouchRecStop" type="ircc" value="AAAAAgAAABoAAABjAw==" />
</remoteCommandList>

This is Example JSON Command List from a Bravia TV.

{"id":20,"result":
  [{"bundled":true,"type":"RM-J1100"},
    [{"name":"PowerOff","value":"AAAAAQAAAAEAAAAvAw=="},
     {"name":"Input","value":"AAAAAQAAAAEAAAAlAw=="},
     {"name":"GGuide","value":"AAAAAQAAAAEAAAAOAw=="},
     {"name":"EPG","value":"AAAAAgAAAKQAAABbAw=="}, 
     {"name":"Favorites","value":"AAAAAgAAAHcAAAB2Aw=="},
     {"name":"Display","value":"AAAAAQAAAAEAAAA6Aw=="},
     {"name":"Home","value":"AAAAAQAAAAEAAABgAw=="}, 
     {"name":"Options","value":"AAAAAgAAAJcAAAA2Aw=="},
     {"name":"Return","value":"AAAAAgAAAJcAAAAjAw=="},
     {"name":"Up","value":"AAAAAQAAAAEAAAB0Aw=="},
     {"name":"Down","value":"AAAAAQAAAAEAAAB1Aw=="},
     {"name":"Right","value":"AAAAAQAAAAEAAAAzAw=="},
     {"name":"Left","value":"AAAAAQAAAAEAAAA0Aw=="},
     {"name":"Confirm","value":"AAAAAQAAAAEAAABlAw=="},
     {"name":"Red","value":"AAAAAgAAAJcAAAAlAw=="},
     {"name":"Green","value":"AAAAAgAAAJcAAAAmAw=="},
     {"name":"Yellow","value":"AAAAAgAAAJcAAAAnAw=="},
     {"name":"Blue","value":"AAAAAgAAAJcAAAAkAw=="},
     {"name":"Num1","value":"AAAAAQAAAAEAAAAAAw=="},
     {"name":"Num2","value":"AAAAAQAAAAEAAAABAw=="},
     {"name":"Num3","value":"AAAAAQAAAAEAAAACAw=="},
     {"name":"Num4","value":"AAAAAQAAAAEAAAADAw=="},
     {"name":"Num5","value":"AAAAAQAAAAEAAAAEAw=="},
     {"name":"Num6","value":"AAAAAQAAAAEAAAAFAw=="},
     {"name":"Num7","value":"AAAAAQAAAAEAAAAGAw=="},
     {"name":"Num8","value":"AAAAAQAAAAEAAAAHAw=="},
     {"name":"Num9","value":"AAAAAQAAAAEAAAAIAw=="},
     {"name":"Num0","value":"AAAAAQAAAAEAAAAJAw=="},
     {"name":"Num11","value":"AAAAAQAAAAEAAAAKAw=="},
     {"name":"Num12","value":"AAAAAQAAAAEAAAALAw=="},
     {"name":"VolumeUp","value":"AAAAAQAAAAEAAAASAw=="},
     {"name":"VolumeDown","value":"AAAAAQAAAAEAAAATAw=="},
     {"name":"Mute","value":"AAAAAQAAAAEAAAAUAw=="},
     {"name":"ChannelUp","value":"AAAAAQAAAAEAAAAQAw=="},
     {"name":"ChannelDown","value":"AAAAAQAAAAEAAAARAw=="},
     {"name":"SubTitle","value":"AAAAAgAAAJcAAAAoAw=="},
     {"name":"ClosedCaption","value":"AAAAAgAAAKQAAAAQAw=="},
     {"name":"Enter","value":"AAAAAQAAAAEAAAALAw=="},
     {"name":"DOT","value":"AAAAAgAAAJcAAAAdAw=="},
     {"name":"Analog","value":"AAAAAgAAAHcAAAANAw=="},
     {"name":"Teletext","value":"AAAAAQAAAAEAAAA/Aw=="},
     {"name":"Exit","value":"AAAAAQAAAAEAAABjAw=="},
     {"name":"Analog2","value":"AAAAAQAAAAEAAAA4Aw=="},
     {"name":"*AD","value":"AAAAAgAAABoAAAA7Aw=="},
     {"name":"Digital","value":"AAAAAgAAAJcAAAAyAw=="},
     {"name":"Analog?","value":"AAAAAgAAAJcAAAAuAw=="},
     {"name":"BS","value":"AAAAAgAAAJcAAAAsAw=="},
     {"name":"CS","value":"AAAAAgAAAJcAAAArAw=="},
     {"name":"BSCS","value":"AAAAAgAAAJcAAAAQAw=="},
     {"name":"Ddata","value":"AAAAAgAAAJcAAAAVAw=="},
     {"name":"PicOff","value":"AAAAAQAAAAEAAAA+Aw=="},
     {"name":"Tv_Radio","value":"AAAAAgAAABoAAABXAw=="},
     {"name":"Theater","value":"AAAAAgAAAHcAAABgAw=="},
     {"name":"SEN","value":"AAAAAgAAABoAAAB9Aw=="},
     {"name":"InternetWidgets","value":"AAAAAgAAABoAAAB6Aw=="},
     {"name":"InternetVideo","value":"AAAAAgAAABoAAAB5Aw=="},
     {"name":"Netflix","value":"AAAAAgAAABoAAAB8Aw=="},
     {"name":"SceneSelect","value":"AAAAAgAAABoAAAB4Aw=="},
     {"name":"Mode3D","value":"AAAAAgAAAHcAAABNAw=="},
     {"name":"iManual","value":"AAAAAgAAABoAAAB7Aw=="},
     {"name":"Audio","value":"AAAAAQAAAAEAAAAXAw=="},
     {"name":"Wide","value":"AAAAAgAAAKQAAAA9Aw=="},
     {"name":"Jump","value":"AAAAAQAAAAEAAAA7Aw=="},
     {"name":"PAP","value":"AAAAAgAAAKQAAAB3Aw=="},
     {"name":"MyEPG","value":"AAAAAgAAAHcAAABrAw=="},
     {"name":"ProgramDescription","value":"AAAAAgAAAJcAAAAWAw=="},
     {"name":"WriteChapter","value":"AAAAAgAAAHcAAABsAw=="},
     {"name":"TrackID","value":"AAAAAgAAABoAAAB+Aw=="},
     {"name":"TenKey","value":"AAAAAgAAAJcAAAAMAw=="},
     {"name":"AppliCast","value":"AAAAAgAAABoAAABvAw=="},
     {"name":"acTVila","value":"AAAAAgAAABoAAAByAw=="},
     {"name":"DeleteVideo","value":"AAAAAgAAAHcAAAAfAw=="},
     {"name":"PhotoFrame","value":"AAAAAgAAABoAAABVAw=="},
     {"name":"TvPause","value":"AAAAAgAAABoAAABnAw=="},
     {"name":"KeyPad","value":"AAAAAgAAABoAAAB1Aw=="},  
     {"name":"Media","value":"AAAAAgAAAJcAAAA4Aw=="},
     {"name":"SyncMenu","value":"AAAAAgAAABoAAABYAw=="},
     {"name":"Forward","value":"AAAAAgAAAJcAAAAcAw=="},
     {"name":"Play","value":"AAAAAgAAAJcAAAAaAw=="},
     {"name":"Rewind","value":"AAAAAgAAAJcAAAAbAw=="},
     {"name":"Prev","value":"AAAAAgAAAJcAAAA8Aw=="},
     {"name":"Stop","value":"AAAAAgAAAJcAAAAYAw=="},
     {"name":"Next","value":"AAAAAgAAAJcAAAA9Aw=="},
     {"name":"Rec","value":"AAAAAgAAAJcAAAAgAw=="},
     {"name":"Pause","value":"AAAAAgAAAJcAAAAZAw=="},
     {"name":"Eject","value":"AAAAAgAAAJcAAABIAw=="},
     {"name":"FlashPlus","value":"AAAAAgAAAJcAAAB4Aw=="},
     {"name":"FlashMinus","value":"AAAAAgAAAJcAAAB5Aw=="},
     {"name":"TopMenu","value":"AAAAAgAAABoAAABgAw=="},
     {"name":"PopUpMenu","value":"AAAAAgAAABoAAABhAw=="}, 
     {"name":"RakurakuStart","value":"AAAAAgAAAHcAAABqAw=="},
     {"name":"OneTouchTimeRec","value":"AAAAAgAAABoAAABkAw=="},
     {"name":"OneTouchView","value":"AAAAAgAAABoAAABlAw=="},
     {"name":"OneTouchRec","value":"AAAAAgAAABoAAABiAw=="},
     {"name":"OneTouchStop","value":"AAAAAgAAABoAAABjAw=="}]
   ]
}

Sample Code

Console Application Example

For complete example text, see project code on GitHub.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SonyAPILib;

// SonyAPILib.dll library written by Kirk Herron.
// This Example Console application shows how to use the SonyAPILib in your own applications.
// This API connects to Sony Smart Devices(TV, Blue Ray, Tuners) via LAN or Wifi connection.
// Provides a method to send IRCC Remote control commands throught the LAN connection and remotely control the device.
// Retrives the devices Remote Command list for your own use or needs.
// Uses UPnP Protocols for device discovery, sending commands and future advanced features.

namespace ConsoleExample
{
    class Program
    {   
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("SonyAPILib v5.0 by: Kirk Herron");
            Console.WriteLine("Starting Console Example Program");
            Console.WriteLine("================================");
            
            // 1st create new instance of the SonyAPILib
            // This Class will be used to Discover Sony Devices on the LAN.
            SonyAPI_Lib mySonyLib = new SonyAPI_Lib();

            #region Set Logging
            // Next Set the API logging information.
            // Enable Logging: default is set to FALSE.
            mySonyLib.LOG.enableLogging = true;

            // Set Logging Level. 
            // Set to "Basic" to only Log Minimum information
            // Set to "All" for all Logging information
            // Default is set to "Basic"
            mySonyLib.LOG.enableLogginglev = "All";

            // Set where the logging file will be saved.
            // Folder will be created if it does not exist!
            // Set to Null to use Default
            // Default is set to C:\ProgramData\Sony
            mySonyLib.LOG.loggingPath = null;

            // Set the name of the Logging file.
            // Default is set to SonyAPILib_LOG.txt
            mySonyLib.LOG.loggingName = "SonyAPILib_LOG.txt";

            // Clears the existing log file and starts a new one
            // Send Null as the param to just clear the file and start a new one
            // Enter a new File name as the param, and log file will be copied to new name before it is cleared.
            // Example: mySonyLib.LOG.clearlog(datestamp + "_Old_Sony_Log_File.txt");
            mySonyLib.LOG.clearLog(null);
            #endregion

            #region Discovery
            // Perform a Discovery to find all/any compatiable devices on the LAN.
            // Returns a list of all Sony Devices that matches service criteria.
            // Send null as default service to locate Sony devices that support the IRCC service.
            // If a different service is used, the SonyAPILib may NOT function properly. (To be Used in Future projects)
            // Return value is a list with each item containg a SonyDevice object.
            // Each returned object will contain only the Name of the Device along with it's IP Address
            // All other properties will be filled in when the device is Initialized.
            Console.WriteLine("Searching for Devices...");
            List<SonyAPI_Lib.SonyDevice> fDev = mySonyLib.API.sonyDiscover(null);
            #endregion

            // fDev.Count will return the number of devices found
            #region Console Output 
            Console.WriteLine("Device Count: " + fDev.Count);
            Console.WriteLine("---------------------------------");
            int i = 0;
            foreach (SonyAPI_Lib.SonyDevice fd in fDev)
            {
                Console.WriteLine(i + " - " + fd.Name);
                i = i + 1;
            }
            Console.WriteLine("---------------------------------");
            #endregion

            // TODO: Here you can perform task or other code as to which device to select or use.
            // You could also do a For Next loop and go through each one.

            // This example checks if there are any devices in the list, if not then Exit
            if (fDev.Count > 0)
            {
                // Allow User to select from the devices found which one to test
                Console.WriteLine("Enter the Device # to Test");
                string cki;
                cki = Console.ReadLine();
                SonyAPI_Lib.SonyDevice mySonyDevice = new SonyAPI_Lib.SonyDevice();

                // Here you can save the device information to a database or text file.
                // This will allow you to Initialize a device WITHOUT having to run the sonyDiscover() method every time.
                // Once you have discovered the devices, saving their information will speed up your application the next time you run it!

                // You can Initialize a device with only the following minimimal information: 
                //     1) The Device Name - MUST Match what the device returns as it's name. (selDev.Name)
                //     2) The IP address of the device. (selDev.Device_IP_Address)

                // Now we initialize a new sonyDevice object with the selected device above.
                #region initialize
                // The Initialize method is used to fill in all the other required properties for the device.
                // For example, during the Initiliation process, the device port, mac, command list and UPnP services are
                // discovered and set to the device object.
                // Also the Registered porperty is checked during this process.
                // However, on Generation 1 devices, this will return a false value if the device is not powered ON.
                // See Wiki page on GitHub for more information about the Initialize method.

                // You MUST perform an Initialize on the device before doing anything else!

                // From above, selDev or each item in the list fDev[] is a SonyDevice object.

                // To initialize with the default information retrieved from sonyDiscovery, use the following method:
                // mySonyDevice.initialize(fDev[1]) or by setting the index to the device you wish.

                // You can also manually initialize the sonyDevice by setting the device information yourself.
                // To manually initialize without using sonyDiscovery, use the following method:
                        // mySonyDevice.Name = "NameOfMySonyDevice";
                        // mySonyDevice.Device_IP_Address = "192.168.0.66";
                        // mySonyDevice.Server_Name = "TheNameOfMyApplication";
                        // mySonyDevice.initialize();
                // This information could be retrieved from a database or other file as your desire.

                // This example will use the first method to initialize the device chosen by the user.
                Console.WriteLine("");
                Console.WriteLine(fDev[Convert.ToInt32(cki)].Name + ": Performing Initilization....");
                mySonyDevice.initialize(fDev[Convert.ToInt32(cki)]);

                #endregion

                // Next we will check the status of the device.
                #region Check Status
                
                //The following will return the current status of the device
                //The value returned is: status_name:value
                //An example would be: viewing:TV
                //An emplty string "" will be returned if there is no response from the device. This could also mean the device is off.
                //Also, this method requires the device to be registered on Generation 1 and 2 devices.
                Console.WriteLine(mySonyDevice.Name + ": Checking Device Status....");
                string status = mySonyDevice.checkStatus(); 
                if (status == "" | status == null)
                {
                    // NO Response!!
                }
                else
                {
                    #region Console Output
                    Console.WriteLine("");
                    Console.WriteLine("Get Status returned: " + status);
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                    #endregion
                }
               
                #endregion

                // Now we must register with the device
                #region register

                bool mySonyReg = false;

                // first check to see if the initize process determined the registration value.
                if (mySonyDevice.Registered == false)
                {
                    Console.WriteLine(mySonyDevice.Name + ": Performing Registration....");
                    Console.WriteLine("Before continuing, you may need to set the device to Registration Mode,");
                    Console.WriteLine("Confirm Registration or enter the Registration PIN code.");
                    Console.WriteLine("Go to the device and perfrom any step, or be ready to before ehitting enter below!");
                    Console.WriteLine("=====================================");
                    Console.WriteLine("Hit any key to continue");
                    Console.ReadKey();
                    // The next method is very IMPORTANT.

                    // YOU MUST RUN THE FOLLOWING METHOD AND RECEIVE A SUCCESSFUL RETURN at least ONCE!
                    // Before you can send any IRCC commands or receive and data back from the device!

                    // The very first time this is executed, you will need to be at your device (TV, Blue Ray)
                    // to confim the registration. Also, some devices (Blue Ray, Home Theater Tuners) require you to put
                    // the device in to "Registration" mode, before you try to register this application as
                    // a controlling device. (Registration uses the MAC address of the computer/device trying
                    //to gain control). If installed on more than 1 computer, each one will require registration.

                    // Register as Controller 
                    // Returns true if successful
                    // Returns false if not successful 

                    mySonyReg = mySonyDevice.register();

                    // Check if register returned false
                    if (mySonyDevice.Registered == false)
                    {
                        //Check if Generaton 3. If yes, prompt for pin code
                        if (mySonyDevice.Generation == 3)
                        {
                            string ckii;
                            Console.WriteLine("Enter PIN Code.");
                            ckii = Console.ReadLine();
                            // Send PIN code to TV to create Autorization cookie
                            Console.WriteLine("Sending Authitication PIN Code.");
                            mySonyReg = mySonyDevice.sendAuth(ckii);
                        }
                    }
                }
                else
                {
                    mySonyReg = true;
                }
                #endregion

                //TODO: Add more code in case of false, or true
                #region Console Output
                Console.WriteLine("Registration returned: " + mySonyReg.ToString());
                Console.WriteLine("---------------------------------");
                Console.WriteLine("");
                #endregion

                // This example will: If true, display device information
                #region Console Output
                if (mySonyReg)
                {

                    Console.WriteLine("Device Information");
                    Console.WriteLine("Mame: " + mySonyDevice.Name);
                    Console.WriteLine("Mac Address: " + mySonyDevice.Device_Macaddress);
                    Console.WriteLine("IP Address: " + mySonyDevice.Device_IP_Address);
                    Console.WriteLine("Port: " + mySonyDevice.Device_Port);
                    Console.WriteLine("Generation: " + mySonyDevice.Generation);
                    Console.WriteLine("Registration: " + mySonyDevice.Registered.ToString());
                    Console.WriteLine("Server Name: " + mySonyDevice.Server_Name);
                    Console.WriteLine("Server Mac: " + mySonyDevice.Server_Macaddress);
                    Console.WriteLine("Action List URL: " + mySonyDevice.actionList_URL);
                    Console.WriteLine("IRCC Control URL: " + mySonyDevice.control_URL);
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                }
                else
                {
                    // Display this if NOT true
                    Console.WriteLine("There was an error");
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                }
                #endregion

                // Get the IRCC command list from the device so we know it's capabilities.
                #region get_remote_command_list
                // The next command is used to retrieve the IRCC command list from the device.
                // ### You must register before this method will return any data ###
                // This method will populate the Commands list in the SonyDevice object when executed.
                // This Methed also returnes a string that contains the contents of the Devices Command List XML file for your own use.
                Console.WriteLine(mySonyDevice.Name + ": Retrieving Remote Command List");
                string CmdList = mySonyDevice.get_remote_command_list();

                // TODO: Parse this information as your application requires.
                // convert to an XMLDocument or dataset for your own use
                #endregion

                // Checks if the list contains any data
                #region Console Output
                if (CmdList != "")
                {
                    Console.WriteLine("Retrieved Command List Successful");
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                }
                else
                {
                    Console.WriteLine("ERROR Retrieving Command List");
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                }
                #endregion

                // Get the IRCC command value by searching the command name
                #region getIRCCcommandString
                // The next method is used to search for an IRCC_Command that matches the param.

                // param is a string containing the command name to search for.
                // Returns a string containing the command's value (If Successful)
                // Returna a Null if the search command is not found in the devices IRCC command list

                // This example will search for the command "ChannelUp"
                Console.WriteLine(mySonyDevice.Name + ": Retrieving Command Value for: VolumeUp");
                string irccCmd = mySonyDevice.getIRCCcommandString("VolumeUp");

                //Check if command was found
                if (irccCmd == "")
                {
                    Console.WriteLine("Command Not Found: VolumeUp");
                }
                else
                {
                #endregion

                // Displays the IRCC command value retrieved
                #region Console Output
                    // Show the IRCC_Command value found information
                    Console.WriteLine("Found Command: VolumeUp");
                    Console.WriteLine("Command Value: " + irccCmd);
                    Console.WriteLine("---------------------------------");
                    Console.WriteLine("");
                    Console.WriteLine("Now we are ready to try to send a few IRCC commands to the device");
                    Console.WriteLine("Hit any key to continue");
                    Console.ReadKey();
                }
                #endregion

                // Next are 4 examples of how you can send the IRCC commands to the device
                #region Example 1
                // This first example will send a "VolumeUp" command value to the device
                // it asumes we already know the value to send to the device.
                // We will use the irccCmd we retrieved above in the getIRCCCommandString method.
                Console.WriteLine(mySonyDevice.Name + ": Sending Command Value " + irccCmd + " to device");
                string results = mySonyDevice.send_ircc(irccCmd);
                System.Threading.Thread.Sleep(500);  // give the device time to react before sending another command

                #region Console Output
                // Show the IRCC_Command value found information
                Console.WriteLine("Sent Command: VolumeUp:" + irccCmd);
                Console.WriteLine("Hit any key to continue");
                Console.WriteLine("---------------------------------");
                Console.ReadKey();
                #endregion
                #endregion

                #region Example 2
                // The next example will use the getIRCCcommandString("CommandName") method to get the command value for "VolumeDown".
                // Then send it to the device
                Console.WriteLine(mySonyDevice.Name + ": Sending Command VolumeDown to device");
                String mycommand = mySonyDevice.getIRCCcommandString("VolumeDown");
                mySonyDevice.send_ircc(mycommand);
                System.Threading.Thread.Sleep(500);  // give the device time to react before sending another command

                #region Console Output
                // Show the IRCC_Command value found information
                Console.WriteLine("Sent Command: VolumeDown:" + mycommand);
                Console.WriteLine("Hit any key to continue");
                Console.WriteLine("---------------------------------");
                Console.ReadKey();
                #endregion
                #endregion

                #region Example 3
                // The next example will use a combination of both examples above for the command "VolumeUp".
                Console.WriteLine(mySonyDevice.Name + ": Sending Command VolumeUp to device again");
                mySonyDevice.send_ircc(mySonyDevice.getIRCCcommandString("VolumeUp"));
                System.Threading.Thread.Sleep(500);  // give the device time to react before sending another command

                #region Console Output
                // Show the IRCC_Command value found information
                Console.WriteLine("Sent Command: VolumeUp:" + mycommand);
                Console.WriteLine("Hit any key to continue");
                Console.WriteLine("---------------------------------");
                Console.ReadKey();
                #endregion
                #endregion

                #region Example 4
                // The next example will use the "channel_set" method to send a complete channel number.
                // This example will use channel 47.1 since it is a valid station in my area. You can change this to what ever you need to.
                // This example should only be used on TV's, as Home theater systems and DVD players will not respond to this!
                Console.WriteLine(mySonyDevice.Name + ": Sending a Set Channel command to device, if device is a TV");
                string checkChannel = mySonyDevice.getIRCCcommandString("ChannelUp");
                if (checkChannel != "")
                {
                    mySonyDevice.channel_set("47.1");
                    System.Threading.Thread.Sleep(500);  // give the device time to react before sending another command
                }

                #region Console Output
                // Show the Set Channel Information
                if (checkChannel != "")
                {
                    Console.WriteLine("Sent Command: Channel_Set");
                    Console.WriteLine("Command Value: " + mySonyDevice.current_Channel);
                }
                else
                {
                    Console.WriteLine("Set Channel Not sent, Device is NOT a TV!");
                }
                #endregion
                #endregion

                #region Example 5
                Console.WriteLine("");
                Console.WriteLine("Now You Try.");
                Console.WriteLine("Here are the Commands: Hit any key to Continue.");
                Console.WriteLine("---------------------------------");                
                Console.ReadKey();
                foreach (SonyAPI_Lib.SonyCommands cmd in mySonyDevice.Commands)
                {
                    Console.WriteLine(cmd.name);
                }
                Console.WriteLine("---------------------------------");
                Console.WriteLine("Enter a command from the list above.");
                cki = Console.ReadLine();
                results = mySonyDevice.send_ircc(mySonyDevice.getIRCCcommandString(cki));
                #endregion

                #region Example 6
                Console.WriteLine("---------------------------------");
                Console.WriteLine("Now, using your TV remote control, navigate to a search screen.");
                Console.WriteLine("This can be Pandora, Youtube or any search where you enter TEXT.");
                Console.WriteLine("Now, enter the text here to send.");
                cki = Console.ReadLine();
                results = mySonyDevice.send_text(cki);
                Console.WriteLine("---------------------------------");
                #endregion

                Console.WriteLine("That's about it for now. Hit enter to Quit.");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Error: No Devices were found!");
                Console.ReadKey();
            }
        }
    }
}

Points of Interest

Use this API in your own applications for Home Automation or to just simply control your Sony device with your computer, Tablet or other device!

History

The latest version is 5.0

To report any issues, please visit https://github.com/KHerron/SonyAPILib/issues

License

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


Written By
Software Developer
United States United States
Programmer by Hobby, Coder by Habit

Comments and Discussions

 
QuestionCrestron Control? Pin
DKoz8729-Jan-16 6:29
DKoz8729-Jan-16 6:29 
AnswerRe: Crestron Control? Pin
HKHerron30-Jan-16 5:22
professionalHKHerron30-Jan-16 5:22 
QuestionUPDATE v5.2 now on GitHub Pin
HKHerron26-Jun-15 3:39
professionalHKHerron26-Jun-15 3:39 
QuestionMy 4K Sony speaks XML ! Pin
Dr Gadgit6-Apr-15 6:05
Dr Gadgit6-Apr-15 6:05 
AnswerRe: My 4K Sony speaks XML ! Pin
HKHerron6-Apr-15 8:21
professionalHKHerron6-Apr-15 8:21 
GeneralRe: My 4K Sony speaks XML ! Pin
Dr Gadgit11-Apr-15 7:19
Dr Gadgit11-Apr-15 7:19 
GeneralRe: My 4K Sony speaks XML ! Pin
HKHerron15-May-15 7:23
professionalHKHerron15-May-15 7:23 
GeneralRe: My 4K Sony speaks XML ! Pin
Dr Gadgit24-May-15 1:13
Dr Gadgit24-May-15 1:13 
QuestionChanges in Code Pin
HKHerron14-Feb-15 5:23
professionalHKHerron14-Feb-15 5:23 
QuestionTesting and Bug Reports Pin
HKHerron14-Feb-15 5:20
professionalHKHerron14-Feb-15 5:20 
AnswerRe: Testing and Bug Reports Pin
HKHerron3-Mar-15 3:42
professionalHKHerron3-Mar-15 3:42 

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.