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.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.webkit.CookieManager;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.WebSettings;
|
||||
@ -26,6 +27,9 @@ import androidx.appcompat.content.res.AppCompatResources;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import android.os.Process;
|
||||
import android.widget.Button;
|
||||
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@ -40,19 +44,49 @@ public class MainActivity extends AppCompatActivity {
|
||||
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
|
||||
synchronized (this) {
|
||||
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
||||
if (device != null) {
|
||||
setupPrinterConnection(device);
|
||||
}
|
||||
String action = intent.getAction();
|
||||
|
||||
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);
|
||||
if (device != null) {
|
||||
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
|
||||
// protected void onCreate(Bundle savedInstanceState) {
|
||||
// super.onCreate(savedInstanceState);
|
||||
@ -68,106 +102,170 @@ public class MainActivity extends AppCompatActivity {
|
||||
//
|
||||
// webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
||||
// webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
||||
//// webView.loadUrl("file:///android_asset/index.html");
|
||||
//// webView.loadUrl("file:///android_asset/index.html");
|
||||
// findUsbPrinter();
|
||||
// }
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||
IntentFilter filter = new IntentFilter();
|
||||
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);
|
||||
|
||||
registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
|
||||
WebView webView = new WebView(this);
|
||||
setContentView(webView);
|
||||
WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
webSettings.setDomStorageEnabled(true);
|
||||
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
||||
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||
webView.setWebViewClient(new WebViewClient());
|
||||
CookieManager.getInstance().setAcceptCookie(true);
|
||||
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
|
||||
|
||||
WebView webView = new WebView(this);
|
||||
setContentView(webView);
|
||||
WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
webSettings.setDomStorageEnabled(true);
|
||||
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
||||
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||
webView.setWebViewClient(new WebViewClient());
|
||||
CookieManager.getInstance().setAcceptCookie(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("file:///android_asset/index.html");
|
||||
|
||||
webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
||||
//webView.loadUrl("file:///android_asset/index.html");
|
||||
findUsbPrinter();
|
||||
}
|
||||
findUsbPrinter();
|
||||
}
|
||||
|
||||
private void findUsbPrinter(){
|
||||
// Find the first connected USB device
|
||||
|
||||
|
||||
private void findUsbPrinter() {
|
||||
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
|
||||
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
|
||||
while(deviceIterator.hasNext()){
|
||||
UsbDevice device = deviceIterator.next();
|
||||
for (UsbDevice device : deviceList.values()) {
|
||||
|
||||
boolean looksLikePrinter = false;
|
||||
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
||||
UsbInterface intf = device.getInterface(i);
|
||||
Integer intfClass = intf.getInterfaceClass();
|
||||
if (intfClass == UsbConstants.USB_CLASS_PRINTER || intfClass == UsbConstants.USB_CLASS_VENDOR_SPEC) {
|
||||
usbInterface = intf;
|
||||
usbDevice = device;
|
||||
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
||||
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
||||
);
|
||||
if (!usbManager.hasPermission(usbDevice)) {
|
||||
usbManager.requestPermission(usbDevice, permissionIntent);
|
||||
} else {
|
||||
setupPrinterConnection(usbDevice);
|
||||
}
|
||||
// Prefer PRINTER or VENDOR_SPEC, but accept any interface that has BULK OUT
|
||||
if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER
|
||||
|| 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;
|
||||
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
||||
this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
|
||||
);
|
||||
if (!usbManager.hasPermission(device)) {
|
||||
usbManager.requestPermission(device, permissionIntent);
|
||||
} else {
|
||||
// We already have permission; now build interface+endpoint
|
||||
if (!setupPrinterConnection(device)) {
|
||||
Log.d("USB", "findUsbPrinter: permission ok but no interface/endpoint");
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("USB", "setupPrinterConnection: No printer interface or BULK OUT endpoint found");
|
||||
return false;
|
||||
}
|
||||
|
||||
private void setupPrinterConnection(UsbDevice device){
|
||||
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
|
||||
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 {
|
||||
if (usbInterface == null) {
|
||||
System.out.println("No printer interface found.");
|
||||
return;
|
||||
if (usbDevice == null) {
|
||||
Log.d("USB", "print: usbDevice is null; trying discovery");
|
||||
findUsbPrinter();
|
||||
if (usbDevice == null) {
|
||||
System.out.println("No USB device found.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (usbEndpoint == null) {
|
||||
System.out.println("No bulk OUT endpoint found.");
|
||||
return;
|
||||
// Ensure we have a valid interface/endpoint for the current device
|
||||
if (usbInterface == null || usbEndpoint == null) {
|
||||
if (!setupPrinterConnection(usbDevice)) {
|
||||
System.out.println("No printer interface or BULK OUT endpoint found.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
usbDeviceConnection = usbManager.openDevice(usbDevice);
|
||||
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;
|
||||
}
|
||||
|
||||
usbDeviceConnection.claimInterface(usbInterface, true);
|
||||
if(usbDeviceConnection == null){
|
||||
boolean claimed = usbDeviceConnection.claimInterface(usbInterface, true);
|
||||
if (!claimed) {
|
||||
System.out.println("Failed to claim interface.");
|
||||
usbDeviceConnection.close();
|
||||
return;
|
||||
}
|
||||
|
||||
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
||||
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
|
||||
printer.printFormatted(text);
|
||||
printer.feed(5);
|
||||
printer.cut();
|
||||
usbDeviceConnection.releaseInterface(usbInterface);
|
||||
usbDeviceConnection.close();
|
||||
try {
|
||||
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
||||
printer.logo(AppCompatResources.getDrawable(this, R.drawable.updatedlogocropped));
|
||||
printer.printFormatted(text);
|
||||
printer.feed(5);
|
||||
printer.cut();
|
||||
} finally {
|
||||
usbDeviceConnection.releaseInterface(usbInterface);
|
||||
usbDeviceConnection.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class WebAppInterface {
|
||||
Context mContext;
|
||||
WebAppInterface(Context c){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user