Open this paste on another device
The QR code contains the clean paste URL. It contains no password or delete token.
javaRead-only
1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class AndroidConnector extends JFrame {
private JTextField ipField;
private JTextField messageField;
private JTextArea console;
private JLabel statusLabel;
private Socket socket;
private PrintWriter output;
private BufferedReader input;
private final Object sendLock = new Object();
private JButton connectButton;
private JButton disconnectButton;
private JButton sendButton;
private static final int PORT = 5000;
public AndroidConnector() {
setTitle("Android Connector");
setSize(650, 500);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
dispose();
}
});
// Panel połączenia
JPanel connectionPanel = new JPanel(new GridBagLayout());
connectionPanel.setBorder(
BorderFactory.createTitledBorder("Połączenie z telefonem"));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
connectionPanel.add(new JLabel("Adres IP telefonu:"), c);
ipField = new JTextField("192.168.1.100", 15);
c.gridx = 1;
connectionPanel.add(ipField, c);
connectButton = new JButton("Połącz");
c.gridx = 2;
connectionPanel.add(connectButton, c);
disconnectButton = new JButton("Rozłącz");
disconnectButton.setEnabled(false);
c.gridx = 3;
connectionPanel.add(disconnectButton, c);
statusLabel = new JLabel("Status: Niepołączono");
statusLabel.setForeground(Color.RED);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
connectionPanel.add(statusLabel, c);
add(connectionPanel, BorderLayout.NORTH);
// Konsola
console = new JTextArea();
console.setEditable(false);
console.setFont(new Font("Monospaced", Font.PLAIN, 13));
JScrollPane scrollPane = new JScrollPane(console);
scrollPane.setBorder(
BorderFactory.createTitledBorder("Konsola komunikacji"));
add(scrollPane, BorderLayout.CENTER);
// Panel wiadomości
JPanel messagePanel = new JPanel(new BorderLayout(5, 5));
messagePanel.setBorder(
BorderFactory.createTitledBorder("Wysyłanie wiadomości"));
messageField = new JTextField();
sendButton = new JButton("Wyślij do telefonu");
sendButton.setEnabled(false);
messagePanel.add(messageField, BorderLayout.CENTER);
messagePanel.add(sendButton, BorderLayout.EAST);
add(messagePanel, BorderLayout.SOUTH);
connectButton.addActionListener(e -> connect());
disconnectButton.addActionListener(e -> disconnect());
sendButton.addActionListener(e -> sendMessage());
messageField.addActionListener(e -> sendMessage());
log("Android Connector uruchomiony.");
log("Oczekiwanie na połączenie z telefonem.");
}
private void connect() {
String ip = ipField.getText().trim();
if (ip.isEmpty()) {
log("Wpisz adres IP telefonu.");
return;
}
connectButton.setEnabled(false);
statusLabel.setText("Status: Łączenie...");
statusLabel.setForeground(Color.ORANGE);
new Thread(() -> {
Socket newSocket = new Socket();
try {
newSocket.connect(
new InetSocketAddress(ip, PORT), 5000);
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(
newSocket.getOutputStream(),
StandardCharsets.UTF_8), true);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
newSocket.getInputStream(),
StandardCharsets.UTF_8));
synchronized (sendLock) {
socket = newSocket;
output = writer;
input = reader;
}
SwingUtilities.invokeLater(() -> {
statusLabel.setText("Status: Połączono!");
statusLabel.setForeground(new Color(0, 150, 0));
connectButton.setEnabled(false);
disconnectButton.setEnabled(true);
sendButton.setEnabled(true);
ipField.setEnabled(false);
log("Połączono z telefonem: " + ip);
});
String message;
while ((message = reader.readLine()) != null) {
String received = message;
SwingUtilities.invokeLater(() ->
log("ANDROID: " + received));
}
logOnEDT("Telefon zakończył połączenie.");
} catch (IOException e) {
logOnEDT("Błąd połączenia: " + e.getMessage());
} finally {
try {
newSocket.close();
} catch (IOException ignored) {
}
synchronized (sendLock) {
if (socket == newSocket) {
socket = null;
output = null;
input = null;
}
}
SwingUtilities.invokeLater(() -> {
statusLabel.setText("Status: Niepołączono");
statusLabel.setForeground(Color.RED);
connectButton.setEnabled(true);
disconnectButton.setEnabled(false);
sendButton.setEnabled(false);
ipField.setEnabled(true);
});
}
}, "Android-Connection").start();
}
private void sendMessage() {
String message = messageField.getText().trim();
if (message.isEmpty()) {
return;
}
synchronized (sendLock) {
if (output == null || socket == null
|| socket.isClosed()) {
log("Brak połączenia z telefonem.");
return;
}
output.println(message);
if (output.checkError()) {
log("Nie udało się wysłać wiadomości.");
return;
}
log("ECLIPSE: " + message);
messageField.setText("");
}
}
private void disconnect() {
synchronized (sendLock) {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
log("Błąd rozłączania: " + e.getMessage());
}
}
}
}
private void log(String message) {
console.append(message + "\n");
console.setCaretPosition(console.getDocument().getLength());
}
private void logOnEDT(String message) {
SwingUtilities.invokeLater(() -> log(message));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
AndroidConnector app = new AndroidConnector();
app.setVisible(true);
});
}
}
Paste details
- Visibility
- Public
- Size
- 7.8 KB
- Protection
- Standard link access
- Retention
- Until 2026-12-10
Share with confidence
Unlisted links are not searchable, but anyone with the URL can open them. Never paste live credentials or personal data.