12
12
import java .io .Reader ;
13
13
import java .io .StringWriter ;
14
14
import java .io .Writer ;
15
+ import java .net .InetAddress ;
15
16
import java .net .InetSocketAddress ;
16
17
import java .util .ArrayList ;
17
18
import java .util .Arrays ;
18
19
19
20
/**
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
21
23
* http://sc0ty.pl/2012/02/samsung-tv-network-remote-control-protocol/ .
22
24
*
23
- * @author Maarten Visscher
25
+ * @author Maarten Visscher <[email protected] >
24
26
*/
25
27
public class SamsungRemote {
26
28
@@ -41,22 +43,53 @@ public class SamsungRemote {
41
43
private final boolean debug ;
42
44
private final ArrayList <String > log ; // A very simple log which will be filled when debug==true and can be obtained from outside using getLog().
43
45
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
+
44
74
/**
45
75
* Opens a socket connection to the television.
46
76
*
47
77
* @param host the host name.
48
78
* @throws IOException if an I/O error occurs when creating the socket.
79
+ * @deprecated
49
80
*/
50
81
public SamsungRemote (String host ) throws IOException {
51
82
this (host , false );
52
83
}
53
84
54
85
/**
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.
56
88
*
57
89
* @param host the host name.
58
90
* @param debug whether or not to keep a log.
59
91
* @throws IOException if an I/O error occurs when creating the socket.
92
+ * @deprecated
60
93
*/
61
94
public SamsungRemote (String host , boolean debug ) throws IOException {
62
95
this .debug = debug ;
@@ -69,12 +102,15 @@ public SamsungRemote(String host, boolean debug) throws IOException {
69
102
}
70
103
71
104
/**
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.
73
107
*
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.
75
110
* @return the response from the television.
76
111
* @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
78
114
*/
79
115
public TVReply authenticate (String name ) throws IOException {
80
116
String hostAddress = socket .getLocalAddress ().getHostAddress ();
@@ -83,13 +119,16 @@ public TVReply authenticate(String name) throws IOException {
83
119
}
84
120
85
121
/**
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.
87
124
*
88
125
* @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.
90
128
* @return the response from the television.
91
129
* @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
93
132
*/
94
133
public TVReply authenticate (String id , String name ) throws IOException {
95
134
String hostAddress = socket .getLocalAddress ().getHostAddress ();
@@ -98,16 +137,19 @@ public TVReply authenticate(String id, String name) throws IOException {
98
137
}
99
138
100
139
/**
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
102
142
* while waiting for the television response.
103
143
*
104
144
* @param ip a parameter for the television.
105
145
* @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.
107
148
* @return the response from the television.
108
149
* @throws IOException if an I/O error occurs.
109
150
*/
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 {
111
153
emptyReaderBuffer (in );
112
154
113
155
log ("Authenticating with ip: " + ip + ", id: " + id + ", name: " + name + "." );
@@ -135,7 +177,8 @@ public TVReply authenticate(String ip, String id, String name) throws IOExceptio
135
177
}
136
178
137
179
/**
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.
139
182
*
140
183
* @param keycode the key code to send.
141
184
* @throws IOException if an I/O error occurs.
@@ -145,7 +188,8 @@ public void keycode(Keycode keycode) throws IOException {
145
188
}
146
189
147
190
/**
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.
149
193
*
150
194
* @param keycode the key code to send.
151
195
* @throws IOException if an I/O error occurs.
@@ -163,7 +207,8 @@ public void keycode(String keycode) throws IOException {
163
207
}
164
208
165
209
/**
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
167
212
* when you are successfully authenticated.
168
213
*
169
214
* @param keycode the key code to send.
@@ -174,7 +219,8 @@ public void keycodeAsync(Keycode keycode) throws IOException {
174
219
}
175
220
176
221
/**
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
178
224
* when you are successfully authenticated.
179
225
*
180
226
* @param keycode the key code to send.
@@ -189,8 +235,9 @@ public void keycodeAsync(String keycode) throws IOException {
189
235
}
190
236
191
237
/**
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).
194
241
*
195
242
* @throws IOException if an I/O error occurs.
196
243
*/
@@ -207,7 +254,8 @@ public void checkConnection() throws IOException {
207
254
* @return the authentication payload.
208
255
* @throws IOException if an I/O error occurs.
209
256
*/
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 {
211
259
StringWriter writer = new StringWriter ();
212
260
writer .write (0x64 );
213
261
writer .write (0x00 );
@@ -236,8 +284,10 @@ private String getKeycodePayload(String keycode) throws IOException {
236
284
}
237
285
238
286
/**
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.
241
291
*
242
292
* @param reader the reader.
243
293
* @return the payload which was sent with the relevant message.
@@ -296,7 +346,8 @@ private void writeString(Writer writer, String string) throws IOException {
296
346
}
297
347
298
348
/**
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.
300
351
*
301
352
* @param writer the writer.
302
353
* @param string the string to encode using base64 and write.
@@ -319,7 +370,8 @@ private String readString(Reader reader) throws IOException {
319
370
}
320
371
321
372
/**
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.
323
375
*
324
376
* @param reader the reader.
325
377
* @return the characters which were read.
@@ -347,7 +399,8 @@ private void emptyReaderBuffer(Reader reader) throws IOException {
347
399
}
348
400
349
401
/**
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
351
404
* (otherwise the array will be empty).
352
405
*
353
406
* @return a simple log.
@@ -372,7 +425,8 @@ private void log(String message) {
372
425
}
373
426
374
427
/**
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.
376
430
*/
377
431
public void close () {
378
432
log ("Closing socket connection." );
0 commit comments