Android Question usb permission always asks every time the tablet is restarted

fifiddu70

Well-Known Member
Licensed User
Longtime User
hello to all, I'm creating an app that uses a thermal printer usb, I downloaded the library usb serial 2.4 and also downloaded the demo project of the library,
I tried to enter the data vid and pid of my printer and this is seen from the demo and immediately asks me for permission to usb,
the problem asks me for permission to usb every time I restart the tablet as soon as I access the printer demo,
I need never ask this permission because in my application does not require the intervention of a person that cues that box to have the permission of usb,
I also copied the file device_filter on the folder: object / res / xml
I have also added the code on the manifest editor, but I still have problems with permission at every restart of the tablet, I hope I explained myself,
in manifest editor i add this code:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="27"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
    AddPermission (android.permission.ACCESS_COARSE_LOCATION)
    AddPermission(android.permission.BLUETOOTH)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddActivityText(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" />)
CreateResource(xml, device_filter.xml,
<resources>
<usb-device vendor-id="456" product-id="808" />
</resources>
)
vendor id and product id of my print: vid= 456 pid = 808
i try to demo project to use usbserial and work print but when restarted the ask permission is show, the problem is only restart the tablet, this is code of example:
B4X:
#Region  Project Attributes
    #ApplicationLabel: USB Serial Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: false
    #BridgeLogger:True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    ' Demo has been changed to support Ver_2.4 JeanLC
    Dim usb1 As UsbSerial   
    Dim usb2 As UsbSerial   
    Dim astreams1 As AsyncStreams
    Dim astreams2 As AsyncStreams
    Dim connected As Boolean
End Sub

Sub Globals

    Dim btnSend, btnOpen, btnClose As Button
    Private btnOpen2 As Button
    Private btnClose2 As Button
    Private btnSend2 As Button
    Private txtreport As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    btnClose.Enabled = False
    btnSend.Enabled = False
    
    btnClose2.Enabled = False
    btnSend2.Enabled = False
End Sub

Sub btnOpen_Click
    If usb1.UsbPresent(1) = usb1.USB_NONE Then    ' Ver_2.4
        Msgbox("No device USB NONE","")
        Return
    End If
    If (usb1.HasPermission(1)) Then    ' Ver_2.4
        Log(usb1.DeviceInfo(1))
        'This is very important - ( VendorId, ProductId )
        usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x456, 0x808)

        Dim dev As Int
        dev = usb1.Open(19200, 1) 'STMicroeletronics
        If dev <> usb1.USB_NONE Then
            Log("Connected successfully! 1")
            btnOpen.Enabled = False
            btnClose.Enabled = True
            btnSend.Enabled = True
            
        
            Log("CONNECTED SUCCESSFULLY!!!")
            connected = True
            astreams1.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "astreams1")

            'This is important
            usb1.SetParameters(19200, usb1.DATABITS_8,usb1.STOPBITS_1, usb1.PARITY_EVEN)
            
            'Here if you want codce that call print
        Else
            Log("Error opening USB port 1")
        End If
    Else
        usb1.RequestPermission(1)  ' Ver_2.4
    End If

End Sub
Sub stampafattura
    
    txtreport.Text= "print demo!!"
    If connected  Then
        
        If astreams1.IsInitialized = False Then Return
        
        Dim SendMsg As String
        Dim buffer() As Byte
        buffer =  txtreport.Text.GetBytes("UTF8")
        SendMsg = Chr(27) & "!" &Chr(8+16+64) & "" & CRLF
        astreams1.Write(SendMsg.GetBytes("UTF8"))
        astreams1.Write(buffer)
        txtreport.SelectAll
        
        
        
    Else
        
        Msgbox("Attenzione connetti la stampante","STAMPANTE NON CONNESSA")
        
    End If

 
End Sub
Sub btnOpen2_Click
    If usb2.UsbPresent(2) = usb2.USB_NONE Then    ' Ver_2.4
        Log("Msgbox - no device")
        Msgbox("No USB device or accessory detected!", "Error")
        Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission 2")
    If (usb2.HasPermission(2)) Then    ' Ver_2.4
        Msgbox(usb2.DeviceInfo(2), "Device Information 2")    ' Ver_2.4
        Dim dev As Int
        'dev = usb.Open(115200, 2)        ' Ver_2.4
        dev = usb2.Open(9600, 2)        ' Ver_2.4
        If dev <> usb2.USB_NONE Then
            Log("Connected successfully! 2")
            btnOpen2.Enabled = False
            btnClose2.Enabled = True
            btnSend2.Enabled = True           
            astreams2.Initialize(usb2.GetInputStream, usb2.GetOutputStream, "astreams2")
        Else
            Log("Error opening USB port 2")
        End If
    Else
        usb2.RequestPermission(2)  ' Ver_2.4
    End If
End Sub

Sub astreams1_NewData (Buffer() As Byte)
    ' You must check for DeviceInfo or analyze Buffer data to know what is connected to the USB
    ' The order of the USB could change as you plug them and could change when changing the hub port they are connected to
    Log("NewData 1")
    Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub Astreams2_NewData (Buffer() As Byte)
    ' You must check for DeviceInfo or analyze Buffer data to know what is connected to the USB
    ' The order of the USB could change as you plug them and could change when changing the hub port they are connected to
    Log("NewData 2")
    Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub btnClose_Click
    astreams1.Close
    usb1.Close   
    btnOpen.Enabled = True
    btnClose.Enabled = False
    btnSend.Enabled = False
End Sub

Sub btnClose2_Click
    astreams2.Close
    usb2.Close
    btnOpen2.Enabled = True
    btnClose2.Enabled = False
    btnSend2.Enabled = False   
End Sub

Sub btnSend_Click
    stampafattura
End Sub

Sub btnSend2_Click
    astreams2.Write("abcde".GetBytes("UTF8"))
End Sub

Sub btnExit_Click
    ExitApplication
End Sub

Sub aStreams1_Error
    Log("Error: " & LastException)
    astreams1.Close
End Sub
Sub astreams1_Terminated
    Log("Terminated")
    astreams1.Close
End Sub

Sub AStreams2_Error
    Log("Error: " & LastException)
    astreams2.Close
End Sub
Sub Astreams2_Terminated
    Log("Terminated")
    astreams2.Close
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
please help.
 

fifiddu70

Well-Known Member
Licensed User
Longtime User
i have solved this problem, this code in manifest editor not working:
B4X:
CreateResource(xml, device_filter.xml,
<resources>
<usb-device vendor-id="456" product-id="808" />
</resources>
)
the vendori id and product id is ok but the code not work, i try to sostitute with this code:
B4X:
AddActivityText(main, <intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

<intent-filter>
<action android:name="b4a.gkc.weather.action.START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>


<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />)
this code is correct and work fine
 
Upvote 0
Top