Android Question USB Serial permission

iCAB

Well-Known Member
Licensed User
Hi There
I am testing USB serial using the sample code

B4X:
   If manager.GetDevices.Length = 0 Then
     Log("No connected usb devices.")
   Else
     Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
     If manager.HasPermission(device) = False Then
       ToastMessageShow("Please allow connection and click again.", True)
       manager.RequestPermission(device)
     Else
       usbserial.Initialize("serial", device, -1)
       usbserial.BaudRate = 9600
       usbserial.DataBits = usbserial.DATA_BITS_8
       usbserial.StartReading
       CheckConnected.Initialize("CheckConnected", 2000)
       CheckConnected.Enabled = True
     End If

I understand that this popup should appear the first time
"Allow the App XXX to access USB device?
Use by default for this USB device"

But why does it get display after disconnecting and restarting the app? Is there a way around it?


Thanks
iCAB
 

Erel

Administrator
Staff member
Licensed User
You need to add to the manifest editor:
B4X:
AddApplicationText(
<activity android:name="main" >
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
   
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</activity>
)
CreateResource(xml, device_filter.xml,
<resources>
<usb-device vendor-id="1753" product-id="c902"/>
</resources>
)
Change the vendor and product values.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Thanks Erel, works great!

But this is what I have noticed, if I disconnect the USB cable and reconnect while the app is running. The app restarts but gets stuck at the "splash" screen. Here is what the log shows:;

B4X:
** Activity (amaindashboard) Pause, UserClosed = false **
sending message to waiting queue (sleep)
Killing previous instance (main).
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (apermissions) Create, isFirst = false **
running waiting messages (2)
Sleep not resumed (context destroyed): com.abc.template.apermissions$ResumableSub_HideKeyboard
Sleep not resumed (context destroyed): com.abc.template.apermissions$ResumableSub_HideKeyboard
** Activity (apermissions) Resume **
** Activity (apermissions) Pause, UserClosed = false **
** Activity (asplash) Resume **

Any ideas how to prevent the app from restarting like this?

Thanks
iCAB
 
Last edited:
Upvote 0
Top