Android Question SDK 31 and USB.RequestPermission

Filippo

Expert
Licensed User
Longtime User
Hi,

same problem with the Internal Library "USB" version 0.98 as in the thread: https://www.b4x.com/android/forum/threads/sdk-31-and-usbserial-requestpermission.143602/
Is there already an update?

B4X:
Private usbmanager As UsbManager
    
Public Sub ConnectUsbButton
    Dim indexUsbDevice As Int = GetUsbDevice
    usbmanager.RequestPermission(device)
End Sub

 

jahswant

Well-Known Member
Licensed User
Longtime User

Check this library. It was updated today.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Hi @jahswant ,

thanks for the answer.

I also use in my app the library "felUscbSerial", if i replace the lirary "USB" with "UsbSerial26" then the library "felUscbSerial" doesn't work anymore.
I have to enter a USB device when initializing "felUscbSerial", I don't get it with the library "UsbSerial26".

B4X:
Sub Class_Globals
  Private usbmanager As UsbSerial' UsbManager
  Private usbserial As felUsbSerial
End Sub

Dim device As UsbDevice = usbmanager.GetDevices(1) 'The function GetDevices does not exist.'

usbserial.Initialize("serial", device, -1)
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
B4X:
If usb1.UsbPresent(1) = usb1.USB_NONE Then    ' Ver_2.4
                            Log("MsgboxAsync - no device")
                            MsgboxAsync("No USB device or accessory detected!", "Error 1")
                            Log("MsgboxAsync - returned")
                            Return
                        End If
                        Log("Checking permission 1")
                        If (usb1.HasPermission(1)) Then    ' Ver_2.4
                            Dim dev As Int
                            'dev = usb.Open(115200, 1)        ' Ver_2.4
                            dev = usb1.Open(9600, 1)        ' Ver_2.4
                            If dev <> usb1.USB_NONE Then
                                AstreamsUSBSerial.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "AstreamsUSBSerial")
                                Sleep(1000)
                                AstreamsUSBSerial.Write(PrintTicket1(Sales_Code1).GetBytes ("windows-1252"))
                             
                                BCT.Show(Main.Loc.Localize("printedok"))
                                AstreamsUSBSerial.Close
                            Else
                                Log("Error opening USB port 1")
                            End If
                        Else
                            usb1.RequestPermission(1)  ' Ver_2.4
                        End If
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Hi @jahswant ,

I never used the library "UsbSerial" because it never worked with my USB device (Arduino-Nano, Leonardo USB, ...).
Even now it does not work, see logs.

B4X:
Public Sub ConnectUsbButton
    If usbmanager.UsbPresent(1) = usbmanager.USB_NONE Then
        'Log("No connected usb devices.")
        ToastMessageShow(Starter.Language.Value("USBNotConnected"), True)
        IsConnected = False
    Else
        If usbmanager.HasPermission(1) = False Then
            usbmanager.RequestPermission(1)
            
            Dim timeout As Int
            Do While Not(usbmanager.HasPermission(1)) And timeout < 30
                Sleep(1000)
                timeout = timeout + 1
                'Log("Sleep(1000)" & timeout)
            Loop
            
            If usbmanager.HasPermission(1) Then
                'LogColor("ConnectUsbButton", Colors.Green)
                ConnectUsbButton
            End If
        Else
            Dim dev As Int
            dev = usbmanager.Open(9600, 1)
            If dev <> usbmanager.USB_NONE Then
                astreams1.Initialize(usbmanager.GetInputStream, usbmanager.GetOutputStream, "serial")
                IsConnected = True
            Else
                Log("Error opening USB port 1")
            End If
        End If
    End If
End Sub

Private Sub serial_Error
    Log("Error: " & LastException)
    astreams1.Close
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
UsbSerial is irrelevant as far as Fillippo is concerned. It is not a replacement for the USB library. He is not dealing with simulated serial ports, he is dealing directly with USB devices, the USB library is a wrapper round the Android USB APIs. He needs the USB library modified to add the now required flags but while I wrote v0.93 Erel has expanded it significantly since then and I do not have the latest source code.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…