RequestRouteToHost doesn't work - android

I need to check if the route to host exists for my Android app. Here is my code:
private void ensureRouteToHost(String proxyAddr) throws IOException
{
ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
int inetAddr;
inetAddr = lookupHost(proxyAddr); // Return -938825536 for IP 192.168.10.200
Log.d(TAG, "host for proxy is " + inetAddr);
if (inetAddr == -1)
{
Log.e(TAG, "cannot establish route for " + proxyAddr + ": unkown host");
throw new IOException("Cannot establish route for " + proxyAddr + ": Unknown host");
}
else
{
int[] apnTypes = new int[] {ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_MOBILE_MMS, ConnectivityManager.TYPE_MOBILE_DUN, ConnectivityManager.TYPE_MOBILE_HIPRI, ConnectivityManager.TYPE_MOBILE_SUPL};
for (int i=0; i<apnTypes.length; i++)
{
if (connMgr.requestRouteToHost(apnTypes[i], inetAddr))
{
Log.d(TAG, "route to host requested");
return;
}
}
Log.e(TAG, "unable to request route to host");
throw new IOException("Cannot establish route to proxy " + inetAddr);
}
}
public static int lookupHost(String hostname)
{
hostname = hostname.substring(0, hostname.indexOf(":") > 0 ? hostname.indexOf(":") : hostname.length());
String result = "";
String[] array = hostname.split("\\.");
if (array.length != 4) return -1;
int[] hexArray = new int[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
hexArray[0] = Integer.parseInt(array[0]) / 16;
hexArray[1] = Integer.parseInt(array[0]) % 16;
hexArray[2] = Integer.parseInt(array[1]) / 16;
hexArray[3] = Integer.parseInt(array[1]) % 16;
hexArray[4] = Integer.parseInt(array[2]) / 16;
hexArray[5] = Integer.parseInt(array[2]) % 16;
hexArray[6] = Integer.parseInt(array[3]) / 16;
hexArray[7] = Integer.parseInt(array[3]) % 16;
for (int i=0; i<8; i++)
{
result += Integer.toHexString( hexArray[i] );
}
return Long.valueOf(Long.parseLong(result, 16)).intValue();
}
It works perfectly on most devices but, and that's really weird, it doesn't work on Nexus S Europe. I've tried a couple of Nexus and I always got that problem.
The problem is located in the ensureRouteToHost method when I call connMgr.requestRouteToHost(apnTypes[i], inetAddr). It always returns false, whatever I put in. My plan is to check if the IP 192.168.10.200 is reachable or not for my MMS App. This doesn't work neither for public IP such as stackoverflow.com (69.59.197.21 or 1161544981 as int).
So, do you have an idea why this isn't working on some devices ?
Thanks for reading my thread.

You need to bring up those interfaces, e.g. HIPRI, first. The way to do that is described here.
However I find that whilst this makes both interfaces come up, and requestRouteToHost return true (at least, once the network is actually up), the routing still appears to have no effect.
This is as tested on a number of different phones.
Please let me know if you succeed with this.

Problem solved.
It can't find the route to IP with the wifi on. Simplest way is to disable wifi, do your stuff and then enable wifi.
Here is the code I used:
// Disable wifi if it's active
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
{
mWasWifiActive = true;
wifiManager.setWifiEnabled(false);
Log.e(TAG, "Wifi was enabled, now Off.");
}
// Do stuff here
// Re-enable wifi if it was active before routing
if (mWasWifiActive)
{
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
Log.e(TAG, "Wifi is back online.");
}

Related

Android wifiManager.enableNetwork(j.networkId, true) is switching back to previous network

I have to show list of access points (with no internet) and then connect to a selected network. I was able to get all available network network with wifiManager.getScanResults() then i have filtered my preferred open network and configured with wifiManager.addNetwork(conf);then i am trying to connect that preferred network with wifiManager.enableNetwork(j.networkId, true) It connecting to That network and again switching back to previous network.
1) the selected network gets connected
2) then its disconnected after couple of seconds
3 )wifi connects to the previously connected network
The behavior is only observed in Android devices with OS above 7.0. Marsh mellow and below device it is not switching network
here is my code
1 ) scanning and configuring
if (netType == ConnectivityManager.TYPE_WIFI) {
//wifiManager =
(WifiManager)this.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifiManager.getScanResults();
if (!results.equals(null)) {
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
if(result.SSID.toLowerCase().startsWith("XXXX.")) {
list.add(result.SSID);
conf =new WifiConfiguration();
conf.SSID = "\"" + result.SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
}
}
}
2) to connect preferred network
if (j.SSID != null && j.SSID.equals("\"" + XXXXXX+ "\"")) {
Log.d("Config", "" + j.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(j.networkId, true);
wifiManager.reconnect();
break;
}

Reading data from Arduino UNO R3 kit

I'm trying to read the data already stored by me in the Arduino kit, I'm using the physicaloid library to achieve this. I tested the kit (reading data) by connecting it to my PC using the Type B USB cable provided by Arduino itself and using Tera Term. The data begins to transfer after I press '#' on the keyboard (specific to our implementation).
But when I connect it my Android tablet and use the test project by physicaloid to open a device and start communicating, every time I click 'open' it shows a Toast saying it cannot open. I give permission to access the USB device every time it prompts me. Here is the sample program which I had created to read the data:
if(mPhysicaloid.open()){
Toast.makeText(getBaseContext(), "communicating", Toast.LENGTH_SHORT).show();
String signalToStart = new String("#");
byte[] bufToWrite = signalToStart.getBytes();
mPhysicaloid.write(bufToWrite, bufToWrite.length);
byte[] buf = new byte[255];
mPhysicaloid.read(buf);
String data = new String(buf);
tvResult.setText(data);
mPhysicaloid.close();
}
else
Toast.makeText(getBaseContext(), "no communication with device", Toast.LENGTH_LONG).show();
Now here's what I want to know about the data coming from the Arduino USB cable: is it in the RS232 format where the Android device is not able to understand (I don't know, I may be making a blunder here by asking this data format) or is it in the USB data format that is suitable for the Android device to understand? Please help, I have searched over this the whole day. What can I do to open the device and communicate?
I finally got the idea of reading the data from serial USB device. So I thought I'd share it:
First, get all the USB devices attached (if more than one) and get a suitable interface and search for endpoints to communicate with. While initializing the USB device make sure you consider the USB device which you really want to communicate with. You can do that by considering product id and Vendor id.
The code for doing the above described..
private boolean searchEndPoint() {
usbInterface = null;//class level variables, declare these.
endpointOut = null;
endpointIn = null;
Log.d("USB","Searching device and endpoints...");
if (device == null) {
usbDevices = usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = usbDevices.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice tempDevice = deviceIterator.next();
/**Search device for targetVendorID(class level variables[vendorId = SOME_NUMBER and productId=SOME_NUMBER] which u can find) and targetProductID.*/
if (tempDevice .getVendorId() == vendorId) {
if (tempDevice .getProductId() == productId) {
device = tempDevice ;
}
}
}
}
if (device == null){
Log.d("USB","The device with specified VendorId and ProductId not found");
return false;
}
else
Log.d("USB","device found");
/**Search for UsbInterface with Endpoint of USB_ENDPOINT_XFER_BULK,
*and direction USB_DIR_OUT and USB_DIR_IN
*/
try{
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface usbif = device.getInterface(i);
UsbEndpoint tOut = null;
UsbEndpoint tIn = null;
int tEndpointCnt = usbif.getEndpointCount();
if (tEndpointCnt >= 2) {
for (int j = 0; j < tEndpointCnt; j++) {
if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) {
tOut = usbif.getEndpoint(j);
} else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) {
tIn = usbif.getEndpoint(j);
}
}
}
if (tOut != null && tIn != null) {
/** This interface have both USB_DIR_OUT
* And USB_DIR_IN of USB_ENDPOINT_XFER_BULK
*/
usbInterface = usbif;
endpointOut = tOut;
endpointIn = tIn;
}
}
}
if (usbInterface == null) {
Log.d("USB","No suitable interface found!");
return false;
} else {
Log.d("USB","Suitable interface found!");
return true;
}
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
Now you have a device, USB interface, and endpoints ready for communication. Now it's time to establish a connection between your Android device and USB device.
Below is the code for this (and checking whether the connection is up and communicating):
private boolean checkUsbCOMM() {
/**Value for setting request, on the USB connection.*/
final int RQSID_SET_CONTROL_LINE_STATE = 0x22;
boolean success = false;
Log.d("USB","Checking USB Device for communication: ");
try{
Boolean permitToRead = SUSBS_usbManager.hasPermission(SUSBS_device);
if (permitToRead) {
//class level variable(connection, usbManager : declare it)
connection = usbManager.openDevice(device);
if (connection != null) {
connection.claimInterface(usbInterface, true);
int usbResult;
usbResult = connection.controlTransfer(0x21, //requestType
RQSID_SET_CONTROL_LINE_STATE, //SET_CONTROL_LINE_STATE(request)
0, //value
0, //index
null, //buffer
0, //length
500); //timeout = 500ms
Log.i("USB","controlTransfer(SET_CONTROL_LINE_STATE)[must be 0 or greater than 0]: "+usbResult);
if(usbResult >= 0)
success = true;
else
success = false;
}
}
else {
/**If permission is not there then ask for permission*/
usbManager.requestPermission(device, mPermissionIntent);
Log.d("USB","Requesting Permission to access USB Device: ");
}
return success;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
Voila, the USB device is now able to communicate. So let's read using a separate thread:
if(device!=null){
Thread readerThread = new Thread(){
public void run(){
int usbResult = -1000;
int totalBytes = 0;
StringBuffer sb = new StringBuffer();
String usbReadResult=null;
byte[] bytesIn ;
try {
while(true){
/**Reading data until there is no more data to receive from USB device.*/
bytesIn = new byte[endpointIn.getMaxPacketSize()];
usbResult = connection.bulkTransfer(endpointIn,
bytesIn, bytesIn.length, 500);
/**The data read during each bulk transfer is logged*/
Log.i("USB","data-length/read: "+usbResult);
/**The USB result is negative when there is failure in reading or
* when there is no more data to be read[That is :
* The USB device stops transmitting data]*/
if(usbResult < 0){
Log.d("USB","Breaking out from while, usb result is -1");
break;
}
/**Total bytes read from the USB device*/
totalBytes = totalBytes+usbResult;
Log.i("USB","TotalBytes read: "+totalBytes);
for(byte b: bytesIn){
if(b == 0 )
break;
else{
sb.append((char) b);
}
}
}
/**Converting byte data into characters*/
usbReadResult = new String(sb);
Log.d("USB","The result: "+usbReadResult);
//usbResult holds the data read.
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
/**Starting thread to read data from USB.*/
SUSBS_readerThread.start();
SUSBS_readerThread.join();
}
For permission, make sure you add a PendingIntent as well add the permission to your manifest.
AndroidManifest : <uses-feature android:name="android.hardware.usb.host" />
PendingIntent:
private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this,
0, new Intent(ACTION_USB_PERMISSION), 0);
/**Setting up the Broadcast receiver to request a permission to allow the APP to access the USB device*/
IntentFilter filterPermission = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filterPermission);

ADK: device gets stuck when reading from the ADK

I have problems when I when to send messages via USB from the board to the devicersa.
Hardware:
Arduino ADK 2011
Samsung Galaxy S3, Android 4.1.2
The problem is the read method in the Android app never terminates and makes the thread get stuck:
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
mAccessory = accessory;
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
Log.d(TAG, "openAccessory(): FileDescriptor instanciated. valid " + fd.valid());
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
new Thread(null, new Runnable() {
#Override
public void run() {
int ret = 0;
byte[] buffer = new byte[255];
while (ret >= 0 && mInputStream != null) {
try {
// never terminates
ret = mInputStream.read(buffer);
} catch (IOException e) {
Log.e(TAG, "openAccessory(): Could not read inputStream: " + e);
e.printStackTrace();
break;
}
} ...
The connection works fine since I use the special USB-library. When I connect the device the app opens automatically very well. But with logs I see it never passes the read command. Also the Arduinio monitor says that:
Device addressed... Requesting device descriptor.
found possible device. swithcing to serial mode
device supports protcol 1 or above
found android acessory device
config desc
interface desc
interface desc
2
4
Sending message...
Done
disconnect
The ADK sends messages, to the device in the loop (once):
sntmsg[0] = COMMAND_TEXT;
sntmsg[1] = TARGET_DEFAULT;
sntmsg[2] = 25;
for (int i = 0; i < 25; i++) {
sntmsg[3 + i] = hello[i];
}
// schreiben (buffer, length)
Serial.println("Sending message...");
acc.write(sntmsg, 3 + 25);
Serial.println("Done");
done = true;
delay(250);
Now I figured out the problem might be the disconnect. Immedeately after running through the first loop in the Arduiino code it prints disconnect to the monitor. The code in the libraries of AndroidAccessory.cpp is:
bool AndroidAccessory::isConnected(void)
{
USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff;
byte err;
max.Task();
usb.Task();
if (!connected &&
usb.getUsbTaskState() >= USB_STATE_CONFIGURING &&
usb.getUsbTaskState() != USB_STATE_RUNNING) {
Serial.print("\nDevice addressed... ");
Serial.print("Requesting device descriptor.\n");
err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
if (err) {
Serial.print("\nDevice descriptor cannot be retrieved. Trying again\n");
return false;
}
if (isAccessoryDevice(devDesc)) {
Serial.print("found android acessory device\n");
connected = configureAndroid();
} else {
Serial.print("found possible device. swithcing to serial mode\n");
switchDevice(1);
}
} else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
if (connected)
Serial.println("disconnect\n");
connected = false;
}
return connected;
}
So in the second loop this method returns false, even though the smartphone is still connected via usb. Do you know why it thinks it is dosconnected after the first loop iteration?
Thanks,
FL

Using Android to Communicate with a USB HID Device

I am new to USB and to Android so please forgive me if I don't explain myself clearly.
I have a USB HID device that I can communicate with in Windows. I am trying to establish communication using an Acer Iconia A500 tablet running Android 3.1.
I am able to find the device, enumerate it, get its only available interface, get the only available endpoint (0), and determine what type of endpoint it is (transfer interrupt from device to host).
My understanding of the USB spec is that all HID devices are required at a munimum to have a control endpoint (Endpoint 0) and an interrupt IN endpoint. But it seems that endpoint 0 here is the interrupt In endpoint, not the control endpoint.
Yet in order for the device to enumerate it must successfully transfer its descriptor data across the control endpoint. I deduce that the control endpoint therefore must be getting found (and used) because the host does, in fact, enumerate the device.
This is as far as I am able to proceed, as stated above, the only interface/endpoint presented to me at the application level is an interrupt type going from device to host. No endpoint available to my app going from host to device, interrupt or control. So the device waits to be told what to do and the host waits for something to happen in the device. Not very stimulating.
Bear in mind that this device responds properly when connected to Windows, e.g. I am able to send a report containing 13 bytes of data that causes the device to light an LED. So it seems to be complying with the USB HID spec. As an act of desperation I have tried using this one endpoint as both a control endpoint and as a interrupt OUT endpoint, using controltransfer() and UsbRequest() to submit the data to the device, no response in either case.
So my question is: "The control transfer endpoint is (?) being used to set up the device, why am I not able to find & use it?"
Thanks for any insight, below is the relevant code, I can include the rest in its entirety if needed:
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnectionRead;
private UsbDeviceConnection mConnectionWrite;
private UsbEndpoint mEndpointRead;
private UsbEndpoint mEndpointWrite;
// check for existing devices
for (UsbDevice device : mUsbManager.getDeviceList().values())
{
//Need to filter for my device when other HIDs are also connected, but for now...
String devName = device.getDeviceName();
if (DEBUG == 1){
Toast.makeText(UsbHidDeviceTesterActivity.this, "My device got connected: " + devName, Toast.LENGTH_LONG).show();
}
//mDevice = device;
setHIDDevice(device);
}
private boolean setHIDDevice(UsbDevice device)
{
UsbInterface usbInterfaceRead = null;
UsbInterface usbInterfaceWrite = null;
UsbEndpoint ep1 = null;
UsbEndpoint ep2 = null;
boolean UsingSingleInterface = true;
mDevice = device;
//This HID device is using a single interface
if (UsingSingleInterface)
{
//usbInterfaceRead = device.getInterface(0x00);//only 1 EP on this interface
usbInterfaceRead = findInterface(device);
//Try getting an interface at next index
//usbInterfaceWrite = device.getInterface(0x01);//throws exception
// Try using the same interface for reading and writing
usbInterfaceWrite = usbInterfaceRead;
int endPointCount = usbInterfaceWrite.getEndpointCount();
if (DEBUG == 2)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Endpoints: " + endPointCount, Toast.LENGTH_LONG).show();
//Toast.makeText(UsbHidDeviceTesterActivity.this, "Interface: " + usbInterfaceRead, Toast.LENGTH_LONG).show();
}
if (endPointCount == 1)//only getting 1 endpoint
{
ep1 = usbInterfaceRead.getEndpoint(0);
//As an act of desperation try equating ep2 to this read EP, so that we can later attempt to write to it anyway
ep2 = usbInterfaceRead.getEndpoint(0);
}
else if (endPointCount == 2)
{
ep1 = usbInterfaceRead.getEndpoint(0);
ep2 = usbInterfaceRead.getEndpoint(1);
}
}
else // ! UsingSingleInterface
{
usbInterfaceRead = device.getInterface(0x00);
usbInterfaceWrite = device.getInterface(0x01);
if ((usbInterfaceRead.getEndpointCount() == 1) && (usbInterfaceWrite.getEndpointCount() == 1))
{
ep1 = usbInterfaceRead.getEndpoint(0);
ep2 = usbInterfaceWrite.getEndpoint(0);
}
if (DEBUG == 3)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Using Dual Interface", Toast.LENGTH_LONG).show();
}
}
//because ep1 = ep2 this will now not cause a return unless no ep is found at all
if ((ep1 == null) || (ep2 == null))
{
if (DEBUG == 4)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "One EP is null", Toast.LENGTH_LONG).show();
}
return false;
}
// Determine which endpoint is the read, and which is the write
if (ep1.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)//I am getting a return of 3, which is an interrupt transfer
{
if (ep1.getDirection() == UsbConstants.USB_DIR_IN)//I am getting a return of 128, which is a device-to-host endpoint
{
mEndpointRead = ep1;
if (DEBUG == 5)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "EP1 type: " + ep1.getType(), Toast.LENGTH_LONG).show();
}
}
if (ep1.getDirection() == UsbConstants.USB_DIR_OUT)//nope
{
mEndpointWrite = ep1;
if (DEBUG == 6)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "EP1 is a write", Toast.LENGTH_LONG).show();
}
}
}
if (ep2.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
{
if (ep2.getDirection() == UsbConstants.USB_DIR_IN)
{
//Try treating it as a write anyway
//mEndpointRead = ep2;
mEndpointWrite = ep2;
}
else if (ep2.getDirection() == UsbConstants.USB_DIR_OUT)
{
//usbEndpointWrite = ep2;
mEndpointWrite = ep2;
}
}
//check that we should be able to read and write
if ((mEndpointRead == null) || (mEndpointWrite == null))
{
return false;
}
if (device != null)
{
UsbDeviceConnection connection = mUsbManager.openDevice(device);
if (connection != null && connection.claimInterface(usbInterfaceRead, true))
{
Log.d(TAG, "open SUCCESS");
mConnectionRead = connection;
// Start the read thread
//Comment out while desperately attempting to write on this connection/interface
//Thread thread = new Thread(this);
//thread.start();
}
else
{
Log.d(TAG, "open FAIL");
mConnectionRead = null;
}
}
if (UsingSingleInterface)
{
mConnectionWrite = mConnectionRead;
}
else //! UsingSingleInterface
{
mConnectionWrite = mUsbManager.openDevice(device);
mConnectionWrite.claimInterface(usbInterfaceWrite, true);
}
return true;
}
// searches for an interface on the given USB device
private UsbInterface findInterface(UsbDevice device) {
Log.d(TAG, "findInterface " + device);
int count = device.getInterfaceCount();
if (DEBUG == 7)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Interface count: " + count, Toast.LENGTH_LONG).show();
}
for (int i = 0; i < count; i++) {
UsbInterface intf = device.getInterface(i);
String InterfaceInfo = intf.toString();
Log.d(TAG, "Interface: " + InterfaceInfo);
//Class below is 3 for USB_HID
if (intf.getInterfaceClass() == 3 && intf.getInterfaceSubclass() == 0 &&
intf.getInterfaceProtocol() == 0) {
return intf;
}
//....try just returning the interface regardless of class/subclass
//return intf;
}
return null;
}
private boolean sendControlTransfer(byte[] dataToSend)
{
synchronized (this)
{
if (mConnectionRead != null)
{
//byte[] message = new byte[13]; // or 14?
byte[] message = dataToSend;
if (DEBUG == 9)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Sending Control Transfer", Toast.LENGTH_LONG).show();
}
//first field ox21 is bin 00100001 which splits into 0 01 00001 for direction(1bit)/type(2b)/recipient(5b)
//To set direction as 'host to Device' we need 0, To set type to HID we need 11 (3), and for recipient we want 00001
//second field 0x09 is class specific request code, 0x09 is listed as 'reserved for future use'
//third field 0x200 is value
//int transfer = mConnectionRead.controlTransfer(0x21, 0x9, 0x200, 0, message, message.length, 0);
//try with type set to HID
int transfer = mConnectionRead.controlTransfer(0xC1, 0x9, 0x200, 0, message, message.length, 0);
if (DEBUG == 10)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Transfer returned " + transfer, Toast.LENGTH_LONG).show();
}
}
}
return true;
}
private boolean sendInterruptTransfer(byte[] dataToSend)
{
int bufferDataLength = mEndpointWrite.getMaxPacketSize();//The write endpoint is null unless we just copy the read endpoint
if (DEBUG == 12)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Max Packet Size: " + bufferDataLength, Toast.LENGTH_LONG).show();
}
ByteBuffer buffer = ByteBuffer.allocate(bufferDataLength + 1);
UsbRequest request = new UsbRequest();
buffer.put(dataToSend);
request.initialize(mConnectionWrite, mEndpointWrite);
request.queue(buffer, bufferDataLength);
try
{
/* only use requestwait on a read
if (request.equals(mConnectionWrite.requestWait()))
{
return true;
}
*/
}
catch (Exception ex)
{
// An exception has occurred
if (DEBUG == 13)
{
Toast.makeText(UsbHidDeviceTesterActivity.this, "Caught Write Exception", Toast.LENGTH_LONG).show();
}
}
return true;
}
So, I have been researching similar things. I cannot confirm, but what I believe is happening is:
Android does not list the control endpoint when it enumerates it's endpoints. It only lists other endpoints.
A connection to any endpoint can send control transfers to endpoint 0, through the controlTransfer method, which (quoting from the api) "Performs a control transaction on endpoint zero for this device."
So, in your above code, I would use the 0th endpoint as an interrupt input endpoint, but it will still allow for control transfers.
An example of someone using a HID device is the Missle Launcher demo, the device it uses is a HID device with an interrupt endpoint.
You can get a full list of the details of interfaces and endpoint by using the following:
UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext())
{
UsbDevice device = deviceIterator.next();
Log.i(TAG,"Model: " + device.getDeviceName());
Log.i(TAG,"ID: " + device.getDeviceId());
Log.i(TAG,"Class: " + device.getDeviceClass());
Log.i(TAG,"Protocol: " + device.getDeviceProtocol());
Log.i(TAG,"Vendor ID " + device.getVendorId());
Log.i(TAG,"Product ID: " + device.getProductId());
Log.i(TAG,"Interface count: " + device.getInterfaceCount());
Log.i(TAG,"---------------------------------------");
// Get interface details
for (int index = 0; index < device.getInterfaceCount(); index++)
{
UsbInterface mUsbInterface = device.getInterface(index);
Log.i(TAG," ***** *****");
Log.i(TAG," Interface index: " + index);
Log.i(TAG," Interface ID: " + mUsbInterface.getId());
Log.i(TAG," Inteface class: " + mUsbInterface.getInterfaceClass());
Log.i(TAG," Interface protocol: " + mUsbInterface.getInterfaceProtocol());
Log.i(TAG," Endpoint count: " + mUsbInterface.getEndpointCount());
// Get endpoint details
for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)
{
UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);
Log.i(TAG," ++++ ++++ ++++");
Log.i(TAG," Endpoint index: " + epi);
Log.i(TAG," Attributes: " + mEndpoint.getAttributes());
Log.i(TAG," Direction: " + mEndpoint.getDirection());
Log.i(TAG," Number: " + mEndpoint.getEndpointNumber());
Log.i(TAG," Interval: " + mEndpoint.getInterval());
Log.i(TAG," Packet size: " + mEndpoint.getMaxPacketSize());
Log.i(TAG," Type: " + mEndpoint.getType());
}
}
}
Log.i(TAG," No more devices connected.");
}
Control transfer doesn't show any interface descriptor and its endpoint number is 0 by default, for both in and out transfer.
if you have other interfaces the index of those interfaces should start from 0 i.e. default control transfer interface does not count.
So your interface 0 holds the endpoint 1 descriptor. use the UsbEndpoint methods to find the attributes of the endpoint whether it is interrupt type or not. if it is then endpoint type by UsbEndpoint.getType() should return 0x03 and endpoint number by UsbEndpoint.getEndpointNumber() should return 0x81 which is usual value for endpoint 1.
below your code is wrong:
//first field ox21 is bin 00100001 which splits into 0 01 00001 for direction(1bit)/type(2b)/recipient(5b)
//To set direction as 'host to Device' we need 0, **To set type to HID we need 11 (3)**, and for recipient we want 00001
//second field 0x09 is class specific request code, **0x09 is listed as 'reserved for future use'**
//**third field 0x200 is value**
//int transfer = mConnectionRead.controlTransfer(0x21, 0x9, 0x200, 0, message, message.length, 0);
//try with type set to HID
int transfer = mConnectionRead.controlTransfer(0xC1, 0x9, 0x200, 0, message, message.length, 0);
Type 2 bits is used to indicate class specific request, i.e. its value is 01,
0x09 is Hid class specific request SET_REPORT, not reserved.
value is the wValue which is used as Report ID for Hid class, for your case it is probably 0, if you have only one report at you HID descriptor.
and the 4 th parameter is wIndex which should be used to indicate the recipient, for your case it should be 0x01 for interface as recipient.
So your code for control transfer for Read or receive data form device should be:
int transfer = mConnectionRead.controlTransfer(0xA1, 0x01, 0x00, 0x01, message, message.length, 0);
where 0x01 in second parameter is GET_REPORT is Hid calls specific request.
And your code for control transfer for Write or send data to device should be:
int transfer = mConnectionWrite.controlTransfer(0x21, 0x09, 0x00, 0x01, message, message.length, 0);
Since you only have Interrupt IN endpoint 1, Bulk or Interrupt transfer should be like:
int transfer = bulkTransfer (ep1, message, message.length, 0);
to have the interrupt Out endpoint there should be a endpoint descriptor for that at the interface descriptor of firmware of your device.
Maybe it is a late answer or off topic. However, I hope someone will one day find this answer useful.
Github now contains great Android libraries to communicate with custom HID devices:
Mine in Kotlin
in Java
The good thing is that if you are lucky, then you only need to know the device VID:PID and the commands it accepts. You don't need to worry about USB protocol or communication details.
If interested about how USB works, then you can have a closer look at library source code.

