starting example and improved SerialPortBuilder
|
@ -0,0 +1 @@
|
|||
/build
|
|
@ -0,0 +1,36 @@
|
|||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
|
||||
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.felhr.examplemultipleports"
|
||||
minSdkVersion 17
|
||||
targetSdkVersion 28
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
|
||||
implementation 'com.android.support:appcompat-v7:28.0.0'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,26 @@
|
|||
package com.felhr.examplemultipleports;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.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.getTargetContext();
|
||||
|
||||
assertEquals("com.felhr.examplemultipleports", appContext.getPackageName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest package="com.felhr.examplemultipleports"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
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/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -0,0 +1,172 @@
|
|||
package com.felhr.examplemultipleports;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Set;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
/*
|
||||
* Notifications from UsbService will be received here.
|
||||
*/
|
||||
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
switch (intent.getAction()) {
|
||||
case UsbService.ACTION_USB_PERMISSION_GRANTED: // USB PERMISSION GRANTED
|
||||
Toast.makeText(context, "USB Ready", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_PERMISSION_NOT_GRANTED: // USB PERMISSION NOT GRANTED
|
||||
Toast.makeText(context, "USB Permission not granted", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_NO_USB: // NO USB CONNECTED
|
||||
Toast.makeText(context, "No USB connected", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_DISCONNECTED: // USB DISCONNECTED
|
||||
Toast.makeText(context, "USB disconnected", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_NOT_SUPPORTED: // USB NOT SUPPORTED
|
||||
Toast.makeText(context, "USB device not supported", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
private UsbService usbService;
|
||||
private TextView display1, display2;
|
||||
private EditText editText1, editText2;
|
||||
private Button button1, button2;
|
||||
private MyHandler mHandler;
|
||||
private final ServiceConnection usbConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
|
||||
usbService = ((UsbService.UsbBinder) arg1).getService();
|
||||
usbService.setHandler(mHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName arg0) {
|
||||
usbService = null;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
display1 = findViewById(R.id.textView1);
|
||||
display2 = findViewById(R.id.textView2);
|
||||
|
||||
editText1 = findViewById(R.id.editText1);
|
||||
editText2 = findViewById(R.id.editText2);
|
||||
|
||||
button1 = findViewById(R.id.buttonSend1);
|
||||
button2 = findViewById(R.id.buttonSend2);
|
||||
|
||||
button1.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
//TODO!!
|
||||
}
|
||||
});
|
||||
|
||||
button2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
//TODO!!
|
||||
}
|
||||
});
|
||||
|
||||
mHandler = new MyHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
setFilters(); // Start listening notifications from UsbService
|
||||
startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(mUsbReceiver);
|
||||
unbindService(usbConnection);
|
||||
}
|
||||
|
||||
private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) {
|
||||
if (!UsbService.SERVICE_CONNECTED) {
|
||||
Intent startService = new Intent(this, service);
|
||||
if (extras != null && !extras.isEmpty()) {
|
||||
Set<String> keys = extras.keySet();
|
||||
for (String key : keys) {
|
||||
String extra = extras.getString(key);
|
||||
startService.putExtra(key, extra);
|
||||
}
|
||||
}
|
||||
startService(startService);
|
||||
}
|
||||
Intent bindingIntent = new Intent(this, service);
|
||||
bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
|
||||
private void setFilters() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED);
|
||||
filter.addAction(UsbService.ACTION_NO_USB);
|
||||
filter.addAction(UsbService.ACTION_USB_DISCONNECTED);
|
||||
filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED);
|
||||
filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED);
|
||||
registerReceiver(mUsbReceiver, filter);
|
||||
}
|
||||
|
||||
/*
|
||||
* This handler will be passed to UsbService. Data received from serial port is displayed through this handler
|
||||
*/
|
||||
private static class MyHandler extends Handler {
|
||||
private final WeakReference<MainActivity> mActivity;
|
||||
|
||||
public MyHandler(MainActivity activity) {
|
||||
mActivity = new WeakReference<>(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case UsbService.MESSAGE_FROM_SERIAL_PORT:
|
||||
String data = (String) msg.obj;
|
||||
//mActivity.get().display.append(data);
|
||||
break;
|
||||
case UsbService.CTS_CHANGE:
|
||||
Toast.makeText(mActivity.get(), "CTS_CHANGE",Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
case UsbService.DSR_CHANGE:
|
||||
Toast.makeText(mActivity.get(), "DSR_CHANGE",Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
case UsbService.SYNC_READ:
|
||||
String buffer = (String) msg.obj;
|
||||
//mActivity.get().display.append(buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.felhr.examplemultipleports;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Binder;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Created by Felipe Herranz(felhr85@gmail.com) on 09/11/2018.
|
||||
*/
|
||||
public class UsbService extends Service {
|
||||
|
||||
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
||||
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
||||
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
||||
public static final String ACTION_USB_NOT_SUPPORTED = "com.felhr.usbservice.USB_NOT_SUPPORTED";
|
||||
public static final String ACTION_NO_USB = "com.felhr.usbservice.NO_USB";
|
||||
public static final String ACTION_USB_PERMISSION_GRANTED = "com.felhr.usbservice.USB_PERMISSION_GRANTED";
|
||||
public static final String ACTION_USB_PERMISSION_NOT_GRANTED = "com.felhr.usbservice.USB_PERMISSION_NOT_GRANTED";
|
||||
public static final String ACTION_USB_DISCONNECTED = "com.felhr.usbservice.USB_DISCONNECTED";
|
||||
public static final String ACTION_CDC_DRIVER_NOT_WORKING = "com.felhr.connectivityservices.ACTION_CDC_DRIVER_NOT_WORKING";
|
||||
public static final String ACTION_USB_DEVICE_NOT_WORKING = "com.felhr.connectivityservices.ACTION_USB_DEVICE_NOT_WORKING";
|
||||
public static final int MESSAGE_FROM_SERIAL_PORT = 0;
|
||||
public static final int CTS_CHANGE = 1;
|
||||
public static final int DSR_CHANGE = 2;
|
||||
public static final int SYNC_READ = 3;
|
||||
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
|
||||
private static final int BAUD_RATE = 9600; // BaudRate. Change this value if you need
|
||||
public static boolean SERVICE_CONNECTED = false;
|
||||
|
||||
private IBinder binder = new UsbBinder();
|
||||
|
||||
private Handler mHandler;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return binder;
|
||||
}
|
||||
|
||||
public void setHandler(Handler mHandler) {
|
||||
this.mHandler = mHandler;
|
||||
}
|
||||
|
||||
public class UsbBinder extends Binder {
|
||||
public UsbService getService() {
|
||||
return UsbService.this;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<vector xmlns:aapt="http://schemas.android.com/aapt"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillType="evenOdd"
|
||||
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="78.5885"
|
||||
android:endY="90.9159"
|
||||
android:startX="48.7653"
|
||||
android:startY="61.0927"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0"/>
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0"/>
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1"/>
|
||||
</vector>
|
|
@ -0,0 +1,171 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillColor="#008577"
|
||||
android:pathData="M0,0h108v108h-108z"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
</vector>
|
|
@ -0,0 +1,94 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.felhr.examplemultipleports.MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textViewTitle1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text="Multiple Serial Ports"
|
||||
android:textAppearance="?android:attr/textAppearanceLarge"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dp"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="34dp"
|
||||
android:background="#000000"
|
||||
android:textColor="#FFFFFF"
|
||||
android:scrollbars="vertical"
|
||||
android:text="COM1: "
|
||||
android:gravity="bottom" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dp"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="10dp"
|
||||
android:background="#000000"
|
||||
android:layout_below="@+id/textView1"
|
||||
android:textColor="#FFFFFF"
|
||||
android:text="COM2: "
|
||||
android:scrollbars="vertical"
|
||||
android:gravity="bottom" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editText1"
|
||||
android:layout_width="227dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignLeft="@+id/textView1"
|
||||
android:layout_alignRight="@+id/textView1"
|
||||
android:layout_below="@+id/textView2"
|
||||
android:layout_marginRight="184dp"
|
||||
android:layout_marginTop="28dp"
|
||||
android:ems="10">
|
||||
</EditText>
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonSend1"
|
||||
style="?borderlessButtonStyle"
|
||||
android:layout_width="178dp"
|
||||
android:layout_height="48dp"
|
||||
android:layout_alignLeft="@+id/editText1"
|
||||
android:layout_alignRight="@+id/editText1"
|
||||
android:layout_below="@+id/editText1"
|
||||
android:layout_marginLeft="227dp"
|
||||
android:layout_marginRight="-175dp"
|
||||
android:layout_marginTop="-48dp"
|
||||
android:textSize="10sp"
|
||||
android:background="@color/colorAccent"
|
||||
android:text="Send through COM1"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editText2"
|
||||
android:layout_width="227dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignLeft="@+id/textView1"
|
||||
android:layout_alignRight="@+id/textView1"
|
||||
android:layout_below="@+id/editText1"
|
||||
android:layout_marginRight="184dp"
|
||||
android:layout_marginTop="28dp"
|
||||
android:ems="10">
|
||||
</EditText>
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonSend2"
|
||||
style="?borderlessButtonStyle"
|
||||
android:layout_width="178dp"
|
||||
android:layout_height="48dp"
|
||||
android:layout_alignLeft="@+id/editText2"
|
||||
android:layout_alignRight="@+id/editText2"
|
||||
android:layout_below="@+id/editText2"
|
||||
android:layout_marginLeft="227dp"
|
||||
android:layout_marginRight="-175dp"
|
||||
android:layout_marginTop="-48dp"
|
||||
android:textSize="10sp"
|
||||
android:background="@color/colorAccent"
|
||||
android:text="Send through COM2"/>
|
||||
</RelativeLayout>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background"/>
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background"/>
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
Po Szerokość: | Wysokość: | Rozmiar: 2.9 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 4.8 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 2.0 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 2.7 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 4.4 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 6.7 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 6.2 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 10 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 8.9 KiB |
Po Szerokość: | Wysokość: | Rozmiar: 15 KiB |
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#008577</color>
|
||||
<color name="colorPrimaryDark">#00574B</color>
|
||||
<color name="colorAccent">#D81B60</color>
|
||||
</resources>
|
|
@ -0,0 +1,3 @@
|
|||
<resources>
|
||||
<string name="app_name">Example multiple ports</string>
|
||||
</resources>
|
|
@ -0,0 +1,11 @@
|
|||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,17 @@
|
|||
package com.felhr.examplemultipleports;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
include ':usbserial', ':example', ':examplesync', ':examplestreams'
|
||||
include ':usbserial', ':example', ':examplesync', ':examplestreams', ':examplemultipleports'
|
|
@ -7,8 +7,8 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Handler;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class SerialPortBuilder {
|
|||
|
||||
private static SerialPortBuilder SerialPortBuilder;
|
||||
|
||||
private List<UsbDevice> devices;
|
||||
private List<UsbDeviceStatus> devices;
|
||||
private List<UsbSerialDevice> serialDevices = new ArrayList<>();
|
||||
|
||||
private int index = 0;
|
||||
|
@ -53,14 +53,23 @@ public class SerialPortBuilder {
|
|||
|
||||
public List<UsbDevice> getPossibleSerialPorts(){
|
||||
HashMap<String, UsbDevice> allDevices = usbManager.getDeviceList();
|
||||
|
||||
return Stream.of(allDevices.values()).filter(
|
||||
p -> UsbSerialDevice.isSupported(p))
|
||||
return Stream.of(allDevices.values())
|
||||
.filter(UsbSerialDevice::isSupported)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public boolean getSerialPorts(Context context){
|
||||
devices = getPossibleSerialPorts();
|
||||
|
||||
if(devices == null) {
|
||||
devices = Stream.of(getPossibleSerialPorts())
|
||||
.map(UsbDeviceStatus::new)
|
||||
.toList();
|
||||
}else{
|
||||
devices.addAll(Stream.of(getPossibleSerialPorts())
|
||||
.map(UsbDeviceStatus::new)
|
||||
.filter(p -> !devices.contains(p))
|
||||
.toList());
|
||||
}
|
||||
|
||||
if(devices == null || devices.size() == 0)
|
||||
return false;
|
||||
|
@ -84,9 +93,18 @@ public class SerialPortBuilder {
|
|||
return getSerialPorts(context);
|
||||
}
|
||||
|
||||
private void requestPermission(Context context){
|
||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||
usbManager.requestPermission(devices.get(index), mPendingIntent);
|
||||
private boolean requestPermission(Context context){
|
||||
if(!devices.get(index).open) {
|
||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||
usbManager.requestPermission(devices.get(index).usbDevice, mPendingIntent);
|
||||
return true;
|
||||
}else{
|
||||
index++;
|
||||
if(index <= devices.size()-1)
|
||||
return requestPermission(context);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void initReceiver(Context context){
|
||||
|
@ -95,12 +113,16 @@ public class SerialPortBuilder {
|
|||
context.registerReceiver(usbReceiver, filter);
|
||||
}
|
||||
|
||||
private void createAllPorts(UsbDevice usbDevice){
|
||||
int interfaceCount = usbDevice.getInterfaceCount();
|
||||
private void createAllPorts(UsbDeviceStatus usbDeviceStatus){
|
||||
int interfaceCount = usbDeviceStatus.usbDevice.getInterfaceCount();
|
||||
for(int i=0;i<=interfaceCount-1;i++) {
|
||||
//TODO: Maybe opening the same device more than once causes troubles!!!
|
||||
UsbSerialDevice usbSerialDevice = UsbSerialDevice.createUsbSerialDevice(usbDevice,
|
||||
usbManager.openDevice(usbDevice),
|
||||
if(usbDeviceStatus.usbDeviceConnection == null) {
|
||||
usbDeviceStatus.usbDeviceConnection = usbManager.openDevice(usbDeviceStatus.usbDevice);
|
||||
}
|
||||
|
||||
UsbSerialDevice usbSerialDevice = UsbSerialDevice.createUsbSerialDevice(
|
||||
usbDeviceStatus.usbDevice,
|
||||
usbDeviceStatus.usbDeviceConnection,
|
||||
i);
|
||||
|
||||
serialDevices.add(usbSerialDevice);
|
||||
|
@ -113,6 +135,7 @@ public class SerialPortBuilder {
|
|||
if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
|
||||
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
|
||||
if (granted) {
|
||||
devices.get(index).open = true;
|
||||
createAllPorts(devices.get(index));
|
||||
index++;
|
||||
if(index < devices.size()){
|
||||
|
@ -167,4 +190,22 @@ public class SerialPortBuilder {
|
|||
serialPortCallback.onSerialPortsDetected(serialDevices, true);
|
||||
}
|
||||
}
|
||||
|
||||
private class UsbDeviceStatus {
|
||||
public UsbDevice usbDevice;
|
||||
public UsbDeviceConnection usbDeviceConnection;
|
||||
public boolean open;
|
||||
|
||||
public UsbDeviceStatus(UsbDevice usbDevice) {
|
||||
this.usbDevice = usbDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
UsbDevice objDevice = (UsbDevice) obj;
|
||||
return (objDevice.getVendorId() == usbDevice.getVendorId())
|
||||
&& (objDevice.getProductId() == usbDevice.getProductId());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|