Android Question Connecting Android to Windows using USB cable not Bluetooth/WiFi

Dave G

Active Member
Licensed User
My goal is to allow an Android (maybe iOS later) to connect to Windows using USB cable (B4A compiler) to exchange data with an app on the Android and another one on Windows without use of Bluetooth or Wi-Fi.

On Windows side I'm trying to use B4J-Chat, which sees the Android USB connection as COM9. I can type in text to send, but since there isn't a client on the Android nothing happens.

On the Android side I'm trying USBExample/USB. It doesn't see the USB to Windows connection, but will see as SD Card that is plugged into Android.

So, my question is how do I get Android to see the USB cable connection to Windows?

This is for an application I'm working on for a non-profit that will connect home medical devices to PC, MacOS, Linux, and hopefully mobile devices such as phone. I successfully am able to connect an SD Card to the phone, but navigating thru the SD Card is too error prone and Android won't allow my app to access anything outside it's sandbox without the user doing the selection of files on the SD Card.

Thanks
 
Last edited:
Solution
The only way to do it is with USB debug mode. You can then run ADB to create a TCP/IP connection. This is how the IDE works in debug mode for example.

agraham

Expert
Licensed User
Longtime User
If you were happy with a passive connection just plugging the device into a USB cable to the desktop makes the device appear as a drive in windows, Perhaps you could then use normal file operations in a B4J app to do whatever you want. The desktop appears to have complete unfettered access to the device file store as far as I can see.
 
Upvote 0

Dave G

Active Member
Licensed User
A device such as a phone is not a normal mounted device like C:. It's not accessible using normal file access methods. Windows Explorer is able to get to it and display folders and files. It's even difficult to get to a phone using C#.Net. BUT, I found a way to do it using ADB, as Erel suggested. Android makes it very difficult to access data on the device in order to prevent rogue apps from stealing/corrupting data. B4X provides externalStorage library, but it's very difficult for a user to navigate through the Android folders and some areas are restricted. I'm able to see everything on the Android phone from an application on Windows. Can copy from/to device, and a bunch of other cool stuff. All without the user doing anything on the phone. So far I've done it with C# and next will see if I can do it with B4J.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
A device such as a phone is not a normal mounted device like C:. It's not accessible using normal file access methods. Windows Explorer is able to get to it and display folders and files.
That's true. Most phones are connected as MTP devices (same as cameras). It is quite difficult to work with this connection.
 
Upvote 0

Dave G

Active Member
Licensed User
I was able to create a B4J app that invokes ADB to get info from a USB connected phone/tablet. Works surprisingly well. Can walk through the file system, copy files between Android device and Windows. Clean up files. Get devices IP. Install APKs. Even control the UI! Very impressed with ADB.

My main goal was to be able to transfer data directly to an app's 'sandboxed' folder. In my case data from medical devices. This approach does not require the user to do anything on the Android side and only run my B4J app on the Windows side. Will see if I can port to macOS and Linux.

Thanks for your suggestion regarding ADB Erel.
 
Upvote 0
Top