1. Are you ready for the Galaxy S20? Here is everything we know so far!

USB comms between Tablet and Ubuntu PC Exception

Discussion in 'Android Development' started by mdsousa, Nov 24, 2020.

  1. mdsousa

    mdsousa Newbie
    Thread Starter

    Hi

    I have an Ubuntu laptop and a Android 9 Tablet (Samsung Active 2). The tablet is connected to the laptop with FTDI connectors and a NULL cable. The code to connect to the laptop through the FTDI connectors and NULL cable is below:
    Code (Java):
    1.  
    2. import android.hardware.usb.UsbDevice;
    3. import android.hardware.usb.UsbDeviceConnection;
    4. import android.hardware.usb.UsbManager;
    5. import com.hoho.android.usbserial.driver.UsbSerialDriver;
    6. import com.hoho.android.usbserial.driver.UsbSerialPort;
    7. import com.hoho.android.usbserial.driver.UsbSerialProber;
    8. .
    9. .
    10. .
    11. try
    12. {
    13.    UsbManager usbManager = (UsbManager) view.getContext().getSystemService(Context.USB_SERVICE);
    14.    if( usbManager != null )
    15.    {
    16.       Log.i("USB COMMS USBMANAGER", usbManager.toString());
    17.       Map<String, UsbDevice> deviceList = usbManager.getDeviceList();
    18.       Log.i("USB COMMS DeviceList Size: ", String.valueOf(deviceList.size()));
    19.       UsbSerialDriver usbDriver = null;
    20.       UsbSerialPort usbPort = null;
    21.       UsbDeviceConnection usbConnection = null;
    22.       for( UsbDevice usbDevice : deviceList.values() )
    23.       {
    24.           Log.i("USB COMMS UsbDevice: ", usbDevice.toString());
    25.           String deviceName = usbDevice.getManufacturerName();
    26.           Log.i("USB COMMS deviceName: ", deviceName);
    27.           if( deviceName.toLowerCase().contains("ftdi") )
    28.           {
    29.               usbDriver = UsbSerialProber.getDefaultProber().probeDevice(usbDevice);
    30.               Log.i("USB COMMS driver: ", usbDriver.toString());
    31.               usbConnection = usbManager.openDevice(usbDriver.getDevice());
    32.               if( usbConnection != null ) {
    33.                   Log.i("USB COMMS connection: ", usbConnection.toString());
    34.                   usbPort = usbDriver.getPorts().get(0);
    35.                   Log.i("USB COMMS port: ", usbPort.toString());
    36.                   usbPort.open(usbConnection);
    37.                   Log.i("USB COMMS port is open ", "");
    38.                   usbPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
    39.               }
    40.               else
    41.               {
    42.                   Log.i("USB COMMS connection is null", "");
    43.               }
    44.               break;
    45.           }
    46.       }
    47.       if( usbPort != null )
    48.       {
    49.           Log.i("USB COMMS calling SerialMsgThread with port: ", usbPort.toString());
    50.           SerialMsgThread st = new SerialMsgThread(usbPort, mapView);
    51.           st.start();
    52.       }
    53.       else
    54.       {
    55.           Log.i("SerialMsgThread ", "USBSerialDriver is Null");
    56.       }
    57.    }
    58.    else
    59.    {
    60.       Log.i("USB COMMS USBMANAGER", "IS NULL");
    61.    }
    62. }
    63. catch(Exception e)
    64. {
    65.   Log.i("USB COMMS EXCEPTION: ", e.toString());
    66. }
    67.  
    I have the following in my manifest.xml
    HTML:
    1.  
    2.         <service
    3.            android:name="com.android.notification.NotificationService"
    4.            android:label="Plugin Notification Service">
    5.             <intent-filter>
    6.                 <action android:name="com.android.notification.NotificationService"/>
    7.             </intent-filter>
    8.             <intent-filter>
    9.                 <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    10.             </intent-filter>
    11.             <meta-data
    12.                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
    13.                android:resource="@xml/accessory_filter" />
    14.         </service>
    15.  
    I have the following device filter under res/
    HTML:
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <resources>
    4. <!-- The following is the FTDI parameters -->
    5.     <usb-device vendor-id="1027" product-id="24577" model="FT232" manufacturer="FTDI" version="6.0"/>
    6. </resources>
    7.  
    This is the logcat output, including the exception:
    Code (Java):
    1.  
    2. 2020-11-24 08:22:28.599 1578-1578/Â USBÂ COMMSÂ USBMANAGER: android.hardware.usb.UsbManager@7de64a6
    3. 2020-11-24 08:22:28.601 1578-1578/ USB COMMS DeviceList Size:: 1
    4. 2020-11-24 08:22:28.606 1578-1578/Â USBÂ COMMSÂ UsbDevice:: UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1027,mProductId=24577,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=FTDI,mProductName=USB Serial Converter,mVersion=6.00,mSerialNumber=FTBVMD1M,mConfigurations=[
    5.     '-->UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=22,mInterfaces=[
    6.    '-->UsbInterface[mId=0,mAlternateSetting=0,mName=USB Serial Converter,mClass=255,mSubclass=255,mProtocol=255,mEndpoints=[
    7.     '-->UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0]
    8.    '-->UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]]
    9. 2020-11-24 08:22:28.606 1578-1578/Â USBÂ COMMSÂ deviceName:: FTDI
    10. 2020-11-24 08:22:28.612 1578-1578/Â USBÂ COMMSÂ driver:: com.hoho.android.usbserial.driver.FtdiSerialDriver@abf9ee7
    11. 2020-11-24 08:22:28.617 1578-1578/ E/UsbManager: exception in UsbManager.openDevice
    12.     java.lang.SecurityException: User has not given permission to device UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1027,mProductId=24577,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=FTDI,mProductName=USB Serial Converter,mVersion=6.00,mSerialNumber=FTBVMD1M,mConfigurations=[
    13.     UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=22,mInterfaces=[
    14.     UsbInterface[mId=0,mAlternateSetting=0,mName=USB Serial Converter,mClass=255,mSubclass=255,mProtocol=255,mEndpoints=[
    15.     UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0]
    16.     UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]]
    17.         at android.os.Parcel.createException(Parcel.java:1966)
    18.         at android.os.Parcel.readException(Parcel.java:1934)
    19.         at android.os.Parcel.readException(Parcel.java:1884)
    20.         at android.hardware.usb.IUsbManager$Stub$Proxy.openDevice(IUsbManager.java:614)
    21.         at android.hardware.usb.UsbManager.openDevice(UsbManager.java:791)
    22.         at android.view.View.performClick(View.java:7348)
    23.         at android.widget.TextView.performClick(TextView.java:14215)
    24.         at android.view.View.performClickInternal(View.java:7314)
    25.         at android.view.View.access$3200(View.java:846)
    26.         at android.view.View$PerformClick.run(View.java:27803)
    27.         at android.os.Handler.handleCallback(Handler.java:873)
    28.         at android.os.Handler.dispatchMessage(Handler.java:99)
    29.         at android.os.Looper.loop(Looper.java:214)
    30.         at android.app.ActivityThread.main(ActivityThread.java:7179)
    31.         at java.lang.reflect.Method.invoke(Native Method)
    32.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    33.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
    34.      Caused by: android.os.RemoteException: Remote stack trace:
    35.         at com.android.server.usb.UsbUserSettingsManager.checkPermission(UsbUserSettingsManager.java:243)
    36.         at com.android.server.usb.UsbHostManager.openDevice(UsbHostManager.java:560)
    37.         at com.android.server.usb.UsbService.openDevice(UsbService.java:368)
    38.         at android.hardware.usb.IUsbManager$Stub.onTransact(IUsbManager.java:70)
    39.         at android.os.Binder.execTransact(Binder.java:739)
    40.  

    Edit:
    This is my rules files for USB devices (/etc/udev/rules.d/my-newrule.rules):
    Code (Text):
    1.  
    2. KERNEL=="ttyACM0", MODE="0777"
    3. KERNEL=="ttyUSB0", MODE="0777"
    4.  

    Any suggestions as to how to fix this would be appreciated.

    Thanks...
     


    #1 mdsousa, Nov 24, 2020
    Last edited: Nov 24, 2020

    1. Download the Forums for Android™ app!


      Download

       
  2. I'm still learning about this myself, but to use USB you need to make a permission popup happen and the user has to grant permission. Everything I've read so far says there's no way to skip that. Since you're seeing a SecurityException, my guess is that's what you're running into.

    To make this happen, you need to set up the app configuration right, and in your code you have to create an Intent that asks for "com.android.example.USB_PERMISSION", call UsbManager.requestPermission() with it, and have a BroadcastReceiver ready to receive the permission response and use it to set up the USB connection.

    See these links:
    https://stackoverflow.com/questions/12388914/usb-device-access-pop-up-suppression
    https://developer.android.com/guide/topics/connectivity/usb/accessory.html

    If you've managed to figure this out since you posted this question, I'd appreciate some advice.
     
    ocnbrze likes this.
Loading...

Share This Page

Loading...