Android Question USBManager TargetSDK=34

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All,

I have an app that uses USBManager to communicate with a serial device. The app works perfectly ok until I change the TargetSDK = 34. I have noticed that the code below returns 0, when the TargetSDK is to 34

Code Fails:
If USBManager1.GetDevices.Length = 0 Then
    Log("Error")
End If

While checking, and if I am not mistaken it seems that we need the code for the Broadcast receiver as in this link:
https://developer.android.com/develop/connectivity/usb/host#java

If anyone has a B4A sample using the above code, I would greatly appreciate your help!

Thanks
iCAB
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for your reply and your help.

Below is the manifest file I am using. As you can see in the comments, I have also tried the device filter suggestion as per the above post.

Manifest file:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="34"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
SetApplicationAttribute(android:allowBackup, "false")
'SetApplicationAttribute(android:theme, "@style/MyTheme")
'End of default text.


'**********REQUIRED FOR QCode, also corresponding RunTime permission should be requested
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

'********** GPS Permission ****************************
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_CONNECT)



'********** Camera Permission section ****************************
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)

AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
AddPermission("android.hardware.camera")


'********************************************************************************
'This section was added for FileProvider
AddManifestText(<uses-permission
   android:name="android.permission.WRITE_EXTERNAL_STORAGE"
   android:maxSdkVersion="28" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

'********************************************************************************
'******************* For USB permissions
AddApplicationText(
<activity android:name="main" android:exported="true">
    <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="1027" product-id="24577"/>
</resources>
)


'''' I also tried commenting the above block and used this one
'AddActivityText(main,
'<intent-filter>
'    <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
'    <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>
'    <!-- 0x067b / 0x2303 Prolific PL2303 -->
'    <usb-device vendor-id="1027" product-id="2457" />
'</resources>
')
'SetActivityAttribute(main, android:directBootAware, "true")


''''' I also tried with and without this block
AddManifestText( <uses-permission android:name="android.permission.USB_PERMISSION" /> )
AddManifestText( <uses-feature android:name="android.hardware.usb.host" /> )
AddManifestText( <uses-feature android:name="android.hardware.usb.accessory"/> )
AddManifestText(<uses-permission android:name="android.hardware.usb.host" />)





#if USE_GOOGLE_PLAY_SERVICE
'************ Google Play Services Base ************
AddApplicationText(
   <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:exported="false"/>
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************
#end if

Thanks again for your support!!!
iCAB
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks again for your reply.

As I mentioned in the first post, I am only facing the problem if I change the TargetSDK to 34:
Problematic code:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="34"/>

if I set the TargetSDK back to 28, there are no issues whatsoever. In fact the app has been running in the field for 3+years:
Ok Code:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28"/>

Does this change your statement at all?
Also, ,do we need the Broadcast receiver code as mentioned here: https://developer.android.com/develop/connectivity/usb/host#java?

Thanks again for your reply!

iCAB
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No special permission is needed to list the devices. I've just tested this code:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim usb As UsbManager
    usb.Initialize
    For Each d As UsbDevice In usb.GetDevices
        Log(d.DeviceName & ", " & d.DeviceId & ", " & d.DeviceClass)
    Next
End Sub
With targetSdkVersion=34 and an Android 15 device.

The output:
/dev/bus/usb/001/002, 1002, 0
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Yes you are right, but here is the interesting part, (I am getting the same behavior with your sample code above and with my application ):
  • Connect the tablet to the controller board.
  • Perform a fresh install.
  • Run the above code, no devices are found.
  • Restart the app, and run the above code, no devices are found
  • Disconnect the USB cable from the Tablet size, and reconnect it, run the above code no devices are found.
  • If I disconnect and reconnect the USB cable from the controller side, I immediately get a prompt
    "OpenAppName to Handle FT232 USB UART", after which the device list starts to show the device, and from there on things work properly
  • Also If I leave the 2 connected for a few minutes without disconnecting and reconnecting, the above prompt eventually shows up on its's own and after which all is good

Also, I changed the sample code a bit for testing, and I noticed that the DeviceName and DeviceID keep changing


B4X:
Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    Dim usb As UsbManager
    usb.Initialize
    For Each d As UsbDevice In usb.GetDevices
        xui.MsgboxAsync(d.DeviceName & ", " & d.DeviceId & ", " & d.DeviceClass, "B4X")
        Exit
    Next
    
End Sub


Thanks for your help!!!
 
Upvote 0