added usbserial lib
This commit is contained in:
		@ -4,7 +4,7 @@ android {
 | 
			
		||||
    compileSdkVersion 30
 | 
			
		||||
    defaultConfig {
 | 
			
		||||
        applicationId "hendrikschutter.com.externgnss"
 | 
			
		||||
        minSdkVersion 14
 | 
			
		||||
        minSdkVersion 17
 | 
			
		||||
        targetSdkVersion 30
 | 
			
		||||
        versionCode 1
 | 
			
		||||
        versionName "1.0"
 | 
			
		||||
@ -27,4 +27,5 @@ dependencies {
 | 
			
		||||
    testImplementation 'junit:junit:4.12'
 | 
			
		||||
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
 | 
			
		||||
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
 | 
			
		||||
    implementation 'com.github.mik3y:usb-serial-for-android:3.4.4'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,65 @@
 | 
			
		||||
package hendrikschutter.com.externgnss;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.hardware.usb.UsbDeviceConnection;
 | 
			
		||||
import android.hardware.usb.UsbManager;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import android.widget.EditText;
 | 
			
		||||
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
 | 
			
		||||
import com.hoho.android.usbserial.driver.UsbSerialProber;
 | 
			
		||||
import com.hoho.android.usbserial.util.SerialInputOutputManager;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class ExternGNSS implements SerialInputOutputManager.Listener {
 | 
			
		||||
    private MainActivity uiactivity;
 | 
			
		||||
    ExternGNSS(UsbManager manager, MainActivity mainactivity) {
 | 
			
		||||
        Log.i("HAG", "init extern gnss");
 | 
			
		||||
    this.uiactivity = mainactivity;
 | 
			
		||||
        // Find all available drivers from attached devices.
 | 
			
		||||
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
 | 
			
		||||
        if (availableDrivers.isEmpty()) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Open a connection to the first available driver.
 | 
			
		||||
        UsbSerialDriver driver = availableDrivers.get(0);
 | 
			
		||||
        UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
 | 
			
		||||
        if (connection == null) {
 | 
			
		||||
            // add UsbManager.requestPermission(driver.getDevice(), ..) handling here
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
 | 
			
		||||
        try {
 | 
			
		||||
            port.open(connection);
 | 
			
		||||
            port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
 | 
			
		||||
 | 
			
		||||
            SerialInputOutputManager usbIoManager = new SerialInputOutputManager(port, this);
 | 
			
		||||
            usbIoManager.start();
 | 
			
		||||
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onNewData(byte[] data) {
 | 
			
		||||
        //runOnUiThread(() -> { textView.append(new String(data)); });
 | 
			
		||||
        Log.i("HAG", new String(data));
 | 
			
		||||
        if( this.uiactivity != null){
 | 
			
		||||
            this.uiactivity.setExternGnss(new String(data));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onRunError(Exception e) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -3,6 +3,7 @@ package hendrikschutter.com.externgnss;
 | 
			
		||||
import android.Manifest;
 | 
			
		||||
import android.annotation.SuppressLint;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.hardware.usb.UsbManager;
 | 
			
		||||
import android.location.LocationListener;
 | 
			
		||||
import android.location.LocationManager;
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity;
 | 
			
		||||
@ -11,10 +12,8 @@ import android.util.Log;
 | 
			
		||||
import android.widget.EditText;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import pub.devrel.easypermissions.AfterPermissionGranted;
 | 
			
		||||
import pub.devrel.easypermissions.EasyPermissions;
 | 
			
		||||
 | 
			
		||||
import static java.lang.Thread.sleep;
 | 
			
		||||
 | 
			
		||||
public class MainActivity extends AppCompatActivity {
 | 
			
		||||
@ -30,8 +29,14 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
        setContentView(R.layout.activity_main);
 | 
			
		||||
        startLocationInit();
 | 
			
		||||
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
 | 
			
		||||
        Log.i("HAG", "init app");
 | 
			
		||||
        ExternGNSS tmp = new ExternGNSS(manager, this);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // From https://github.com/googlesamples/easypermissions
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
 | 
			
		||||
@ -46,20 +51,6 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
            // Already have permission, do the thing
 | 
			
		||||
            Toast.makeText(cntxToastInternGNSSUpdate, "Permission granted!", Toast.LENGTH_SHORT).show();
 | 
			
		||||
 | 
			
		||||
            // Get the output UI
 | 
			
		||||
           // output = (TextView) findViewById(R.id.interngnssLongTextView);
 | 
			
		||||
 | 
			
		||||
            // Test print
 | 
			
		||||
            ///printlnOnView(output, "Running!");
 | 
			
		||||
 | 
			
		||||
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 | 
			
		||||
            //List<String> providers = locationManager.getAllProviders();
 | 
			
		||||
 | 
			
		||||
            //for (String temp : providers) {
 | 
			
		||||
                // System.out.println(temp);
 | 
			
		||||
                //printProvider(temp);
 | 
			
		||||
            //}
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            // Do not have permissions, request them now
 | 
			
		||||
            EasyPermissions.requestPermissions(this, "We require location access!!!",
 | 
			
		||||
@ -67,18 +58,6 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
    private void printProvider(String provider) {
 | 
			
		||||
        LocationProvider info = locationManager.getProvider(provider);
 | 
			
		||||
        output.append("Name: " + info.getName() + "\n");
 | 
			
		||||
        output.append("Accuracy: " + info.getAccuracy() + "\n");
 | 
			
		||||
        output.append("PowerReq.: " + info.getPowerRequirement() + "\n");
 | 
			
		||||
        output.append("CellIDReq.: " + info.requiresCell() + "\n");
 | 
			
		||||
        output.append("NetworkReq.: " + info.requiresNetwork() + "\n");
 | 
			
		||||
        output.append("SatReq.: " + info.requiresSatellite() + "\n\n");
 | 
			
		||||
    }
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
    private void setEditTextLabel(final EditText v, final String str) {
 | 
			
		||||
        Runnable myRun = new Runnable() {
 | 
			
		||||
            public void run() {
 | 
			
		||||
@ -87,46 +66,24 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        };
 | 
			
		||||
        runOnUiThread(myRun);
 | 
			
		||||
    }
 | 
			
		||||
  /*
 | 
			
		||||
    private void printOnView(final TextView v, final String str) {
 | 
			
		||||
 | 
			
		||||
    public void setExternGnss( final String str) {
 | 
			
		||||
        Runnable myRun = new Runnable() {
 | 
			
		||||
            public void run() {
 | 
			
		||||
                final TextView v =  (TextView) findViewById(R.id.externGNSSTextView);
 | 
			
		||||
                v.append(str);
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
        runOnUiThread(myRun);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void addToString(Location loc) {
 | 
			
		||||
        //Verwendet die Variablen centerLat & centerLng
 | 
			
		||||
        String time = loc.getTime() +"";
 | 
			
		||||
        String latStr = Double.toString(loc.getLatitude());
 | 
			
		||||
        String lngStr = Double.toString(loc.getLongitude());
 | 
			
		||||
        String acc = loc.getAccuracy() + "";
 | 
			
		||||
        String numOfSat = loc.getExtras().getInt("satellites") +"";
 | 
			
		||||
        String satStr = "";
 | 
			
		||||
        String distStr = "";
 | 
			
		||||
        // Bestimme die Distanz (Fix <-> Center)
 | 
			
		||||
        Location locationA = new Location("Center");
 | 
			
		||||
        locationA.setLatitude(Double.valueOf(centerLat));
 | 
			
		||||
        locationA.setLongitude(Double.valueOf(centerLng));
 | 
			
		||||
        Location locationB = new Location("Fix");
 | 
			
		||||
        locationB.setLatitude(Double.valueOf(latStr));
 | 
			
		||||
        locationB.setLongitude(Double.valueOf(lngStr));
 | 
			
		||||
        distStr = locationA.distanceTo(locationB) + "";
 | 
			
		||||
        logString = logString + time + ";"; logString = logString + numOfSat + ";"; logString = logString + distStr +
 | 
			
		||||
                ";"; logString = logString + acc + ";"; logString = logString + latStr + ";"; logString = logString + lngStr
 | 
			
		||||
                + ";"; logString = logString + centerLat + ";"; logString = logString + centerLng + ";"; logString =
 | 
			
		||||
                logString + satStr + ";"; logString = logString + "\n";
 | 
			
		||||
    }
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
    @SuppressLint("MissingPermission")
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onResume() {
 | 
			
		||||
        super.onResume();
 | 
			
		||||
        if (!EasyPermissions.hasPermissions(this, perms)) {
 | 
			
		||||
            // Dummy thread ...
 | 
			
		||||
            /*
 | 
			
		||||
            new Thread(new Runnable() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void run() {
 | 
			
		||||
@ -140,14 +97,18 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }).start();
 | 
			
		||||
             */
 | 
			
		||||
        } else {
 | 
			
		||||
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
 | 
			
		||||
            if(locationManager != null){
 | 
			
		||||
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Worker-Thread
 | 
			
		||||
            new Thread(
 | 
			
		||||
                    new Runnable() {
 | 
			
		||||
                        @Override
 | 
			
		||||
                        public void run() {
 | 
			
		||||
                            while (doRun) {
 | 
			
		||||
                            while (doRun && (locationManager != null)) {
 | 
			
		||||
                                if (locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
 | 
			
		||||
                                    setEditTextLabel((EditText) findViewById(R.id.interngnssLatNumber), String.format( "%.6f", locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()));
 | 
			
		||||
                                    setEditTextLabel((EditText) findViewById(R.id.interngnssLongNumber), String.format( "%.6f", locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()));
 | 
			
		||||
 | 
			
		||||
@ -151,4 +151,13 @@
 | 
			
		||||
        app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
        app:layout_constraintTop_toBottomOf="@+id/interngnssAccuracyNumber" />
 | 
			
		||||
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:id="@+id/externGNSSTextView"
 | 
			
		||||
        android:layout_width="360dp"
 | 
			
		||||
        android:layout_height="347dp"
 | 
			
		||||
        android:layout_marginStart="16dp"
 | 
			
		||||
        android:layout_marginTop="300dp"
 | 
			
		||||
        app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
        app:layout_constraintTop_toTopOf="parent" />
 | 
			
		||||
 | 
			
		||||
</androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
@ -17,6 +17,7 @@ allprojects {
 | 
			
		||||
    repositories {
 | 
			
		||||
        google()
 | 
			
		||||
        mavenCentral()
 | 
			
		||||
        maven { url 'https://jitpack.io' }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user