|
| 1 | +package net.sourceforge.smallbasic; |
| 2 | + |
| 3 | +import android.app.PendingIntent; |
| 4 | +import android.content.BroadcastReceiver; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.IntentFilter; |
| 8 | +import android.hardware.usb.UsbDevice; |
| 9 | +import android.hardware.usb.UsbDeviceConnection; |
| 10 | +import android.hardware.usb.UsbEndpoint; |
| 11 | +import android.hardware.usb.UsbInterface; |
| 12 | +import android.hardware.usb.UsbManager; |
| 13 | +import android.os.Build; |
| 14 | +import android.os.Handler; |
| 15 | +import android.os.Looper; |
| 16 | +import android.util.Log; |
| 17 | +import android.widget.Toast; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +/** |
| 22 | + * Usb host mode communications |
| 23 | + */ |
| 24 | +public class UsbConnection extends BroadcastReceiver { |
| 25 | + private static final String TAG = "smallbasic"; |
| 26 | + private static final String ACTION_USB_PERMISSION = "smallbasic.android.USB_PERMISSION"; |
| 27 | + private static final String PERMISSION_ERROR = "Not permitted"; |
| 28 | + private static final String USB_DEVICE_NOT_FOUND = "Usb device not found"; |
| 29 | + private static final int TIMEOUT_MILLIS = 1000; |
| 30 | + private static final int BUFFER_SIZE = 64; |
| 31 | + |
| 32 | + private final UsbDeviceConnection _connection; |
| 33 | + private final UsbInterface _usbInterface; |
| 34 | + private final UsbDevice _usbDevice; |
| 35 | + private final UsbManager _usbManager; |
| 36 | + private final UsbEndpoint _endpointIn; |
| 37 | + private final UsbEndpoint _endpointOut; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructs a new UsbConnection |
| 41 | + */ |
| 42 | + public UsbConnection(Context context, int vendorId) throws IOException { |
| 43 | + _usbManager = getUsbManager(context); |
| 44 | + _usbDevice = getDevice(_usbManager, vendorId); |
| 45 | + if (_usbDevice == null) { |
| 46 | + throw new IOException(USB_DEVICE_NOT_FOUND); |
| 47 | + } |
| 48 | + |
| 49 | + if (!_usbManager.hasPermission(_usbDevice)) { |
| 50 | + requestPermission(context); |
| 51 | + throw new IOException(PERMISSION_ERROR); |
| 52 | + } |
| 53 | + |
| 54 | + _usbInterface = _usbDevice.getInterface(0); |
| 55 | + _endpointIn = _usbInterface.getEndpoint(0); |
| 56 | + _endpointOut = _usbInterface.getEndpoint(1); |
| 57 | + _connection = _usbManager.openDevice(_usbDevice); |
| 58 | + _connection.claimInterface(_usbInterface, true); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Closes the USB connection |
| 63 | + */ |
| 64 | + public void close() { |
| 65 | + if (_connection != null && _usbInterface != null) { |
| 66 | + _connection.releaseInterface(_usbInterface); |
| 67 | + _connection.close(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Handles receiving the request permission response event |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void onReceive(Context context, Intent intent) { |
| 76 | + Log.d(TAG, "onReceive entered"); |
| 77 | + if (ACTION_USB_PERMISSION.equals(intent.getAction())) { |
| 78 | + boolean permitted = _usbManager.hasPermission(_usbDevice); |
| 79 | + String name = _usbDevice.getProductName(); |
| 80 | + final String message = "USB connection [" + name + "] access " + (permitted ? "permitted" : "denied"); |
| 81 | + final BroadcastReceiver receiver = this; |
| 82 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 83 | + Toast.makeText(context, message, Toast.LENGTH_LONG).show(); |
| 84 | + context.unregisterReceiver(receiver); |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Receives the next packet of data from the usb connection |
| 91 | + */ |
| 92 | + public String receive() { |
| 93 | + String result; |
| 94 | + byte[] dataIn = new byte[BUFFER_SIZE]; |
| 95 | + int bytesRead = _connection.bulkTransfer(_endpointIn, dataIn, dataIn.length, TIMEOUT_MILLIS); |
| 96 | + if (bytesRead > 0) { |
| 97 | + result = new String(dataIn, 0, bytesRead); |
| 98 | + } else { |
| 99 | + result = ""; |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sends the given data to the usb connection |
| 106 | + */ |
| 107 | + public void send(byte[] dataOut) { |
| 108 | + _connection.bulkTransfer(_endpointOut, dataOut, dataOut.length, TIMEOUT_MILLIS); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the UsbDevice matching the given vendorId |
| 113 | + */ |
| 114 | + private UsbDevice getDevice(UsbManager usbManager, int vendorId) { |
| 115 | + UsbDevice result = null; |
| 116 | + for (UsbDevice device : usbManager.getDeviceList().values()) { |
| 117 | + if (device.getVendorId() == vendorId) { |
| 118 | + result = device; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns the UsbManager |
| 127 | + */ |
| 128 | + private static UsbManager getUsbManager(Context context) { |
| 129 | + return (UsbManager) context.getSystemService(Context.USB_SERVICE); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Invokes the display of a permission prompt |
| 134 | + */ |
| 135 | + private void requestPermission(Context context) { |
| 136 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 137 | + IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); |
| 138 | + filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1); |
| 139 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 140 | + context.registerReceiver(this, filter, Context.RECEIVER_NOT_EXPORTED); |
| 141 | + } |
| 142 | + int flags = PendingIntent.FLAG_IMMUTABLE; |
| 143 | + Intent intent = new Intent(ACTION_USB_PERMISSION); |
| 144 | + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, flags); |
| 145 | + _usbManager.requestPermission(_usbDevice, pendingIntent); |
| 146 | + }); |
| 147 | + Log.d(TAG, "requesting permission"); |
| 148 | + } |
| 149 | +} |
0 commit comments