Skip to content

Commit 7353d16

Browse files
committed
Added constructor with InetAddress.
1 parent caae858 commit 7353d16

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

build.gradle

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
apply plugin: 'java'
22

3-
//sourceCompatibility = '1.7'
4-
version = '3.0.0'
3+
version = '3.1.0'
54
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
65

76
repositories {
87
mavenCentral()
9-
// You may define additional repositories, or even remove "mavenCentral()".
10-
// Read more about repositories here:
11-
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
128
}
139

1410
dependencies {
1511
compile 'commons-codec:commons-codec:1.10'
16-
// You can read more about how to add dependency here:
17-
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
18-
testCompile group: 'junit', name: 'junit', version: '4.10'
12+
//testCompile group: 'junit', name: 'junit', version: '4.10'
1913
}
2014

2115
// Adding all dependencies into jar.

src/main/java/nl/maartenvisscher/samsungtvcontrol/SamsungRemote.java

+79-25
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
import java.io.Reader;
1313
import java.io.StringWriter;
1414
import java.io.Writer;
15+
import java.net.InetAddress;
1516
import java.net.InetSocketAddress;
1617
import java.util.ArrayList;
1718
import java.util.Arrays;
1819

1920
/**
20-
* API for controlling Samsung Smart TVs using a socket connection on port 55000. The protocol information has been gathered from
21+
* API for controlling Samsung Smart TVs using a socket connection on port
22+
* 55000. The protocol information has been gathered from
2123
* http://sc0ty.pl/2012/02/samsung-tv-network-remote-control-protocol/ .
2224
*
23-
* @author Maarten Visscher
25+
* @author Maarten Visscher <[email protected]>
2426
*/
2527
public class SamsungRemote {
2628

@@ -41,22 +43,53 @@ public class SamsungRemote {
4143
private final boolean debug;
4244
private final ArrayList<String> log; // A very simple log which will be filled when debug==true and can be obtained from outside using getLog().
4345

46+
/**
47+
* Opens a socket connection to the television.
48+
*
49+
* @param host the host address.
50+
* @throws IOException if an I/O error occurs when creating the socket.
51+
*/
52+
public SamsungRemote(InetAddress host) throws IOException {
53+
this(host, false);
54+
}
55+
56+
/**
57+
* Opens a socket connection to the television and keeps a simple log when
58+
* debug is true.
59+
*
60+
* @param host the host address.
61+
* @param debug whether or not to keep a log.
62+
* @throws IOException if an I/O error occurs when creating the socket.
63+
*/
64+
public SamsungRemote(InetAddress host, boolean debug) throws IOException {
65+
this.debug = debug;
66+
this.log = new ArrayList<>();
67+
this.socket = new Socket();
68+
socket.connect(new InetSocketAddress(host, PORT), SO_TIMEOUT);
69+
socket.setSoTimeout(SO_TIMEOUT);
70+
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
71+
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
72+
}
73+
4474
/**
4575
* Opens a socket connection to the television.
4676
*
4777
* @param host the host name.
4878
* @throws IOException if an I/O error occurs when creating the socket.
79+
* @deprecated
4980
*/
5081
public SamsungRemote(String host) throws IOException {
5182
this(host, false);
5283
}
5384

5485
/**
55-
* Opens a socket connection to the television and keeps a simple log when debug is true.
86+
* Opens a socket connection to the television and keeps a simple log when
87+
* debug is true.
5688
*
5789
* @param host the host name.
5890
* @param debug whether or not to keep a log.
5991
* @throws IOException if an I/O error occurs when creating the socket.
92+
* @deprecated
6093
*/
6194
public SamsungRemote(String host, boolean debug) throws IOException {
6295
this.debug = debug;
@@ -69,12 +102,15 @@ public SamsungRemote(String host, boolean debug) throws IOException {
69102
}
70103

71104
/**
72-
* Authenticates with the television using host IP address for the ip and id parameters.
105+
* Authenticates with the television using host IP address for the ip and id
106+
* parameters.
73107
*
74-
* @param name the name for this controller, which is displayed on the television.
108+
* @param name the name for this controller, which is displayed on the
109+
* television.
75110
* @return the response from the television.
76111
* @throws IOException if an I/O error occurs.
77-
* @see SamsungRemote#authenticate(java.lang.String, java.lang.String, java.lang.String) authenticate
112+
* @see SamsungRemote#authenticate(java.lang.String, java.lang.String,
113+
* java.lang.String) authenticate
78114
*/
79115
public TVReply authenticate(String name) throws IOException {
80116
String hostAddress = socket.getLocalAddress().getHostAddress();
@@ -83,13 +119,16 @@ public TVReply authenticate(String name) throws IOException {
83119
}
84120

85121
/**
86-
* Authenticates with the television using host IP address for the ip parameter.
122+
* Authenticates with the television using host IP address for the ip
123+
* parameter.
87124
*
88125
* @param id a parameter for the television.
89-
* @param name the name for this controller, which is displayed on the television.
126+
* @param name the name for this controller, which is displayed on the
127+
* television.
90128
* @return the response from the television.
91129
* @throws IOException if an I/O error occurs.
92-
* @see SamsungRemote#authenticate(java.lang.String, java.lang.String, java.lang.String) authenticate
130+
* @see SamsungRemote#authenticate(java.lang.String, java.lang.String,
131+
* java.lang.String) authenticate
93132
*/
94133
public TVReply authenticate(String id, String name) throws IOException {
95134
String hostAddress = socket.getLocalAddress().getHostAddress();
@@ -98,16 +137,19 @@ public TVReply authenticate(String id, String name) throws IOException {
98137
}
99138

100139
/**
101-
* Authenticates with the television. Has to be done every time when a new socket connection has been made, prior to sending key codes. Blocks
140+
* Authenticates with the television. Has to be done every time when a new
141+
* socket connection has been made, prior to sending key codes. Blocks
102142
* while waiting for the television response.
103143
*
104144
* @param ip a parameter for the television.
105145
* @param id a parameter for the television.
106-
* @param name the name for this controller, which is displayed on the television.
146+
* @param name the name for this controller, which is displayed on the
147+
* television.
107148
* @return the response from the television.
108149
* @throws IOException if an I/O error occurs.
109150
*/
110-
public TVReply authenticate(String ip, String id, String name) throws IOException {
151+
public TVReply authenticate(String ip, String id, String name)
152+
throws IOException {
111153
emptyReaderBuffer(in);
112154

113155
log("Authenticating with ip: " + ip + ", id: " + id + ", name: " + name + ".");
@@ -135,7 +177,8 @@ public TVReply authenticate(String ip, String id, String name) throws IOExceptio
135177
}
136178

137179
/**
138-
* Sends a key code to TV, blocks shortly waiting for TV response to check delivery. Only works when you are successfully authenticated.
180+
* Sends a key code to TV, blocks shortly waiting for TV response to check
181+
* delivery. Only works when you are successfully authenticated.
139182
*
140183
* @param keycode the key code to send.
141184
* @throws IOException if an I/O error occurs.
@@ -145,7 +188,8 @@ public void keycode(Keycode keycode) throws IOException {
145188
}
146189

147190
/**
148-
* Sends a key code to TV, blocks shortly waiting for TV response to check delivery. Only works when you are successfully authenticated.
191+
* Sends a key code to TV, blocks shortly waiting for TV response to check
192+
* delivery. Only works when you are successfully authenticated.
149193
*
150194
* @param keycode the key code to send.
151195
* @throws IOException if an I/O error occurs.
@@ -163,7 +207,8 @@ public void keycode(String keycode) throws IOException {
163207
}
164208

165209
/**
166-
* Sends a key code to TV in a non-blocking manner, thus it does not check the delivery (use checkConnection() to poll the TV status). Only works
210+
* Sends a key code to TV in a non-blocking manner, thus it does not check
211+
* the delivery (use checkConnection() to poll the TV status). Only works
167212
* when you are successfully authenticated.
168213
*
169214
* @param keycode the key code to send.
@@ -174,7 +219,8 @@ public void keycodeAsync(Keycode keycode) throws IOException {
174219
}
175220

176221
/**
177-
* Sends a key code to TV in a non-blocking manner, thus it does not check the delivery (use checkConnection() to poll the TV status). Only works
222+
* Sends a key code to TV in a non-blocking manner, thus it does not check
223+
* the delivery (use checkConnection() to poll the TV status). Only works
178224
* when you are successfully authenticated.
179225
*
180226
* @param keycode the key code to send.
@@ -189,8 +235,9 @@ public void keycodeAsync(String keycode) throws IOException {
189235
}
190236

191237
/**
192-
* Checks the connection by sending an empty key code, does not return anything but instead throws an exception when a problem arose (for instance
193-
* the TV turned off).
238+
* Checks the connection by sending an empty key code, does not return
239+
* anything but instead throws an exception when a problem arose (for
240+
* instance the TV turned off).
194241
*
195242
* @throws IOException if an I/O error occurs.
196243
*/
@@ -207,7 +254,8 @@ public void checkConnection() throws IOException {
207254
* @return the authentication payload.
208255
* @throws IOException if an I/O error occurs.
209256
*/
210-
private String getAuthenticationPayload(String ip, String id, String name) throws IOException {
257+
private String getAuthenticationPayload(String ip, String id, String name)
258+
throws IOException {
211259
StringWriter writer = new StringWriter();
212260
writer.write(0x64);
213261
writer.write(0x00);
@@ -236,8 +284,10 @@ private String getKeycodePayload(String keycode) throws IOException {
236284
}
237285

238286
/**
239-
* Reads an incoming message or waits for a new one when it is not relevant. I believe non-relevant messages has to do with showing or hiding of
240-
* windows on the TV, and start with 0x0a. This method returns the payload of the relevant message.
287+
* Reads an incoming message or waits for a new one when it is not relevant.
288+
* I believe non-relevant messages has to do with showing or hiding of
289+
* windows on the TV, and start with 0x0a. This method returns the payload
290+
* of the relevant message.
241291
*
242292
* @param reader the reader.
243293
* @return the payload which was sent with the relevant message.
@@ -296,7 +346,8 @@ private void writeString(Writer writer, String string) throws IOException {
296346
}
297347

298348
/**
299-
* Encodes the string with base64 and writes the result length and the result itself to the writer.
349+
* Encodes the string with base64 and writes the result length and the
350+
* result itself to the writer.
300351
*
301352
* @param writer the writer.
302353
* @param string the string to encode using base64 and write.
@@ -319,7 +370,8 @@ private String readString(Reader reader) throws IOException {
319370
}
320371

321372
/**
322-
* Reads the next characters from the reader using the length given in the first byte.
373+
* Reads the next characters from the reader using the length given in the
374+
* first byte.
323375
*
324376
* @param reader the reader.
325377
* @return the characters which were read.
@@ -347,7 +399,8 @@ private void emptyReaderBuffer(Reader reader) throws IOException {
347399
}
348400

349401
/**
350-
* Returns a simple log with for instance TV response payloads as string array, will only be filled when this class is constructed with debug true
402+
* Returns a simple log with for instance TV response payloads as string
403+
* array, will only be filled when this class is constructed with debug true
351404
* (otherwise the array will be empty).
352405
*
353406
* @return a simple log.
@@ -372,7 +425,8 @@ private void log(String message) {
372425
}
373426

374427
/**
375-
* Closes the socket connection. Should always be called at the end of a session.
428+
* Closes the socket connection. Should always be called at the end of a
429+
* session.
376430
*/
377431
public void close() {
378432
log("Closing socket connection.");

0 commit comments

Comments
 (0)