How to write files from Android to PC and back? - java

The Situation:
Client has an Android phone that is not activated to use cellular services and his office does not have wifi. He wants me to write him a simple customized contact list that he can view the names and the phone numbers of people(already completed). However, he wants a Desktop Client (done in Java) where he can update the contacts, as he is a bit older and prefers using a standard sized keyboard instead of the slider keyboard on his phone.(this is completed as well).
I need to complete a class for the app that will read a file on the PC's local hard drive and update the contacts file on the SD card.
Options:
As far as I can tell, my only option in this case would be to use the USB port from the phone. I will have the desktop client write the contacts file to the SD card on the Android Device.
Question:
Can I use the USBManager class to have the app:
1. Look for xml contact file on PC.
2. Read the file on the PC and update the file on the SD card.
Additional Information:
Client's Phone Model: Motorola Droid II Global
Android OS version: 2.3.4
Perhaps it may be easier to have the Desktop Client access the SDCard and write the files to the Android Device. I am willing to review all concepts and ideas about this process. Thank you.

If the device is in "Development Mode" you can use the Android Debug Bridge (adb) to push/pull files from a device over a USB connection.
http://developer.android.com/tools/help/adb.html#copyfiles
If the SD card is removable than it sounds like it might be easier to purchase a SD card reader for a PC and have the PC java program write to the SD card, remove it from the PC and then re-insert it into the Android device and then the Android app can read it back from the SD card. Which is what you said in your last paragraph...

What about Bluetooth using Androids BluetoothSocket class, on the device? You can purchase USB Bluetooth dongles for PC's if needed. However, Using a USB cable connection would be easier.

Related

Android tablet can't connect to MagTek USB card reader after device reboot

I have an Android tablet (custom commercial tablet by a company called Glory Star) that is running on Android 5.1.1. With it I am using a MagTek Dynamag card reader (model #21073075), that is usable with the mtscra.jar library provided by MagTek (https://www.magtek.com/support/dynamag?tab=software). The library/card reader are working correctly, in that I am able to connect to and communicate with the card reader, pulling in card data whenever a card is swiped. Their library is using USB Host (http://developer.android.com/guide/topics/connectivity/usb/host.html).
Once a connection is established, an interface needs to be found and subsequently an endpoint on which the communication takes place. This all occurs as it should EXCEPT under one condition...after the tablet is rebooted. Once the tablet is rebooted and my app is launched, the mtscra library is still able to find the MagTek card reader, but it cannot locate an interface, so it ultimately can't establish a connection. If I unplug and replug in the card reader, however, the interface can again be found. I believe something is happening on reboot that is disabling the ability for the interface of the card reader to be located.
Is there a known solution to this issue? Is there a way to essentially programmatically "reset" the usb port (that would resemble unplugging and replugging it back in) when my app is launched (I have root access)? Or, is there a way to disable/enable the usb port via the command line?

USB-connection from android to phone

I'm trying to send data from android to a computer via USB,
I found the USB Accessory introduction, http://developer.android.com/guide/topics/connectivity/usb/accessory.html (Basically I want to send a few bytes to a programm on the computer) So, am i right on using the accessory on android? What would i use for the pc programm, and can I/ Do i have to create a socket connection from my application to the programm
You will need access to the USB device plugged in to the PC which lets you send USB Control Request and Bulk Transfer requests.
If you are using linux, you could try libusb - which is pretty straight forward to use. For windows am not sure though.

Android and JFileChooser

I've developed a desktop application that allows the user to extract images from their phone.
When I first started this project, my android was running Gingerbread. When I plugged my phone into the laptop via USB, it would appear as a "Device with removable storage". Therefore, the JFileChooser would pick it up.
nwdir = new JFileChooser();
nwdir.setCurrentDirectory(new java.io.File("C:\\"));
nwdir.setDialogTitle(choosertitle);
nwdir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
nwdir.setAcceptAllFileFilterUsed(true);
However, I recently upgraded the phone's o/s to Icecream and it now shows up on the desktop as a "portable device". This is something that the JFileChooser cannot see?!
Has anyone got any suggestions on how to get around this problem? I was going to roll back the O/S of the phone, but this somewhat limits the application's device support.
The default transfer mode has been changed in Android 4.0 Ice Cream Sandwich from USB Mass Storage mode to MTP (Media Transfer Protocol) which doesn't use a standard filesystem mount. On some phones it is possible to change the USB mode while on others it may be necessary to install an apk to enable it
I've found a way around it by doing this.... Settings > Wireless & Network > USB utilities and click the button > then (and ONLY then) plug in USB cable

Programmatically Turn Off USB Storage on Android Devices

On many android devices, when the device is plugged into the USB port of a computer or even on some USB charging devices, the phone goes into USB Storage mode. When the device is in this mode, android apps cannot access the sdcard. Is there any way (1) to detect when the device is in this mode and (2) to programmatically turn off USB storage, at least temporarily, so my android app can access the sdcard?
I've seen other SO questions and the answers are not really sufficient
(e.g., Android: Detecting USB).
A1: If current state of Sd card is SHARED it means that it has connected to PC in MSC mode, you can check this case as following:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_SHARED.equals(state)) {
// Sd card has connected to PC in MSC mode
}
A2: You can force to turn off usb mass storage connection by calling:
MountService.setUsbMassStorageEnabled(false);
or
StorageManager.disableUsbMassStorage();
but unfortunately, both these API are not public accessible?!
You can detect it (you have the link), but, AFAIK, you can't mount/unmount USB storage at least not with the public SDK APIs. In Honeycomb (3.0 and above), USB mass storage is no longer used to access the device's external storage, so your app and a computer can access it simultaneously.

Detected android device connected to PC

I have an android devic, i wish to know when it is connected to the pc via java/.net
i know its possible - the Samsung New PC Studio is doing it, when ever i connect an android device it lets me know. how can i achieve the same?
You have to listen for one of this notifications
http://developer.android.com/reference/android/content/Intent.html
If you're using it as a mass storage device then use this
http://developer.android.com/reference/android/content/Intent.html#ACTION_UMS_CONNECTED
Other USB events aren't clearly described in the documentation
EDIT:
If you want to read on the computer side I would read this article
http://today.java.net/article/2006/07/05/java-and-usb
and then check which kind of devices are connected to the usb port. if it is a mobile phone, then you can try to send a short message to it. on your phone you make a small piece of code which returns an appropriate answer so you can detect thats its really android/your phone/this phones you want to identify
java device detection
This site have a guide on how to detect device with java web and java core
http://51degrees.com/Support/Documentation/Java/GettingStarted.aspx

Categories