123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package com.example.greenwatch.communication;
-
- import com.example.greenwatch.repositories.DeviceRepository;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.Enumeration;
-
- public class WiFiCommunication {
-
- private final InetAddress address;
- private final DatagramSocket socket;
- private final int port;
- private volatile String rxString;
- private String sendMessage;
- //private String sendMsg = "default";
- private boolean isNewMessage;
- volatile private boolean running;
- private static WiFiCommunication wifiCommunicationInstance;
- private DeviceRepository mDeviceRepository;
-
- private WiFiCommunication() {
- port = 1234;
- try {
- socket = new DatagramSocket(port);
- socket.setBroadcast(true);
- address = InetAddress.getByName("255.255.255.255");
- running = true;
- isNewMessage = false;
- new ReceiveThread().start();
- new SendThread().start();
- } catch (SocketException | UnknownHostException e) {
- throw new RuntimeException(e);
- }
- }
-
- public static synchronized WiFiCommunication getInstance() {
- if (wifiCommunicationInstance == null){
- wifiCommunicationInstance = new WiFiCommunication();
- }
- return wifiCommunicationInstance;
- }
-
- public void setDeviceRepository(DeviceRepository deviceRepository) {
- if (mDeviceRepository == null) {
- this.mDeviceRepository = deviceRepository;
- }
- }
-
- public void setNewMessage(String newMessage, boolean isNewMessage){
- this.sendMessage = newMessage;
- this.isNewMessage = isNewMessage;
- }
-
- public static String getLocalIpAddress() {
- try {
- for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
- NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
- for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
- InetAddress inetAddress = addresses.nextElement();
- if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
- return inetAddress.getHostAddress();
- }
- }
- }
- } catch (SocketException ex) {
- ex.printStackTrace();
- }
- return null;
- }
-
- public void stopCommunication() {
- running = false;
- socket.close();
- }
-
- private class SendThread extends Thread {
- private int tmpCnt = 0;
- @Override
- public void run() {
- try {
- do {
-
- if(isNewMessage)
- {
- isNewMessage = false;
- String txString = sendMessage;
- byte[] txBuffer = txString.getBytes();
-
- DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
-
- for(int i = 0; i < 20; i++) {
- socket.send(txPacket);
- }
- }
- } while (running);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private class ReceiveThread extends Thread {
- private String rxString = "";
- private String previousRxString = "";
- @Override
- public void run() {
- try {
- do {
- byte[] rxBuffer = new byte[1024];
- DatagramPacket packet = new DatagramPacket(rxBuffer, rxBuffer.length);
- socket.receive(packet);
- rxString = new String(packet.getData(), 0, packet.getLength());
-
- if(!previousRxString.equals(rxString)){
- mDeviceRepository.getNewReceivedMessage(rxString);
-
- }
- previousRxString = rxString;
-
- } while (running);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
|