completed the device listener
This commit is contained in:
parent
b4e2309243
commit
a2180dfaea
@ -14,6 +14,7 @@ import android.hardware.usb.UsbInterface;
|
|||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
import android.webkit.CookieManager;
|
import android.webkit.CookieManager;
|
||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
@ -26,6 +27,9 @@ import androidx.appcompat.content.res.AppCompatResources;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import android.os.Process;
|
||||||
|
import android.widget.Button;
|
||||||
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@ -40,19 +44,49 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
|
String action = intent.getAction();
|
||||||
synchronized (this) {
|
|
||||||
|
if (ACTION_USB_PERMISSION.equals(action)) {
|
||||||
|
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||||
|
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
|
||||||
|
if (granted && device != null) {
|
||||||
|
Log.d("USB", "Permission granted for: " + device);
|
||||||
|
usbDevice = device;
|
||||||
|
|
||||||
|
if (!setupPrinterConnection(device)) {
|
||||||
|
Log.d("USB", "Permission ok, but no interface/endpoint found.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.d("USB", "Permission denied for device: " + device);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
|
||||||
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||||
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
|
||||||
if (device != null) {
|
if (device != null) {
|
||||||
setupPrinterConnection(device);
|
Log.d("USB", "Device attached: " + device);
|
||||||
}
|
usbDevice = device;
|
||||||
|
|
||||||
|
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
||||||
|
context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
||||||
|
);
|
||||||
|
usbManager.requestPermission(device, permissionIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
|
||||||
|
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||||
|
if (device != null && device.equals(usbDevice)) {
|
||||||
|
Log.d("USB", "Device detached: " + device);
|
||||||
|
usbDevice = null;
|
||||||
|
usbInterface = null;
|
||||||
|
usbEndpoint = null;
|
||||||
|
usbDeviceConnection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// @Override
|
// @Override
|
||||||
// protected void onCreate(Bundle savedInstanceState) {
|
// protected void onCreate(Bundle savedInstanceState) {
|
||||||
// super.onCreate(savedInstanceState);
|
// super.onCreate(savedInstanceState);
|
||||||
@ -77,8 +111,11 @@ protected void onCreate(Bundle savedInstanceState) {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
|
filter.addAction(ACTION_USB_PERMISSION);
|
||||||
|
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
|
||||||
|
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
|
||||||
|
registerReceiver(usbReceiver, filter, RECEIVER_NOT_EXPORTED);
|
||||||
|
|
||||||
WebView webView = new WebView(this);
|
WebView webView = new WebView(this);
|
||||||
setContentView(webView);
|
setContentView(webView);
|
||||||
@ -91,81 +128,142 @@ protected void onCreate(Bundle savedInstanceState) {
|
|||||||
CookieManager.getInstance().setAcceptCookie(true);
|
CookieManager.getInstance().setAcceptCookie(true);
|
||||||
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
|
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
|
||||||
|
|
||||||
//webView.loadUrl("http://10.23.107.10:8000/kioskvideo");
|
webView.loadUrl("http://10.23.107.10:8000/kioskvideo");
|
||||||
|
|
||||||
webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
// webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
||||||
//webView.loadUrl("file:///android_asset/index.html");
|
//webView.loadUrl("file:///android_asset/index.html");
|
||||||
|
|
||||||
findUsbPrinter();
|
findUsbPrinter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void findUsbPrinter() {
|
private void findUsbPrinter() {
|
||||||
// Find the first connected USB device
|
|
||||||
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
|
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
|
||||||
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
|
for (UsbDevice device : deviceList.values()) {
|
||||||
while(deviceIterator.hasNext()){
|
|
||||||
UsbDevice device = deviceIterator.next();
|
boolean looksLikePrinter = false;
|
||||||
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
||||||
UsbInterface intf = device.getInterface(i);
|
UsbInterface intf = device.getInterface(i);
|
||||||
Integer intfClass = intf.getInterfaceClass();
|
// Prefer PRINTER or VENDOR_SPEC, but accept any interface that has BULK OUT
|
||||||
if (intfClass == UsbConstants.USB_CLASS_PRINTER || intfClass == UsbConstants.USB_CLASS_VENDOR_SPEC) {
|
if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER
|
||||||
usbInterface = intf;
|
|| intf.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC) {
|
||||||
|
looksLikePrinter = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < intf.getEndpointCount(); j++) {
|
||||||
|
UsbEndpoint ep = intf.getEndpoint(j);
|
||||||
|
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
|
||||||
|
&& ep.getDirection() == UsbConstants.USB_DIR_OUT) {
|
||||||
|
looksLikePrinter = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (looksLikePrinter) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (looksLikePrinter) {
|
||||||
usbDevice = device;
|
usbDevice = device;
|
||||||
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
||||||
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
||||||
);
|
);
|
||||||
if (!usbManager.hasPermission(usbDevice)) {
|
if (!usbManager.hasPermission(device)) {
|
||||||
usbManager.requestPermission(usbDevice, permissionIntent);
|
usbManager.requestPermission(device, permissionIntent);
|
||||||
} else {
|
} else {
|
||||||
setupPrinterConnection(usbDevice);
|
// We already have permission; now build interface+endpoint
|
||||||
|
if (!setupPrinterConnection(device)) {
|
||||||
|
Log.d("USB", "findUsbPrinter: permission ok but no interface/endpoint");
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return; // stop after first suitable device
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("USB", "findUsbPrinter: No suitable USB printer device found");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean setupPrinterConnection(UsbDevice device) {
|
||||||
|
usbInterface = null;
|
||||||
|
usbEndpoint = null;
|
||||||
|
|
||||||
|
if (device == null) {
|
||||||
|
Log.d("USB", "setupPrinterConnection: device is null");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick the first interface that has a BULK OUT endpoint
|
||||||
|
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
||||||
|
UsbInterface intf = device.getInterface(i);
|
||||||
|
|
||||||
|
for (int j = 0; j < intf.getEndpointCount(); j++) {
|
||||||
|
UsbEndpoint ep = intf.getEndpoint(j);
|
||||||
|
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
|
||||||
|
&& ep.getDirection() == UsbConstants.USB_DIR_OUT) {
|
||||||
|
|
||||||
|
usbInterface = intf;
|
||||||
|
usbEndpoint = ep;
|
||||||
|
|
||||||
|
Log.d("USB", "setupPrinterConnection: interface#" + i + ", OUT endpoint#" + j + " set");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPrinterConnection(UsbDevice device){
|
Log.d("USB", "setupPrinterConnection: No printer interface or BULK OUT endpoint found");
|
||||||
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
|
return false;
|
||||||
UsbEndpoint ep = usbInterface.getEndpoint(i);
|
|
||||||
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK &&
|
|
||||||
ep.getDirection() == UsbConstants.USB_DIR_OUT) {
|
|
||||||
usbEndpoint = ep;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void print(String text) throws Exception {
|
private void print(String text) throws Exception {
|
||||||
if (usbInterface == null) {
|
if (usbDevice == null) {
|
||||||
System.out.println("No printer interface found.");
|
Log.d("USB", "print: usbDevice is null; trying discovery");
|
||||||
|
findUsbPrinter();
|
||||||
|
if (usbDevice == null) {
|
||||||
|
System.out.println("No USB device found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (usbEndpoint == null) {
|
// Ensure we have a valid interface/endpoint for the current device
|
||||||
System.out.println("No bulk OUT endpoint found.");
|
if (usbInterface == null || usbEndpoint == null) {
|
||||||
|
if (!setupPrinterConnection(usbDevice)) {
|
||||||
|
System.out.println("No printer interface or BULK OUT endpoint found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
usbDeviceConnection = usbManager.openDevice(usbDevice);
|
usbDeviceConnection = usbManager.openDevice(usbDevice);
|
||||||
if (usbDeviceConnection == null) {
|
if (usbDeviceConnection == null) {
|
||||||
System.out.println("Unable to open connection.");
|
System.out.println("Unable to open connection. Requesting permission again...");
|
||||||
|
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
||||||
|
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
||||||
|
);
|
||||||
|
usbManager.requestPermission(usbDevice, permissionIntent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
usbDeviceConnection.claimInterface(usbInterface, true);
|
boolean claimed = usbDeviceConnection.claimInterface(usbInterface, true);
|
||||||
if(usbDeviceConnection == null){
|
if (!claimed) {
|
||||||
|
System.out.println("Failed to claim interface.");
|
||||||
|
usbDeviceConnection.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
||||||
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
|
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
|
||||||
printer.printFormatted(text);
|
printer.printFormatted(text);
|
||||||
printer.feed(5);
|
printer.feed(5);
|
||||||
printer.cut();
|
printer.cut();
|
||||||
|
} finally {
|
||||||
usbDeviceConnection.releaseInterface(usbInterface);
|
usbDeviceConnection.releaseInterface(usbInterface);
|
||||||
usbDeviceConnection.close();
|
usbDeviceConnection.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class WebAppInterface {
|
public class WebAppInterface {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user