kopia lustrzana https://github.com/felHR85/UsbSerial
added disconnectDevice method and finishing example
rodzic
274cf71f62
commit
8846e3d9ce
|
@ -16,6 +16,12 @@ android {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
encoding "UTF-8"
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
|
@ -33,4 +39,7 @@ dependencies {
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||||
|
|
||||||
|
implementation project(':usbserial')
|
||||||
|
implementation 'com.annimon:stream:1.2.1'
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,25 @@
|
||||||
package com.felhr.examplemultipleports;
|
package com.felhr.examplemultipleports;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.hardware.usb.UsbDevice;
|
||||||
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.annimon.stream.Stream;
|
||||||
|
import com.felhr.usbserial.SerialPortBuilder;
|
||||||
|
import com.felhr.usbserial.SerialPortCallback;
|
||||||
|
import com.felhr.usbserial.UsbSerialDevice;
|
||||||
|
import com.felhr.usbserial.UsbSerialInterface;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Felipe Herranz(felhr85@gmail.com) on 09/11/2018.
|
* Created by Felipe Herranz(felhr85@gmail.com) on 09/11/2018.
|
||||||
|
@ -30,10 +44,75 @@ public class UsbService extends Service {
|
||||||
private static final int BAUD_RATE = 9600; // BaudRate. Change this value if you need
|
private static final int BAUD_RATE = 9600; // BaudRate. Change this value if you need
|
||||||
public static boolean SERVICE_CONNECTED = false;
|
public static boolean SERVICE_CONNECTED = false;
|
||||||
|
|
||||||
|
private List<UsbSerialDevice> serialPorts;
|
||||||
|
|
||||||
|
private Context context;
|
||||||
|
private UsbManager usbManager;
|
||||||
|
private SerialPortBuilder builder;
|
||||||
|
|
||||||
private IBinder binder = new UsbBinder();
|
private IBinder binder = new UsbBinder();
|
||||||
|
|
||||||
private Handler mHandler;
|
private Handler mHandler;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Different notifications from OS will be received here (USB attached, detached, permission responses...)
|
||||||
|
* About BroadcastReceiver: http://developer.android.com/reference/android/content/BroadcastReceiver.html
|
||||||
|
*/
|
||||||
|
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context arg0, Intent arg1) {
|
||||||
|
if (arg1.getAction().equals(ACTION_USB_ATTACHED)) {
|
||||||
|
boolean ret = builder.openSerialPorts(context, BAUD_RATE,
|
||||||
|
UsbSerialInterface.DATA_BITS_8,
|
||||||
|
UsbSerialInterface.STOP_BITS_1,
|
||||||
|
UsbSerialInterface.PARITY_NONE,
|
||||||
|
UsbSerialInterface.FLOW_CONTROL_OFF);
|
||||||
|
if(ret)
|
||||||
|
Toast.makeText(context, "Couldnt open the device", Toast.LENGTH_SHORT).show();
|
||||||
|
} else if (arg1.getAction().equals(ACTION_USB_DETACHED)) {
|
||||||
|
UsbDevice usbDevice = arg1.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||||
|
|
||||||
|
List<UsbSerialDevice> filteredDevice = Stream.of(serialPorts)
|
||||||
|
.filter(p -> usbDevice.getDeviceId() == p.getDeviceId())
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if(filteredDevice.size() == 1){
|
||||||
|
UsbSerialDevice disconnectedDevice = filteredDevice.get(0);
|
||||||
|
disconnectedDevice.syncClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(ACTION_USB_DISCONNECTED);
|
||||||
|
arg0.sendBroadcast(intent);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final SerialPortCallback serialPortCallback = new SerialPortCallback() {
|
||||||
|
@Override
|
||||||
|
public void onSerialPortsDetected(List<UsbSerialDevice> serialPorts, boolean opened) {
|
||||||
|
//TODO!!
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
this.context = this;
|
||||||
|
UsbService.SERVICE_CONNECTED = true;
|
||||||
|
setFilter();
|
||||||
|
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
|
builder = SerialPortBuilder.createSerialPortBuilder(serialPortCallback);
|
||||||
|
|
||||||
|
boolean ret = builder.openSerialPorts(context, BAUD_RATE,
|
||||||
|
UsbSerialInterface.DATA_BITS_8,
|
||||||
|
UsbSerialInterface.STOP_BITS_1,
|
||||||
|
UsbSerialInterface.PARITY_NONE,
|
||||||
|
UsbSerialInterface.FLOW_CONTROL_OFF);
|
||||||
|
|
||||||
|
if(!ret)
|
||||||
|
Toast.makeText(context, "No Usb serial ports available", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
|
@ -44,6 +123,15 @@ public class UsbService extends Service {
|
||||||
this.mHandler = mHandler;
|
this.mHandler = mHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void setFilter() {
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(ACTION_USB_PERMISSION);
|
||||||
|
filter.addAction(ACTION_USB_DETACHED);
|
||||||
|
filter.addAction(ACTION_USB_ATTACHED);
|
||||||
|
registerReceiver(usbReceiver, filter);
|
||||||
|
}
|
||||||
|
|
||||||
public class UsbBinder extends Binder {
|
public class UsbBinder extends Binder {
|
||||||
public UsbService getService() {
|
public UsbService getService() {
|
||||||
return UsbService.this;
|
return UsbService.this;
|
||||||
|
|
|
@ -93,6 +93,17 @@ public class SerialPortBuilder {
|
||||||
return getSerialPorts(context);
|
return getSerialPorts(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void disconnectDevice(UsbDevice usbDevice){
|
||||||
|
List<UsbSerialDevice> filteredDevice = Stream.of(serialDevices)
|
||||||
|
.filter(p -> usbDevice.getDeviceId() == p.getDeviceId())
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if(filteredDevice.size() == 1){
|
||||||
|
UsbSerialDevice disconnectedDevice = filteredDevice.get(0);
|
||||||
|
disconnectedDevice.syncClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean requestPermission(Context context){
|
private boolean requestPermission(Context context){
|
||||||
if(!devices.get(index).open) {
|
if(!devices.get(index).open) {
|
||||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
|
|
|
@ -3,7 +3,6 @@ package com.felhr.usbserial;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import com.annimon.stream.Stream;
|
import com.annimon.stream.Stream;
|
||||||
import com.felhr.deviceids.CH34xIds;
|
import com.felhr.deviceids.CH34xIds;
|
||||||
|
@ -247,6 +246,18 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
return outputStream;
|
return outputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getVid(){
|
||||||
|
return device.getVendorId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPid(){
|
||||||
|
return device.getProductId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDeviceId(){
|
||||||
|
return device.getDeviceId();
|
||||||
|
}
|
||||||
|
|
||||||
//Debug options
|
//Debug options
|
||||||
public void debug(boolean value)
|
public void debug(boolean value)
|
||||||
{
|
{
|
||||||
|
|
Ładowanie…
Reference in New Issue