Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
316e19236f | ||
|
|
b337d86655 | ||
|
|
18eb62b034 | ||
|
|
23ac26d0a4 | ||
|
|
518e207032 | ||
|
|
7ed3408d01 |
@@ -3,6 +3,11 @@
|
|||||||
package="com.example.greenwatch">
|
package="com.example.greenwatch">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
|
||||||
|
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
|
||||||
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|||||||
@@ -0,0 +1,225 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
import android.graphics.SurfaceTexture;
|
||||||
|
import android.media.MediaCodec;
|
||||||
|
import android.media.MediaExtractor;
|
||||||
|
import android.media.MediaFormat;
|
||||||
|
import android.media.MediaMuxer;
|
||||||
|
import android.media.MediaRecorder;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.view.Surface;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
public class AlarmRecorder {
|
||||||
|
private String videoPath;
|
||||||
|
private String audioPath;
|
||||||
|
private MediaFormat videoFormat;
|
||||||
|
private MediaFormat audioFormat;
|
||||||
|
private MediaExtractor videoExtractor;
|
||||||
|
private MediaExtractor audioExtractor;
|
||||||
|
private Runnable audioRecorderRunnable;
|
||||||
|
private Runnable videoRecorderRunnable;
|
||||||
|
private MediaRecorder videoRecorder;
|
||||||
|
private MediaRecorder audioRecorder;
|
||||||
|
//private SurfaceHolder previewHolder;
|
||||||
|
private Surface surface;
|
||||||
|
private Thread videoThread; // Video-Thread als Instanzvariable
|
||||||
|
private Thread audioThread; // Audio-Thread als Instanzvariable
|
||||||
|
|
||||||
|
public AlarmRecorder(SurfaceTexture surfaceTexture) {
|
||||||
|
audioRecorder = new MediaRecorder();
|
||||||
|
videoRecorder = new MediaRecorder();
|
||||||
|
videoExtractor = new MediaExtractor();
|
||||||
|
audioExtractor = new MediaExtractor();
|
||||||
|
this.surface = new Surface(surfaceTexture);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startRecording() {
|
||||||
|
createStoragePaths(); //Speicherort und -namen für Audio- und Video-Datei
|
||||||
|
|
||||||
|
audioRecorderRunnable = new AudioRecorder(audioRecorder, audioPath);
|
||||||
|
videoRecorderRunnable = new VideoRecorder(videoRecorder, videoPath, surface);
|
||||||
|
audioThread = new Thread(audioRecorderRunnable);
|
||||||
|
videoThread = new Thread(videoRecorderRunnable);
|
||||||
|
|
||||||
|
//Threads starten
|
||||||
|
videoThread.start();
|
||||||
|
audioThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopRecording() {
|
||||||
|
try {
|
||||||
|
stopVideoRecording();
|
||||||
|
stopAudioRecording();
|
||||||
|
|
||||||
|
waitTillThreadsStopped();
|
||||||
|
File videoFile = new File(videoPath); //Speichere das aufgenommene Video
|
||||||
|
File audioFile = new File(audioPath); //Speichere die aufgenommene Audio
|
||||||
|
|
||||||
|
if (videoFile.exists() && audioFile.exists()) {
|
||||||
|
//Wenn Video- und Audioaufzeichnung gestoppt und abgespeichert sind, beginne mit dem Mergeprozess der beiden
|
||||||
|
mergeVideoWithAudio();
|
||||||
|
}
|
||||||
|
} catch (RuntimeException stopException) {
|
||||||
|
stopException.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createStoragePaths(){
|
||||||
|
//Pfade zum Zwischenspeichern der aufgenommenen Audio und Video-Datei
|
||||||
|
String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
|
||||||
|
String dcimDirectory = externalStorageDirectory + "/DCIM";
|
||||||
|
videoPath = dcimDirectory + "/video.mp4";
|
||||||
|
audioPath = dcimDirectory + "/audio.mp3";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopVideoRecording(){
|
||||||
|
if (videoRecorder != null) {
|
||||||
|
videoRecorder.stop();
|
||||||
|
videoRecorder.release();
|
||||||
|
videoRecorder = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopAudioRecording(){
|
||||||
|
if (audioRecorder != null) {
|
||||||
|
audioRecorder.stop();
|
||||||
|
audioRecorder.release();
|
||||||
|
audioRecorder = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitTillThreadsStopped(){
|
||||||
|
try {
|
||||||
|
videoThread.join();
|
||||||
|
audioThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergeVideoWithAudio() {
|
||||||
|
try {
|
||||||
|
setVideoExtractorDataSource(); //extrahieren der Video Datei, die zuvor zwischengespeichert wurde
|
||||||
|
setAudioExtractorDataSource(); //extrahieren der Audio Datei, die zuvor zwischengespeichert wurde
|
||||||
|
|
||||||
|
//Speicherort der später zusammengeführten Datei
|
||||||
|
String outputFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + "/merged_video.mp4";
|
||||||
|
//MediaMuxer zum Zusammenführen einer Audio- und einer Videodatei
|
||||||
|
MediaMuxer muxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
|
||||||
|
int videoTrack = muxer.addTrack(videoFormat);
|
||||||
|
int audioTrack = muxer.addTrack(audioFormat);
|
||||||
|
muxer.start();
|
||||||
|
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
|
||||||
|
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
|
||||||
|
|
||||||
|
videoExtraction(buffer, videoTrack, bufferInfo, muxer);
|
||||||
|
audioExtraction(buffer, audioTrack, bufferInfo, muxer);
|
||||||
|
|
||||||
|
muxer.stop();
|
||||||
|
muxer.release();
|
||||||
|
|
||||||
|
// Löschen der separaten Video- und Audio-Dateien
|
||||||
|
deleteVideoFile();
|
||||||
|
deleteAudioFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setVideoExtractorDataSource() {
|
||||||
|
try {
|
||||||
|
videoExtractor.setDataSource(videoPath);
|
||||||
|
int videoTrackIndex = getTrackIndex(videoExtractor, "video/");
|
||||||
|
if (videoTrackIndex < 0) {
|
||||||
|
// Video-Track nicht gefunden
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
videoExtractor.selectTrack(videoTrackIndex);
|
||||||
|
videoFormat = videoExtractor.getTrackFormat(videoTrackIndex);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAudioExtractorDataSource() {
|
||||||
|
try {
|
||||||
|
audioExtractor.setDataSource(audioPath);
|
||||||
|
int audioTrackIndex = getTrackIndex(audioExtractor, "audio/");
|
||||||
|
if (audioTrackIndex < 0) {
|
||||||
|
// Audio-Track nicht gefunden
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
audioExtractor.selectTrack(audioTrackIndex);
|
||||||
|
audioFormat = audioExtractor.getTrackFormat(audioTrackIndex);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getTrackIndex(MediaExtractor extractor, String mimeType) {
|
||||||
|
int trackCount = extractor.getTrackCount();
|
||||||
|
for (int i = 0; i < trackCount; i++) {
|
||||||
|
MediaFormat format = extractor.getTrackFormat(i);
|
||||||
|
String trackMimeType = format.getString(MediaFormat.KEY_MIME);
|
||||||
|
if (trackMimeType.startsWith(mimeType)) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void videoExtraction(ByteBuffer buffer, int videoTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
|
||||||
|
videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
|
||||||
|
while (true) {
|
||||||
|
int sampleSize = videoExtractor.readSampleData(buffer, 0);
|
||||||
|
if (sampleSize < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
long presentationTimeUs = videoExtractor.getSampleTime();
|
||||||
|
bufferInfo.offset = 0;
|
||||||
|
bufferInfo.size = sampleSize;
|
||||||
|
bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
|
||||||
|
bufferInfo.presentationTimeUs = presentationTimeUs;
|
||||||
|
muxer.writeSampleData(videoTrack, buffer, bufferInfo);
|
||||||
|
videoExtractor.advance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void audioExtraction(ByteBuffer buffer, int audioTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
|
||||||
|
audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
|
||||||
|
while (true) {
|
||||||
|
int sampleSize = audioExtractor.readSampleData(buffer, 0);
|
||||||
|
if (sampleSize < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
long presentationTimeUs = audioExtractor.getSampleTime();
|
||||||
|
bufferInfo.offset = 0;
|
||||||
|
bufferInfo.size = sampleSize;
|
||||||
|
bufferInfo.flags = 0; // or MediaCodec.BUFFER_FLAG_KEY_FRAME
|
||||||
|
bufferInfo.presentationTimeUs = presentationTimeUs;
|
||||||
|
muxer.writeSampleData(audioTrack, buffer, bufferInfo);
|
||||||
|
audioExtractor.advance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteVideoFile(){
|
||||||
|
File videoFile = new File(videoPath);
|
||||||
|
if (videoFile.exists()) {
|
||||||
|
videoFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteAudioFile(){
|
||||||
|
File audioFile = new File(audioPath);
|
||||||
|
if (audioFile.exists()) {
|
||||||
|
audioFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
import android.media.MediaRecorder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class AudioRecorder implements Runnable {
|
||||||
|
|
||||||
|
private final MediaRecorder audioRecorder;
|
||||||
|
private final String audioPath;
|
||||||
|
public AudioRecorder(MediaRecorder audioRecorder, String audioPath) {
|
||||||
|
this.audioRecorder = audioRecorder;
|
||||||
|
this.audioPath = audioPath;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
audioRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
|
||||||
|
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
||||||
|
audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
|
||||||
|
audioRecorder.setOutputFile(audioPath);
|
||||||
|
|
||||||
|
try {
|
||||||
|
audioRecorder.prepare();
|
||||||
|
audioRecorder.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
public class Beschleunigungssensor {
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
public class Kamera {
|
|
||||||
}
|
|
||||||
@@ -1,56 +1,101 @@
|
|||||||
package com.example.greenwatch;
|
package com.example.greenwatch;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import android.Manifest;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.annotation.SuppressLint;
|
import android.graphics.SurfaceTexture;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.TextureView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.Toast;
|
||||||
import android.widget.TextView;
|
|
||||||
import java.net.SocketException;
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
WifiCommunication communication; //Member WifiCommunication
|
import androidx.core.app.ActivityCompat;
|
||||||
TextView tvReceivedData; //Test Textview um empfangene Daten anzuzeigen -> Gegen finale Textview in der Anzeige austauschen
|
import androidx.core.content.ContextCompat;
|
||||||
//ToDo: Falls finale Textview zum Anzeigen der Nachricht anders heißt, muss das in der Klasse WifiCommunication.java angepasst werden!
|
|
||||||
// Wo anpassen: Receive Thread, Zeile 58
|
|
||||||
private EditText etSendMessage; //Test Edittext um eine Nachricht zu senden -> gegen Nachricht von den Sensoren austauschen
|
|
||||||
private Button btnSend; //Test Sendebutton
|
|
||||||
|
|
||||||
private String sendmsg = "default";
|
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{
|
||||||
|
|
||||||
|
private boolean isRecording = false;
|
||||||
|
private static final int REQUEST_PERMISSION = 200;
|
||||||
|
private Button button;
|
||||||
|
private TextureView textureView;
|
||||||
|
private AlarmRecorder alarmRecorder;
|
||||||
|
|
||||||
@SuppressLint("MissingInflatedId")
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
xmlToJava(); //Funktionsaufruf, wo die Testbuttons etc angelegt sind
|
button = findViewById(R.id.button);
|
||||||
}
|
textureView = findViewById(R.id.textureView);
|
||||||
|
textureView.setSurfaceTextureListener(this);
|
||||||
|
|
||||||
public void xmlToJava(){
|
button.setOnClickListener(new View.OnClickListener() {
|
||||||
tvReceivedData = findViewById(R.id.tvRecTxt); //Test Textview
|
@Override
|
||||||
etSendMessage = findViewById(R.id.sendMessage); //Test Nachricht
|
public void onClick(View v) {
|
||||||
btnSend = findViewById(R.id.btnSend); //Test Sendebefehl
|
if (!isRecording) {
|
||||||
|
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED
|
||||||
|
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED
|
||||||
|
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ActivityCompat.requestPermissions(MainActivity.this,
|
||||||
|
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
|
||||||
|
REQUEST_PERMISSION);
|
||||||
|
} else {
|
||||||
|
isRecording = true;
|
||||||
|
alarmRecorder.startRecording();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
public void onClickSend(View view) throws SocketException{ //ToDo: Sendebefehl mit boolscher Variable lösen --> wo setzen?
|
isRecording = false;
|
||||||
sendmsg = etSendMessage.getText().toString(); //ToDo: hier String von Sensoren übergeben
|
alarmRecorder.stopRecording();
|
||||||
communication.setMessage(sendmsg, true); //übergibt zu sendenden String und setzt die entscheidende Boolsche Variable fürs Senden
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
|
||||||
super.onResume();
|
alarmRecorder = new AlarmRecorder(surfaceTexture);
|
||||||
//Erstellen des Members
|
|
||||||
communication = new WifiCommunication(MainActivity.this, MainActivity.this, 1234);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
|
||||||
super.onPause();
|
// Die Größe der SurfaceTexture hat sich geändert
|
||||||
//schließt socket
|
// Hier können entsprechende Anpassungen vorgenommen werden
|
||||||
communication.stopCommunication();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
||||||
|
// SurfaceTexture wurde zerstört
|
||||||
|
// Hier können entsprechende Bereinigungen durchgeführt werden
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
|
||||||
|
// SurfaceTexture wurde aktualisiert
|
||||||
|
// Hier können entsprechende Aktionen ausgeführt werden, wenn gewünscht
|
||||||
|
}
|
||||||
|
|
||||||
|
private SurfaceTexture getSurfaceTexture() {
|
||||||
|
return textureView.getSurfaceTexture();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
if (requestCode == REQUEST_PERMISSION) {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// Berechtigungen wurden gewährt
|
||||||
|
Toast.makeText(this, "Berechtigungen gewährt", Toast.LENGTH_SHORT).show();
|
||||||
|
isRecording = true;
|
||||||
|
alarmRecorder.startRecording();
|
||||||
|
} else {
|
||||||
|
// Berechtigungen wurden nicht gewährt
|
||||||
|
Toast.makeText(this, "Berechtigungen nicht gewährt", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
public class Mikrofon {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
import android.media.MediaRecorder;
|
||||||
|
import android.view.Surface;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class VideoRecorder implements Runnable{
|
||||||
|
private final MediaRecorder videoRecorder;
|
||||||
|
private final String videoPath;
|
||||||
|
//private SurfaceHolder previewHolder;
|
||||||
|
private Surface surface;
|
||||||
|
|
||||||
|
|
||||||
|
public VideoRecorder(MediaRecorder videoRecorder, String videoPath, Surface surface) {
|
||||||
|
this.videoRecorder = videoRecorder;
|
||||||
|
this.videoPath = videoPath;
|
||||||
|
this.surface = surface;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
|
||||||
|
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
||||||
|
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
|
||||||
|
videoRecorder.setOutputFile(videoPath);
|
||||||
|
videoRecorder.setOrientationHint(90);
|
||||||
|
videoRecorder.setPreviewDisplay(surface);
|
||||||
|
|
||||||
|
try {
|
||||||
|
videoRecorder.prepare();
|
||||||
|
videoRecorder.start();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
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 MainActivity mainActivity;
|
|
||||||
private final InetAddress address;
|
|
||||||
private final int port;
|
|
||||||
private final Context context;
|
|
||||||
private boolean newMsg;
|
|
||||||
private String rxString;
|
|
||||||
private String sendMsg;
|
|
||||||
//private String sendMsg = "default";
|
|
||||||
private final DatagramSocket socket;
|
|
||||||
volatile private boolean running;
|
|
||||||
@SuppressLint("SetTextI18n")
|
|
||||||
public WifiCommunication(MainActivity mainActivity, Context context, int port) {
|
|
||||||
this.mainActivity = mainActivity;
|
|
||||||
this.port = port;
|
|
||||||
this.context = context;
|
|
||||||
try {
|
|
||||||
socket = new DatagramSocket(this.port);
|
|
||||||
socket.setBroadcast(true);
|
|
||||||
address = InetAddress.getByName("255.255.255.255");
|
|
||||||
running = true;
|
|
||||||
new ReceiveThread().start();
|
|
||||||
new SendThread().start();
|
|
||||||
} catch (SocketException | UnknownHostException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
mainActivity.runOnUiThread(() -> Toast.makeText(context,"Communication running", Toast.LENGTH_SHORT).show());
|
|
||||||
}
|
|
||||||
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))
|
|
||||||
mainActivity.runOnUiThread(() -> mainActivity.tvReceivedData.setText(rxString));
|
|
||||||
|
|
||||||
previousRxString = rxString;
|
|
||||||
|
|
||||||
} while (running);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String showMessage(){
|
|
||||||
return rxString;
|
|
||||||
}
|
|
||||||
public void setMessage(String message, boolean newMsg){
|
|
||||||
this.sendMsg=message;
|
|
||||||
this.newMsg = newMsg;
|
|
||||||
}
|
|
||||||
private class SendThread extends Thread {
|
|
||||||
private int tmpCnt = 0;
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
do {
|
|
||||||
|
|
||||||
if(newMsg == true)
|
|
||||||
{
|
|
||||||
newMsg = false;
|
|
||||||
|
|
||||||
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMsg;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,31 +6,26 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<TextureView
|
||||||
android:id="@+id/tvRecTxt"
|
android:id="@+id/textureView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="1sp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="1sp"
|
||||||
android:text="Received Message"
|
android:visibility="visible"
|
||||||
app:layout_constraintTop_toBottomOf="@id/sendMessage"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"/>
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintHorizontal_bias="1.0"
|
||||||
<EditText
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
android:id="@+id/sendMessage"
|
app:layout_constraintTop_toBottomOf="@id/button"
|
||||||
android:layout_width="wrap_content"
|
app:layout_constraintVertical_bias="0.0" />
|
||||||
android:layout_height="50dp"
|
|
||||||
android:ems="10"
|
|
||||||
android:inputType="textPersonName"
|
|
||||||
android:text="Send Message"
|
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"/>
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btnSend"
|
android:id="@+id/button"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Send"
|
android:layout_marginEnd="339dp"
|
||||||
android:onClick="onClickSend"
|
android:text="Record Video"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintStart_toEndOf="@id/sendMessage"/>
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"/>
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Reference in New Issue
Block a user