Transfer Data between two android phones using Usb host Api - android

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

Related

Android only: C# .net-MAUI, not Xamarin(!), sending UDP?

Situation:
VS 2022, 17.0.4
Maui App,
net6.0-android
AndroidManifest.xml contains also:
android.permission.INTERNET
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
Connected mobile Phone:
Samsung SM-G960F (Android 10.0 - API 29)
OS: Windows 11, latest patch.
All firewalls are down (for testing purpose only!)
While debugging the develop computer is only connected to a Wifi network; computers ethernet card is disabled.
Mobile phone is connected to this dev computer via USB cable (to be able to debug) and to the same Wifi network as the computer.
App starts and works fine, app can be debugged. No issue at all - except:
After the application is fully initialized and ready to accept user interactions -> Click on button -> Desired method is called -> Code is worked out -> The code should make a simple UDP call but it does not (or the packet does not reach the UDP listener due to missing configuration?).
The UDP receiver works fine and is capable to receive UDP packets.
My mobile phone and the UDP receiver app are using the same port.
I read/found already that in the previous cross-platform framework, means “Xamarin (Android SDK 12)”, some permissions must be set (I did, see above) and that the multicastlock must be set over the WifiManager …
I tried this in my MAUI app. But could not find anything guiding me nor figured it out by myself.
My MAUI sending code:
var dataToBeSend = "What ever ...";
var data = Encoding.UTF8.GetBytes(dataToBeSend);
var UdpClient = new UdpClient();
// UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
UdpClient.ExclusiveAddressUse = false;
UdpClient.EnableBroadcast = true;
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Parse("255.255.255.255"), BroadcastPort));
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Any, BroadcastPort));
UdpClient.Send(data, data.Length, "255.255.255.255", BroadcastPort);
As said: very easy and straight forward.
Notice that I also tried binding UDP code …
So please can someone be so kind to guide me or give me a hint?
Thank you very much in advance!
ANSWER:
After two days I found a solution - and would like to share it because may be it helps someone else.
The code to make the UDP call msut be placed in a THREAD (not task!)
codesnippet:
var communication = new Communication();
var udpThread = new Thread(new ThreadStart(communication.FireUDPCall));
udpThread.Start();
The firewalls can stay turned on / active!

How to access single USB devices of a Composite USB device?

I would like to send data from my Windows computer to my Android Mobile.
For this, I need to activate the Accessory mode of the Android device and the USB Host mode on the Windows device.
On my Windows computer, I have a USB Composite device for the Android Mobile. This Composite USB device bundles several interfaces: Enumeration of USB Composite Devices.
Unfortunately, I can't find information how I can access the single devices of a Composite device.
I want to get a device id / path, which I can open with CreateFile to use the created HANDLE for opening a WinUsb handle with WinUsb_Initialize.
But if I try to open a Composite USB device with CreateFile, I get a ERROR_NOT_ENOUGH_MEMORY result.
I'm using this code:
_deviceHandle = CreateFile(
deviceId, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_NONE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
... with the filename "\?\USB#VID_04E8&PID_6864#RF8NB0NMT0X#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"
It's a GUID_DEVINTERFACE_USB_DEVICE device id for a Samsung Galaxy mobile with enabled USB debugging.
As the driver Windows uses ssudbus2.sys, Version 2.17.16.0 (2021-09-14) from Samsung Electronics Co. Ltd.
The app MyPhoneExplorer can access to my mobile. So it a solution without a special driver must be possible.
How can I get this device id / path of the single USB devices inside a Composite USB device?
The filename you are using represents the overall USB device; it doesn't represent any particular instance. A filename that represents interface will have something like &mi_01 right after the product name, where 1 is the 0-based interface number.
You might be able to just insert the appropriate &mi_xx string into your filename at the appropriate place and get it work. I think you'd also need to modify the GUID at the end of the string, which is the device interface GUID.
The more standard way to find the filename in code is to use the SetupAPI and the configuration manager (CM) API to iterate through all the child devices of your USB device. Look for a child whose device instance ID (retrieved with CM_Get_Device_ID) contains MI_xx where xx is the two-digit interface number you are looking for.
It takes a lot of effort to write up this code in standalone form, and test it, and debug it, so I will not be presenting you with a working code example. Instead, I encourage you to look at the working code in the get_interface_composite function of libusbp which does what you need to do:
https://github.com/pololu/libusbp/blob/759f48d/src/windows/interface_windows.c#L86
There are some more steps to get the path of that device node. And then the code that actually calls CreateFile and WinUsb_Initialize is here:
https://github.com/pololu/libusbp/blob/759f48d/src/windows/generic_handle_windows.c#L56-L77

HID two ways communication between Arduino and Android (controlTransfert)

I want to have a 2 two way (in and out) HID communication between a leonardo and an Android tablet (acting as a host). On the leonardo, I use Nicohood HID project
I have successfully managed to make the code work on Linux, Windows and MacOs, so it is possible.
Using a pure android implementation, I can only receive data from the leonardo but not send to it...
From what I have seen in the descriptor, the data are sent from the device (IN direction) using a specific endpoint. For the other way (OUT, to the device), we must use the control endpoint.
Nevertheless, it doesn't work on android. Here is the code I used :
UsbDeviceConnection mUsbConnection = mUsbManager.openDevice(myDevice);
byte data[] = {7, 11, 13, 17};
int resOut = mUsbConnection.controlTransfer(0x21, 9, 200, 2, data, data.length, 50);
// => resOut value is always -1 (didn't succeed to communicate)
For the parameters, I used the answers to this question: Using Android to Communicate with a USB HID Device and looked at libusb / nicohood hid communication examples on linux...
Out of luck, I tried to change the value from "200" to "0" or the index from 0 to 1 or 2, but I don't really know what they mean, and it didn't really help...
How can I send data to the device using the control endpoint with android ?
PS : I can read HID messages from the device, so it is not a permission issue.

Android transfer sqlite database to USB flash drive from sdcard programatically

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.

Android Open Accessory Protocol using javax.usb

I'm working on getting any existing device communicating with Android devices. The device uses javax.usb (which is built on top of libusb) for usb communication and I am attempting to implement the Android Open Accessory Protocol using it. The device currently recognizes the android device and registers the android device with my application. However when I try to:
Attempt to Start in Accessory Mode - Send a 51 control request ("Get Protocol") to figure out if the device supports the Android accessory protocol.
I get a UsbStallException and my app dies. I know very little when it comes to usb from what I have been reading this should work.
Here's the relevant code:
byte req = (byte) (0x80 | 0x40);
UsbControlIrp irp = usbDev.createUsbControlIrp(req, (byte)51, (short)0, (short)0);
usbDev.syncSubmit(irp);
Any insights as to what I'm doing wrong would be appreciated.
PS: the device I'm running this on uses a 1.4 JVM so any solutions will be constrained by that

Categories

Resources