Force Android to use 3G when on local area wifi without net access

I have a wifi LAN setup which does not have internet access. Just various other local wifi devices connected to it. The DHCP is configured to not return a gateway or dns server. Only an IP and netmask.
When I connect my android to this wifi AP it connects fine, but all internet connectivity on the phone stops working.
I would expect that since the wifi has no gateway setting that android should realize the internet can't go through that connection and should instead be routed through the 3G connection which is at 5 bars.
I've tried setting a static IP on the android phone as well, but this did not help.
The main reason for this setup is so that the android device can transfer data on this remote network to an internet based server since it can connect to the local devices without issue. However the 3G side is broken once the wifi is setup.
Any thoughts on how to work around this issue?
After a bit of coding and testing I have merged Squonk and this solution. This is the class I have created:
package it.helian.exampleprj.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
public class NetworkUtils {
private static final String TAG_LOG = "ExamplePrj";
Context context;
WifiManager wifiMan = null;
WifiManager.WifiLock wifiLock = null;
public NetworkUtils(Context context) {
super();
this.context = context;
}
/**
* Enable mobile connection for a specific address
* #param context a Context (application or activity)
* #param address the address to enable
* #return true for success, else false
*/
public boolean forceMobileConnectionForAddress(Context context, String address) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == connectivityManager) {
Log.d(TAG_LOG, "ConnectivityManager is null, cannot try to force a mobile connection");
return false;
}
//check if mobile connection is available and connected
State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
Log.d(TAG_LOG, "TYPE_MOBILE_HIPRI network state: " + state);
if (0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING)) {
return true;
}
//activate mobile connection in addition to other connection already activated
int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
Log.d(TAG_LOG, "startUsingNetworkFeature for enableHIPRI result: " + resultInt);
//-1 means errors
// 0 means already enabled
// 1 means enabled
// other values can be returned, because this method is vendor specific
if (-1 == resultInt) {
Log.e(TAG_LOG, "Wrong result of startUsingNetworkFeature, maybe problems");
return false;
}
if (0 == resultInt) {
Log.d(TAG_LOG, "No need to perform additional network settings");
return true;
}
//find the host name to route
String hostName = extractAddressFromUrl(address);
Log.d(TAG_LOG, "Source address: " + address);
Log.d(TAG_LOG, "Destination host address to route: " + hostName);
if (TextUtils.isEmpty(hostName)) hostName = address;
//create a route for the specified address
int hostAddress = lookupHost(hostName);
if (-1 == hostAddress) {
Log.e(TAG_LOG, "Wrong host address transformation, result was -1");
return false;
}
//wait some time needed to connection manager for waking up
try {
for (int counter=0; counter<30; counter++) {
State checkState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
if (0 == checkState.compareTo(State.CONNECTED))
break;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
//nothing to do
}
boolean resultBool = connectivityManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
Log.d(TAG_LOG, "requestRouteToHost result: " + resultBool);
if (!resultBool)
Log.e(TAG_LOG, "Wrong requestRouteToHost result: expected true, but was false");
state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
Log.d(TAG_LOG, "TYPE_MOBILE_HIPRI network state after routing: " + state);
return resultBool;
}
/**
* This method extracts from address the hostname
* #param url eg. http://some.where.com:8080/sync
* #return some.where.com
*/
public String extractAddressFromUrl(String url) {
String urlToProcess = null;
//find protocol
int protocolEndIndex = url.indexOf("://");
if(protocolEndIndex>0) {
urlToProcess = url.substring(protocolEndIndex + 3);
} else {
urlToProcess = url;
}
// If we have port number in the address we strip everything
// after the port number
int pos = urlToProcess.indexOf(':');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
// If we have resource location in the address then we strip
// everything after the '/'
pos = urlToProcess.indexOf('/');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
// If we have ? in the address then we strip
// everything after the '?'
pos = urlToProcess.indexOf('?');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
return urlToProcess;
}
/**
* Transform host name in int value used by {#link ConnectivityManager.requestRouteToHost}
* method
*
* #param hostname
* #return -1 if the host doesn't exists, elsewhere its translation
* to an integer
*/
private int lookupHost(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8 )
| (addrBytes[0] & 0xff);
return addr;
}
#SuppressWarnings("unused")
private int lookupHost2(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8 )
| (addrBytes[0] & 0xff);
return addr;
}
public Boolean disableWifi() {
wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiMan != null) {
wifiLock = wifiMan.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "HelianRCAWifiLock");
}
return wifiMan.setWifiEnabled(false);
}
public Boolean enableWifi() {
Boolean success = false;
if (wifiLock != null && wifiLock.isHeld())
wifiLock.release();
if (wifiMan != null)
success = wifiMan.setWifiEnabled(true);
return success;
}
}
This is the usage:
USAGE CODE
boolean mobileRoutingEnabled = checkMobileInternetRouting();
if(!mobileRoutingEnabled) {
networkUtils.disableWifi();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
networkUtils.forceMobileConnectionForAddress(context, RCA_URL);
if(!mobileRoutingEnabled) {
networkUtils.enableWifi();
}
// This second check is for testing purpose
checkMobileInternetRouting();
return callWebService(RCA_COMPLETE_URL, _plate);
where checkMobileInternetRouting is:
private boolean checkMobileInternetRouting() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
return 0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING);
}
USAGE PROCEDURE
Check if the routing to the host is enabled
If yes go with the communication regardless the wifi is connected or not and execute only points 6 (the point 4 will only check that routing is already enable without executing any rilevant action). Otherwise temporary disables the wifi.
Thread sleep of about 3 seconds for letting the 3g connection comes back
Set the 3g routing to the given url
Enable back the wifi
Now the given url can be called even with a wifi connection without net acces
CONCLUSIONS
This is a bit hacky but works properly. The only problem is that this routing has got a timeout of few seconds (like 20-30) that forces you to execute the entire above procedure once more. Setting this timeout to a higher value would be very good.
Google added some useful methods in Android SDK 21 for this purpose.
You can create NetworkRequest:
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build();
And then you can request such network using ConnectivityManager. For example, you want to be sure that all HTTP requests will be passed through the network with internet access. You can build your Retrofit API in this way:
ApiConfig apiConfig;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
apiConfig = new Retrofit.Builder()
.baseUrl("https://api.imatrix.io/")
.client(new OkHttpClient.Builder()
.socketFactory(network.getSocketFactory())
.build())
.build()
.create(ApiConfig.class);
}
#Override
public void onLost(Network network) {
apiConfig = null;
}
});
Please, mind the thread-safety when you're using such snippet of code.
In addition, I suggest check ConnectivityManager#bindProcessToNetwork and this blog.
ConnectivityManager.NetworkCallback is an empty class and it has several methods.
From code, when you detect there is no connectivity, you could switch off WiFi...
As for a setting, there is none (no good way to check if there really is connectivity universally and reliably). But some phones do just what you describe automatically, like for example my LG P-970.
(Note: Android disconnects from mobile networks when it connects to a WiFi, so there is no way to still be connected to a WiFi but route internet access through mobile, even though Linux can do it (with the ip route ... suite of tools))
I can't guarantee this will work as it's something I only experimented with some time ago. I had a similar need to use 3G (or other mobile network) when the wifi-connected network had no route to the outside world.
The following code should drop the wifi connection in order to allow the mobile network to come in to play. You'll need to do various tests along the way and re-establish the wifi connection again afterwards...
WifiManager wifiMan = null;
WifiManager.WifiLock wifiLock = null;
private Boolean disableWifi() {
wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiMan != null) {
wifiLock = wifiMan.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "MyWifiLock");
}
return wifiMan.setWifiEnabled(false);
}
private Boolean enableWifi() {
Boolean success;
if (wifiLock != null)
wifiLock.release();
if (wifiMan != null)
success = wifiMan.setWifiEnabled(true);
return success;
}
you don't need to code anything. i found an app that do exactly this thing. you can configure to disconnect automatically from the wifi if there is no internet from this connection.
https://play.google.com/store/apps/details?id=com.nLabs.internetconnectivity&hl=en

Categories

Resources