Added WiFi Communication to project
This commit is contained in:
parent
d21c22cd02
commit
792c650400
@ -2,6 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.greenwatch">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
@ -9,9 +11,6 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.GreenWatch">
|
||||
<activity
|
||||
android:name=".ConnectionActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".VideodetectionAndAccelerometerActivity"
|
||||
android:exported="false" />
|
||||
|
@ -1,27 +0,0 @@
|
||||
package com.example.greenwatch;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
public class ConnectionActivity extends AppCompatActivity {
|
||||
|
||||
private Button backToMainActivity;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_connection);
|
||||
|
||||
backToMainActivity = (Button) findViewById(R.id.connectionBackToMainActivity);
|
||||
|
||||
backToMainActivity.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@ package com.example.greenwatch;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
@ -11,6 +13,7 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.greenwatch.adapters.DeviceListAdapter;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.viewmodels.MainActivityViewModel;
|
||||
|
||||
@ -52,6 +55,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
|
||||
@Override
|
||||
public void onChanged(List<Device> devices) {
|
||||
deviceListAdapter.setDevices(devices);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Time Stamp: ");
|
||||
sb.append(mMainActivityViewModel.getTimeStamp(mMainActivityViewModel.getLocalDeviceUUID()));
|
||||
@ -134,12 +138,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
startActivity(intent);
|
||||
}
|
||||
public void openVideodetectionAndAccelerometerActivity(){
|
||||
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
|
||||
startActivity(intent);
|
||||
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
|
||||
//startActivity(intent);
|
||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0);
|
||||
}
|
||||
public void openConnectionActivity(){
|
||||
Intent intent = new Intent(this, ConnectionActivity.class);
|
||||
startActivity(intent);
|
||||
//Intent intent = new Intent(this, ConnectionActivity.class);
|
||||
//startActivity(intent);
|
||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", true, "Audio", 10);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
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 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) {
|
||||
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;
|
||||
//todo: adapt send String
|
||||
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ package com.example.greenwatch.repositories;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.communication.WiFiCommunication;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -12,21 +13,30 @@ import java.util.UUID;
|
||||
|
||||
public class DeviceRepository {
|
||||
|
||||
private String delimiter = ", ";
|
||||
private String deviceUUID;
|
||||
private final String delimiter = ", ";
|
||||
private final String SensorStatusKey = "An";
|
||||
private String localDeviceUUID;
|
||||
private static DeviceRepository deviceRepositoryInstance;
|
||||
private WiFiCommunication mWiFiCommunication;
|
||||
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>();
|
||||
private HashMap<String, Device> connectedDevicesList = new HashMap<>();
|
||||
private HashMap<String, String> deviceIDMapper = new HashMap<>();
|
||||
|
||||
private DeviceRepository() {
|
||||
setLocalDeviceUUID();
|
||||
}
|
||||
|
||||
public static synchronized DeviceRepository getInstance() {
|
||||
if (deviceRepositoryInstance == null){
|
||||
deviceRepositoryInstance = new DeviceRepository();
|
||||
deviceRepositoryInstance.setDeviceUUID();
|
||||
}
|
||||
return deviceRepositoryInstance;
|
||||
}
|
||||
|
||||
public void setWiFiCommunication(WiFiCommunication wiFiCommunication) {
|
||||
this.mWiFiCommunication = wiFiCommunication;
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Device>> getConnectedDeviceList() {
|
||||
setMutableLiveDataValue();
|
||||
return deviceList;
|
||||
@ -34,24 +44,12 @@ public class DeviceRepository {
|
||||
|
||||
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){
|
||||
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
setDeviceIDMapper(deviceID);
|
||||
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice);
|
||||
setMutableLiveDataValue();
|
||||
}
|
||||
|
||||
public void createNewRemoteDevice(String message) {
|
||||
String[] messageString = messageStringSplitter(message);
|
||||
String timeStamp = messageString[0];
|
||||
String deviceID = messageString[1];
|
||||
boolean sensorStatus = Boolean.valueOf(messageString[1]);
|
||||
String sensorType = messageString[3];
|
||||
int sensorMassage = Integer.valueOf(messageString[4]);
|
||||
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
}
|
||||
|
||||
public String messageStringBuilder(String deviceID) {
|
||||
private String messageStringBuilder(String deviceID) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
Device device = connectedDevicesList.get(deviceID);
|
||||
if(device != null) {
|
||||
@ -66,14 +64,25 @@ public class DeviceRepository {
|
||||
.append(device.getSensorMassage());
|
||||
return message.toString();
|
||||
}
|
||||
return message.toString();
|
||||
else {
|
||||
message.append("")
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("")
|
||||
.append(delimiter)
|
||||
.append("");
|
||||
return message.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String getLocalDeviceUUID() {
|
||||
return deviceUUID;
|
||||
return localDeviceUUID;
|
||||
}
|
||||
|
||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
String checkedDeviceID = checkDeviceID(deviceID);
|
||||
Device device = connectedDevicesList.get(checkedDeviceID);
|
||||
if(device != null) {
|
||||
@ -190,24 +199,51 @@ public class DeviceRepository {
|
||||
|
||||
private void addToConnectedDeviceList(String key, Device device) {
|
||||
connectedDevicesList.put(key, device);
|
||||
if (key.equals(checkDeviceID(localDeviceUUID))) {
|
||||
mWiFiCommunication.setNewMessage(messageStringBuilder(device.getDeviceID()),true);
|
||||
}
|
||||
}
|
||||
|
||||
private String checkDeviceID(String deviceID) {
|
||||
if(!deviceIDMapper.isEmpty() && deviceID.equals(deviceUUID)) {
|
||||
if(!deviceIDMapper.isEmpty() && deviceID.equals(localDeviceUUID)) {
|
||||
return deviceIDMapper.get(deviceID);
|
||||
}
|
||||
return deviceID;
|
||||
}
|
||||
|
||||
private void setDeviceIDMapper(String deviceID) {
|
||||
deviceIDMapper.put(deviceUUID, deviceID);
|
||||
deviceIDMapper.put(localDeviceUUID, deviceID);
|
||||
}
|
||||
|
||||
private void setDeviceUUID(){
|
||||
this.deviceUUID = UUID.randomUUID().toString();
|
||||
private void setLocalDeviceUUID(){
|
||||
this.localDeviceUUID = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
private String[] messageStringSplitter(String message) {
|
||||
return message.split(delimiter);
|
||||
}
|
||||
|
||||
public void getNewReceivedMessage(String newMessage) {
|
||||
String[] messageString = messageStringSplitter(newMessage);
|
||||
String timeStamp = messageString[0];
|
||||
String deviceID = messageString[1];
|
||||
boolean sensorStatus = convertSensorStatus(messageString[2]);
|
||||
String sensorType = messageString[3];
|
||||
int sensorMassage = Integer.valueOf(messageString[4]);
|
||||
|
||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!connectedDevicesList.containsKey(deviceID)) {
|
||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
else {
|
||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean convertSensorStatus(String status) {
|
||||
return status.equals(SensorStatusKey);
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ public class AccelerometerViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.setDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
|
@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.example.greenwatch.communication.WiFiCommunication;
|
||||
import com.example.greenwatch.models.Device;
|
||||
import com.example.greenwatch.repositories.DeviceRepository;
|
||||
|
||||
@ -14,12 +15,18 @@ public class MainActivityViewModel extends ViewModel {
|
||||
private MutableLiveData<List<Device>> mDeviceList;
|
||||
private DeviceRepository mDeviceRepository;
|
||||
|
||||
|
||||
public void init() {
|
||||
WiFiCommunication mWiFiCommunication;
|
||||
if(mDeviceList != null) {
|
||||
return;
|
||||
}
|
||||
//todo: check if WiFi instanz can be hold only by the repository
|
||||
mDeviceRepository = DeviceRepository.getInstance();
|
||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "", 0);
|
||||
mWiFiCommunication = WiFiCommunication.getInstance();
|
||||
mDeviceRepository.setWiFiCommunication(mWiFiCommunication);
|
||||
mWiFiCommunication.setDeviceRepository(mDeviceRepository);
|
||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
|
||||
mDeviceList = mDeviceRepository.getConnectedDeviceList();
|
||||
}
|
||||
|
||||
@ -28,7 +35,7 @@ public class MainActivityViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
|
||||
mDeviceRepository.setDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
|
||||
}
|
||||
|
||||
public void setTimeStamp(String deviceID, String timeStamp) {
|
||||
|
@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="10dp"
|
||||
tools:context=".ConnectionActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvConnectionStatusmessage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Connection Activity">
|
||||
</TextView>
|
||||
|
||||
<Button
|
||||
android:id="@+id/connectionBackToMainActivity"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Back to MainActivity">
|
||||
</Button>
|
||||
|
||||
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user