Skip to content

Commit

Permalink
Trying to use TCP-based protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
vladd11 committed Mar 14, 2022
0 parents commit c8d7dcb
Show file tree
Hide file tree
Showing 42 changed files with 1,274 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties

#amplify-do-not-edit-begin
amplify/\#current-cloud-backend
amplify/.config/local-*
amplify/logs
amplify/mock-data
amplify/backend/amplify-meta.json
amplify/backend/.temp
build/
dist/
node_modules/
aws-exports.js
awsconfiguration.json
amplifyconfiguration.json
amplifyconfiguration.dart
amplify-build-config.json
amplify-gradle-config.json
amplifytools.xcconfig
.secret-*
**.sample
#amplify-do-not-edit-end
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
57 changes: 57 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.vladd11.app.robot"
minSdk 28
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
debug {
buildConfigField "boolean", "BLUETOOTH", "true"
}
release {
buildConfigField "boolean", "BLUETOOTH", "true"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/io.netty.versions.properties'
}
}

dependencies {
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.github.mik3y:usb-serial-for-android:3.4.3'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.github.dinocore1:ubjson:0.1.8'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'

implementation 'com.github.harry1453:android-bluetooth-serial:v1.1'

// RxJava is also required.
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.vladd11.app.robot;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.vladd11.app.robot", appContext.getPackageName());
}
}
43 changes: 43 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vladd11.app.robot">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />

<application
android:networkSecurityConfig="@xml/net"
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Robot">
<service android:name="org.eclipse.paho.android.service.MqttService">
</service>

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>

</manifest>
19 changes: 19 additions & 0 deletions app/src/main/java/com/vladd11/app/robot/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vladd11.app.robot;

public enum Action {
LEFT((byte) 0x0),
RIGHT((byte) 0x1),
FORWARD((byte) 0x2),
BACK((byte) 0x3),
STOP((byte) 0x4);

private final byte action;

Action(byte action) {
this.action = action;
}

public byte getAction() {
return action;
}
}
114 changes: 114 additions & 0 deletions app/src/main/java/com/vladd11/app/robot/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.vladd11.app.robot;


import android.location.Location;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;

public class ConnectionManager {
private static final String TAG = "ConnectionManager";
private Socket socket;
private OutputStream writer;

public void connect(OnConnectedListener onConnected) {
new Thread(() -> {
try {
socket = new Socket(InetAddress.getByName(Secrets.SERVER_ADDRESS), Secrets.SERVER_PORT);
writer = socket.getOutputStream();
onConnected.call();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}

public void getLocationPoints(OnLocationPointsAvailableListener onResult) throws IOException {
writer.write("r{\"c\": [50.242092, 53.211953],\"e\":[50.240560, 53.213407]}".getBytes(StandardCharsets.UTF_8));
writer.flush();

ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = socket.getInputStream().read(buffer)) != -1; ) {
result.write(buffer, 0, length);
if (buffer[buffer.length - 1] == 0) {
break;
}
}

final List<List<Double>> rawCoordinates = new Gson().fromJson(result.toString("UTF-8"),
TypeToken.getParameterized(List.class, List.class).getType());
final Queue<Location> locationQueue = new ArrayDeque<>();
for (List<Double> coordinates : rawCoordinates) {
final Location location = new Location("");
location.setLatitude(coordinates.get(1));
location.setLongitude(coordinates.get(0));

locationQueue.add(location);
}
onResult.call(locationQueue);
}

public void receiveImageRequests(OnImageRequestReceivedListener listener) {
new Thread(() -> {
try {
while (true) {
while (socket.getInputStream().read() != 128) {

}

listener.call(buffer -> {
final Thread thread = new Thread(() -> {
try {
writer.write(106);
writer.flush();

for (int i = 0; i < buffer.remaining(); i++) {
writer.write(buffer.get());
}
writer.flush();

writer.write(106);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
});
thread.start();
thread.join();
});
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}

public interface OnLocationPointsAvailableListener {
void call(Queue<Location> result);
}

public interface OnImageRequestReceivedListener {
void call(ImageReceivedListener listener);
}

public interface ImageReceivedListener {
void received(ByteBuffer buffer) throws InterruptedException;
}

public interface OnConnectedListener {
void call();
}
}
Loading

0 comments on commit c8d7dcb

Please sign in to comment.