Bitcoin

Bitcoin
Bitcoin

Controlling a television via Wi‑Fi using Java

 Controlling a television via Wi‑Fi using Java requires:


1. A smart TV or a device like a Chromecast/Fire Stick that exposes a network API.



2. A phone app (Java code on Android or a desktop app) that sends commands via HTTP or socket connection to the TV.





---


Simplified Example (Simulated TV over Wi‑Fi)


This example has two parts:


TV Server (runs on the TV or simulated computer)


Phone Controller (runs on the phone/computer)




---


1. TV Server (Simulated TV)


import java.io.*;

import java.net.*;


public class TVServer {

    private static boolean power = false;

    private static int volume = 10;

    private static int channel = 1;


    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(5000);

        System.out.println("TV Server running on port 5000...");


        while (true) {

            Socket socket = serverSocket.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String command = in.readLine();

            System.out.println("Received: " + command);


            switch (command) {

                case "POWER" -> power = !power;

                case "VOL_UP" -> { if (power) volume++; }

                case "VOL_DOWN" -> { if (power && volume > 0) volume--; }

                case "CHANNEL_NEXT" -> { if (power) channel++; }

                case "CHANNEL_PREV" -> { if (power && channel > 1) channel--; }

            }

            System.out.println("Power: " + power + ", Volume: " + volume + ", Channel: " + channel);

            socket.close();

        }

    }

}



---


2. Phone Controller (Client)


import java.io.*;

import java.net.*;

import java.util.Scanner;


public class PhoneControllerWiFi {

    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);

        String serverIP = "192.168.1.5"; // TV's IP address (change to real one)

        int port = 5000;


        while (true) {

            System.out.println("\n--- PHONE REMOTE (Wi-Fi) ---");

            System.out.println("1. Power ON/OFF");

            System.out.println("2. Volume Up");

            System.out.println("3. Volume Down");

            System.out.println("4. Channel Next");

            System.out.println("5. Channel Previous");

            System.out.println("0. Exit");

            System.out.print("Enter choice: ");

            int choice = sc.nextInt();


            String command = switch (choice) {

                case 1 -> "POWER";

                case 2 -> "VOL_UP";

                case 3 -> "VOL_DOWN";

                case 4 -> "CHANNEL_NEXT";

                case 5 -> "CHANNEL_PREV";

                case 0 -> { System.out.println("Exiting..."); return; }

                default -> "INVALID";

            };


            if (!command.equals("INVALID")) {

                try (Socket socket = new Socket(serverIP, port)) {

                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                    out.println(command);

                }

            }

        }

    }

}



---


How it Works


1. Run TVServer.java on one computer (simulating the TV).



2. Run PhoneControllerWiFi.java on another device connected to the same Wi‑Fi network.



3. Replace "192.168.1.5" with the actual IP address of the computer running TVServer.



4. Commands will be sent via TCP sockets.





---


To Control a Real TV


Many smart TVs (Samsung, LG, Android TV) support HTTP or WebSocket APIs.


You need to use their API docume

ntation to send actual commands.


Example: For an Android TV, you can send HTTP POST requests to http://<TV-IP>:8080/command.




No comments:

Post a Comment

Facebook