I have a custom built Android device which is Single Board Computer with a display unit. It has Android 4.1 installed on it and has a USB port. Within an app I created an sqlite database. I want to transfer the database to a usb flash drive using the aforementioned USB port. I understand Android documentation enough to be able to establish a connection between USB host and Accessory. I am able to detect my flash drive using an intent filter.
The following is a code snippet to transfer a byte array using USB classes.
private Byte[] bytes
private static int TIMEOUT = 0;
private boolean forceClaim = true;
...
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
Here a bytearray is transferred using the USB interface. I want to be able to transfer an sqlite database using the same. Is that possible? How can I do it?
As is documented in several other questions here, Android APIs support only raw transfers, so you would have to implement an entire filesystem in your app.
Since your device is custom, you would be better off creating a Linux-level daemon or (bulletproof!) setuid tool to mount the USB drive at operating system level, and leverage the filesystem code already present in the Lunux kernel. Then you can simply perform normal file operations to it. You might even be able to modify Android's vold to do this - because you control the Android install, you have this class of options which a typical 3rd party developer targeting locked down phones does not.
Related
I am new to android studios and I have the task to develop an app which transfers data from an app (Acceleration sensor data - i have created this app already which shows the data) to matlab (on the pc).
I don't really know how I should do this. I've experimented a bit with bluetooth apps, but I don't have a clue how to connect to Matlab.
I would be greatful for your help.
Thanks in advance,
Annika
Unfortunately I can not speak to the android side of things, but MatLab can connect to generic devices with the UART interface, which is fairly low level.
The process with some microprocessors that I am using is to connect the device to the PC, and then note the Outgoing com port.
(In windows 10, these can be found in Bluetooth settings -> More Bluetooth options)
Then you can use
s = serial('COM<what you found in settings>');
s.Baudrate=115200;
s.InputBufferSize = 100;
fopen(s{i});
serials = instrfindall;
to open an connection. The critical command is serial, the other parameters depend on your device/ configuration. Sometimes there can be issues, in which case one options is to build a loop that tries again until it works.
You then collect the data sent via UART via
flushinput(serials);
temp = fscanf(serials,'%s');
and then split the string. If data is sent continuously, you wrap this into a while loop.
After you are done, you can clean up via
fclose(s{i});
delete(instrfind)
instrreset
It should be noted, that establishing a connection takes longer, the more enabled COM ports there are. So it might be worth disabling all those you don't need.
For more specific things matlab can do, check out What Is the MATLAB Serial Port Interface
I am trying to send some data from one Android phone(Nexus 5), which is working as Usb Host, to Android tablet(Samsung Tab 4).
I am using bulkTransfer() method to transfer data from Nexus 5, which is returning the length of data that has been transferred successfully.
My question is How can I verify on the other end (at Samsung Tab), that yes I am getting the data that I had transferred from Nexus 5 ?
Here is some code snippet of transferring data
usbDeviceConnection = manager.openDevice(device);
boolean flag = usbDeviceConnection.claimInterface(usbInterface, true);
String text = textValue.getText().toString();
byteArray = text.getBytes();
// data transfer
int a = usbDeviceConnection.bulkTransfer(endpointOut, byteArray, byteArray.length, 0);
if(a>0){
textInfo.setText("Data has been transferred successfully..."
+"\n"+"Length of data: "+a
);
Please help me to do this task...!
After digging the Usb Host API I have found something that the other end (Samsung Tab) can work in Accessory mode and we can access the Usb Host(Nexus 5) by using the "android.hardware.usb.accessory" library.
Since the android phone who can support the Usb Host feature for example like Nexus 5 can access the device connected to it using "android.hardware.usb.host" library.
The other one for example Samsung Tab(It must support AOAP Protocol ) who does not support the Usb Host feature will access the device connected to it using "android.hardware.usb.accessory" library.
To understand The Usb Host feature you can open the link
http://developer.android.com/guide/topics/connectivity/usb/host.html
To understand The Usb Accessory feature you can open the link
http://developer.android.com/guide/topics/connectivity/usb/accessory.html
I am trying to receive data (strings) from a Bluetooth enabled device whose MAC_ID is known.
I have searched many examples ,but each article is pointing to Bluetooth Chat example, I think in Bluetooth Chat example, application need to be installed on both the devices for them to be connected and exchange strings.Correct me if I am wrong.
But I need to install application only on Receiver device.I have tried installing the application only on one device and tried connecting to the sender device, without success.
Bluetooth is a peer to peer protocol where you need to have application running on both sides. Hence if you want to exchange data very good example would be Bluetooth chat. If you want to download or transfer a file you should either implement obex or FTP profile based applications.
Yes you need to deploy an application on both sides. If you are really restricted in a way that you only can deploy on one side, you have to figure out which standard protocols/bluetooth profiles the other side is capable of. You can figure that out by doing a SDP lookup. For a device, you will then get a list of UUIDs identifying these services. See the bluetooth spec for well known UUIDs. As #7383 pointed out, your are most probably looking for OBEX or FTP.
If you can deploy on both sides, you can write your own app using Blaubot (disclaimer: I wrote it). A simple Blaubot program would do this:
UUID MY_UUID = UUID.fromString("33bb1246-1472-11e5-b60b-1697f925ec7b");
// onCreate() or in a service, we create a blaubot instance
// using Bluetooth to form a network and Bluetooth + NFC to find devices
IBlaubotDevice ownDevice = new BlaubotDevice();
BlaubotUUIDSet uuidSet = new BlaubotUUIDSet(MY_UUID);
BlaubotBluetoothAdapter bluetoothAdapter = new BlaubotBluetoothAdapter(uuidSet, ownDevice);
BlaubotNFCBeacon nfcBeacon = new BlaubotNFCBeacon();
BlaubotBluetoothBeacon bluetoothBeacon = new BlaubotBluetoothBeacon();
this.mBlaubot = BlaubotAndroidFactory.createBlaubot(MY_UUID, ownDevice, adapter, nfcBeacon, bluetoothBeacon);
// start and wait until connected
this.mBlaubot.startBlaubot();
// create a channel and send your file
IBlaubotChannel fileChannel = this.mBlaubot.createChannel(1);
// convert your file to its bytes
File yourFile = // ... however you get it
byte[] fileBytes = ...// ... see http://stackoverflow.com/questions/858980/file-to-byte-in-java
// send it to all connected devices
fileChannel.publish(fileBytes, true);
// to receive it on the other device, do this:
// subscribe to the channel
fileChannel.subscribe(new IBlaubotMessageListener() {
#Override
public void onMessage(BlaubotMessage message) {
// extract your bytes from the message
byte[] fileBytes = message.getPayload();
// .. do something useful or write it to a file again
// to write it to a file
File file = new File(yourFilePath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(fileBytes);
bos.flush();
bos.close();
}
});
This should be all you need. To allow the devices to connect, you have to pair them before or use NFC (just hold them together, when Blaubot is started). If you go with Blaubot let me know if you have problems that can't be solved with the documentation or the android quickstart guide.
I can only guess what your actual scenario looks like. If you have two android phones, this should work. If that is not the case, you should add more informations about the involved devices. Are we really talking about (classic) Bluetooth connections or are you trying to get data from a Bluetooth Low Energy device?
In this case the famous chat example will not help you either.
Can not find good solution of how to copy files from android device to USB drive. I know that android has API's for that since 3.1 version. However docs are not very clear, where I can use that APIs. Is there someone who has experience on this area? I registered broadcast receiver and after allowing usb connection to app, just wanted to transfer some bytes to USB storage like below:
Set<String> devices = mUsbManager.getDeviceList().keySet();
Object[] array = devices.toArray();
String text = array[0].toString();
byte [] s="hello".getBytes();
UsbDeviceConnection connection=mUsbManager.openDevice(mUsbManager.getDeviceList().get(text));
UsbEndpoint usbEndpoint=mUsbManager.getDeviceList().get(text).getInterface(0).getEndpoint(0);
connection.bulkTransfer(usbEndpoint,s,s.length,10000);
However, I did not get what I expect. Could someone help, please?
Thanks in advance
Found solution on this link: https://github.com/mjdev/libaums. You need to write your file system to interact with the usb drive
I am trying to communicate with USB device from Android-based smartphone via OTG. I was able to communicate with my device using Android USB Host API. The problem of USB Host API solution is performance (single bulk transfer bounded by 16384 bytes).
The libusb can perform larger requests and now I am trying to integrate it using Android NDK. I succeeded to compile libusb sources for Android and even initUSB(), but libusb_open(dev, &dev_handle) returns -3 (Access denied).
How can I pass the file descriptor
int fd = connection.getFileDescriptor()
to libusb after getting USB_PERMISSION under Android USB Host API and get USB device access under libusb?
This is what you are looking for.
https://github.com/kuldeepdhaka/libusb/tree/android-open2
just compile it and drop it in. :)
see the "How To for Android" section for full usage.
i made all the required modification to libusb (and im also using it).
It has SELinux fix for "Android 5.0"+ too.
See also https://github.com/libusb/libusb/wiki/Android which now discusses android a little bit. Here is the quote from the proposed readme change (2021-02):
Runtime Permissions:
--------------------
The Runtime Permissions on Android can be transfered from Java to Native over the following approach:
Java:
// obtaining the Usb Permissions over the android.hardware.usb.UsbManager class
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice usbDevice : deviceList.values()) { usbManager.requestPermission(usbDevice, mPermissionIntent); }
// get the native FileDescriptor of the UsbDevice and transfer it to Native over JNI or JNA
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(camDevice);
int fileDescriptor = usbDeviceConnection.getFileDescriptor();
// JNA sample method:
JNA.INSTANCE.set_the_native_Descriptor(fileDescriptor);
Native:
// Initialize LibUsb on Android
set_the_native_Descriptor(int fileDescriptor) {
libusb_context *ctx;
libusb_device_handle *devh;
libusb_set_option(&ctx, LIBUSB_OPTION_WEAK_AUTHORITY, NULL); // important for Android
libusb_init(&ctx);
libusb_wrap_sys_device(NULL, (intptr_t)fileDescriptor, &devh);
// From this point you can regulary use all LibUsb functions as usual.
}