Programmatically Turn Off USB Storage on Android Devices - java

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.

Related

Programatically establish serverless bluetooth connection with computer

I made an app to turn my phone (LGV20) into a PC keyboard using Android Bluetooth. I'm trying to remove the PC server code by getting my app to pair my phone as a keyboard instead of a phone.
The only app I found that doesn't require a PC setup or rooting is Blek. It seems that when you turn the app on it tries to pair your phone as a keyboard like I want. However, I can't figure out how they did it. I imagine they did something like this:
public class MyBluetoothAdapter extends BluetoothAdapter {
private static final String DEVICE_PAIR_TYPE = "Keyboard"
...
}
That's not a real variable in BluetoothAdapter but I imagine something like this was used.
The hint should be when you looked at the app's description:
The receiver device must have Bluetooth 4.0 ....
So a search shows that among the many GATT specifications:
https://www.bluetooth.com/specifications/gatt
There is a HID over GATT profile
And it appears that each Bluetooth LE chip manufacturer has a keyboard HID over GATT example.
I suspect that each Bluetooth LE chip SDK has some JNI/C level type stuff to enable keyboard HID over GATT.
From this SO post: is there any ble api in Ndk
It looks like lower level Bluetooth access is available but invocation may vary on a chip to chip basis.
Given that Android allows for a number of different chip vendors, the author of the app you cite may have some of such SDK based code installed but may not work on all devices.
One open source(ish) implementation that I've found but not tested is:
https://github.com/kshoji/BLE-HID-Peripheral-for-Android
which appears to have a min SDK of 21. However the 'secret sauce' is still 'hidden' in an AAR in the author's lib directory.
So what else can be done?
Find out what Bluetooth Low Energy chipset your device uses. I don't have access to a LGV20 but perhaps something in the logcat will point to a manufacturer.
Once you know the manufacturer and chipset series, see if you can get access to that chipset makers particular Bluetooth Low Energy SDK for any sample code/modules which can enable HID over GATT.
Write app with such code/module with the understanding that you may need to deal with JNI/C.

Generic linux usb permission rule

I am trying to use Java4USB to gather usb devices info on Raspian. My java app runs under its own user, but has no access to usb devices.
I have seen that adding udev rules can do the trick, but...
How can I allow usb access to all usb devices to my app, without changing access rules for other users ?

how to write android program to detect all the Devices connected in a WiFi network? [duplicate]

I am developing an app in which I need to scans WiFi network and display the list of all connected devices.
Allow a use to tap on a device and the app should show all the hardware info of that particular device. Here by hardware I mean - RAM, Storage Media, Storage Capacity, Device Name, Device IP address, etc.
Now this device can be anything like xbox, a laptop with Linux/Windows, mobile phone like iPhone or any Andorid based smart phone or even a printer.
How can I scans WiFi network and query/detect all the devices attached to it?
What are the protocols that I need to use to get list of hardware in a particular device irrespective of the OS running on it?
Check requestPeers of
WifiP2pManager
As per documents it seeks
PeerListListener which returns WifiP2pDeviceList carrying list of WifiP2pDevice which carries deviceAddress, deviceName, primaryDeviceType, secondaryDeviceType, status and other attributes.
Maybe the Network Discovery github project could help you. It lists all users connected to WiFi with IP and MAC addresses and gathers even some information like open ports, device name, ping, etc.
Hope it helps
In my opinion, you can use Wi-Fi Peer-to-Peer
https://developer.android.com/guide/topics/connectivity/wifip2p.html
"Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point (Android's Wi-Fi P2P framework complies with the Wi-Fi Alliance's Wi-Fi Directâ„¢ certification program). Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi P2P, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application."
Blockquote

How do I get the data from a serial GPS connected to my android device?

I am working on a project where I have to connect my android device (LG G3) to a GPS with usb. I can't figure out how I can get the data out of my GPS and I do not want to use the device it's internall GPS because I need to be pretty accurate.
If you want to program your own USB GPS to Android, you can take a look at USB Host
According to USB Host documentation:
When your Android-powered device is in USB host mode, it acts as the
USB host, powers the bus, and enumerates connected USB devices. USB
host mode is supported in Android 3.1 and higher.
To use an USB external GPS you have to enable Android developer Options on your phone and enable using mock locations so the service (your one or a third party one) can send locations to the system "bypassing" the internal location system.
There are several apps that can transfer GPS data to your application (take a look at Your are here GPS for example. This app also lets you wath NMEA data).

How to write files from Android to PC and back?

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.

Categories