UsbDeviceConnection.requestWait() Issue - External Device Won't Reconnect - android

Some background: I have been trying to refactor this Android app's Usb system. Initially, in order to transfer data between endpoints, they used the UsbDeviceConnection.bulkTransfer() method. I refactored it to use the UsbRequest.initialize(), UsbRequest.queue(), and then UsbDeviceConnection.requestWait() methods. When the Android program is booted on the tablet, my new methods work fine (perhaps even better), but when there is a USB disruption/disconnection from the external device, once reconnected, it won't pick up where it left off. However, their old method, UsbDeviceConnection.bulkTransfer() DOES pick up communication upon reconnect.
Here is the code I refactored, along with the original method:
Original Method [bulkTransfer()] -
byte[] response = new byte[64];
int result = 0;
int tries = 0;
while (result < 1){
if (tries > 5) {
Log.d("PayloadTask", "PayloadTask Timed Out.");
return null;
} else if (isCancelled()) {
Log.w("PayloadTask", "PayloadTask Cancelled!");
return null;
}
/*synchronized (usbConnection)*/
result = usbConnection.bulkTransfer(usbFromAggBoard, response, response.length, 25);
tries += 1;
}
Log.i("PayloadTask", new Integer(response.length).toString() + " bytes received: ");
Log.d("PayloadTask", "doInBackground returning some payload from the aggboard.");
return Payload.CreatePayload(response);
Refactored Code [UsbRequest/requestWait()] -
byte[] response = new byte[64];
int result = 0;
int tries = 0;
while (result < 1){
if (tries > 5) {
Log.d("PayloadTask", "PayloadTask Timed Out.");
return null;
} else if (isCancelled()) {
Log.w("PayloadTask", "PayloadTask Cancelled!");
return null;
}
/*synchronized (usbConnection)*/
if (usbConnection != null) {
UsbRequest request = new UsbRequest();
ByteBuffer byteBuffer = ByteBuffer.wrap(response);
byteBuffer.rewind();
try {
request.initialize(usbConnection, usbFromAggBoard);
if (!request.queue(byteBuffer, response.length)) {
throw new IOException("Error queueing USB request.");
}
usbConnection.requestWait();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(usbConnection != null) {
result = request.hashCode();
request.close();
}
else{
Log.d("PayloadTask", "Request not received by usbConnection");
}
}
}
tries += 1;
}
Log.i("PayloadTask", new Integer(response.length).toString() + " bytes received: ");
Log.d("PayloadTask", "doInBackground returning some payload from the aggboard.");
return Payload.CreatePayload(response);

Related

Custom TCPListener closing TCPClient before reply

I am using the following TCPListener in Xamarin.Android that has served me well in the past, but only have one problem with it...
In the following piece of code within startListener() (Full code below)
while (!NS.DataAvailable && sta)
{
System.Threading.Thread.Sleep(50);
if (cntr > 20)
{
TCPC.Close();
NS = null;
break;
}
cntr++;
}
The TCPClient gets closed if the NetworkSteam has not data to Send(Reply) or Receive for a period of time, which is not a problem and do need it to close this.
Problem is that I can set this "timeout" between changing Thread.Sleep(50) value and cntr > 20 value, but becomes very difficult to predict how long this timeout needs to be depending on what data was received, as I need to perform long running operations on the data before sending the reply (long running as in could be 3sec or 30sec), if I set it too short then it closes my TCP client before I am able to reply, if I set it too long the port is blocked for many seconds after sending the reply and I cannot send the next command until this timeout has expired. Is there a better way to handle this timeout, or a better implementation not using TCPListener
private static void startListener()
{
ListenStarted = true;
TCPL = new TcpListener(IPAddress.Any, 8012);
try
{
TCPL.Start();
sta = true;
}
catch (Exception e)
{
//Log Error
}
while (sta)
{
try
{
TCPC = TCPL.AcceptTcpClient();
TCPC.NoDelay = true;
NS = TCPC.GetStream();
while (NS.CanRead && NS.CanWrite && sta)
{
StringBuilder sb = new StringBuilder();
int cntr = 0;
while (!NS.DataAvailable && sta)
{
System.Threading.Thread.Sleep(50);
if (cntr > 20)
{
TCPC.Close();
NS = null;
break;
}
cntr++;
}
if (NS == null)
break;
while (NS.DataAvailable && sta)
{
int bte = NS.ReadByte();
if (bte == -1)
break;
if ((char)bte != (char)0x03)
{
sb.Append((char)bte);
}
else
{
processDataSets(sb);
break;
}
}
}
}
catch (Exception e)
{
sta = false;
}
}
ListenStarted = false;
NS = null;
}

Connecting with Blutooth Low Energy device. How to wait for BluetoothGattCallback methods to finish

I am new to Android and I have a project that connects an android device with other device with BLE. After connecting I have mBluetoothGatt.discoverServices() and I need to call mBluetoothGatt.getServices() after onServicesDiscovered is called. For now I am using this code:
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if(status == BluetoothGatt.GATT_SUCCESS){
servicesFound = true;
Log.i("Send", String.valueOf(gatt.getServices().size()));
}
}
and this is in the button click:
public void btnTestWriteOnClick(View v){
if(mBluetoothGatt != null) {
mBluetoothGatt.discoverServices();
byte[] allBytesToSend = new byte[]{...};//Test byte array.
List<BluetoothGattService> serviceList = mBluetoothGatt.getServices();
while(!servicesFound){
}
servicesFound = false;
displayGattServicesTest(serviceList, allBytesToSend);
}
}
EDIT
My displayGattServicesTest:
private void displayGattServicesTest(List<BluetoothGattService> gattServices, byte[] allBytesToSend) {
if (gattServices == null) return;
//Sets the interval for printing.
//ChangeIntervalAndTimeout();
// Enable notification for characteristic.
EnableNotificationInFFF4(gattServices);
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
if (gattService.getUuid().toString().contains(serviceUUID)) {
for (final BluetoothGattCharacteristic characteristic : gattService.getCharacteristics()) {
if (characteristic.getUuid().toString().contains(characUUID)) {
long startTime = System.currentTimeMillis();
Log.i("Send", "===========================BEGINNING===========================");
int size = 19;
int times = allBytesToSend.length / size;
if (allBytesToSend.length > times * size) {
times++;
}
params = new byte[times][];
int tmp;
for (tmp = 0; tmp < 1000; tmp++) {
int logCount = 0;
for (int i = 0; i < allBytesToSend.length; i++) {
if(allBytesToSend.length < i + size){
size = allBytesToSend.length - i;
}
params[logCount] = new byte[size];
System.arraycopy(allBytesToSend, i, params[logCount], 0, size);
i += size - 1;
Log.i("Send", "====Sending command No " + logCount + "====");
logCount++;
}
WriteIntoPrinter t = new WriteIntoPrinter(characteristic);
t.execute(params);
try {
t.get(2000, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
t.cancel(true);
break;
}
}
Log.i("Send", "===========================DONE===========================");
Log.i("Send", "Tmp = " + tmp);
long difference = System.currentTimeMillis() - startTime;
Log.i("Send", "Time - " + (double) (difference / 1000) + " sec.");
break;
}
}
break;
}
}
}
My AsyncTask:
private class WriteIntoPrinter extends AsyncTask<byte[], Void, Void>{
BluetoothGattCharacteristic characteristic;
WriteIntoPrinter(BluetoothGattCharacteristic characteristic){
this.characteristic = characteristic;
//characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
}
#Override
protected Void doInBackground(final byte[]... params) {
for (int i = 0; i < params.length ; i++) {
Log.i("Send", "Sending - " + i + " part. Number of bytes: " + params[i].length);
characteristic.setValue(params[i]);
mBluetoothGatt.writeCharacteristic(characteristic);
final int finalI = i;
Thread t = new Thread(new Runnable() {
#Override
public void run() {
if (finalI == params.length - 1) {
Log.i("Send", "WaitingThread final - " + finalI);
while (!isWritingOnPaper) {
//SystemClock.sleep(20);
}
Log.i("Send", "WaitingThread final - " + finalI + " Done.");
}
else{
while (!isSuccessful) {
//SystemClock.sleep(20);
}
}
}
});
t.start();
try {
t.join(); // wait for thread to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
isWritingOnPaper = false;
isSuccessful = false;
}
return null;
}
}
NOTE This is only a test don't mind the for loops and the new Thread in the AsyncTask. Basecally in displayGattServicesTest I am splitting a byte[] into 19 byte arrays and sending it 1000 times to test the speed.
NOTE 2 Dept Description: displayGattServicesTest take byte[] that is exactly 4*19 bytes long. After that it makes byte[4][19] and gives byte[4][19] to a AsyncTask that starts writing into the characteristic. Every time waits for onCharacteristicWrite to return true and when it writes the last [19] bytes waits for onCharacteristicChanged to return true and then writes the next byte[4][19]. This is the goal.
I do the same think when reading and writing.
This is working but I don't think this is the right way to do it. :) Is there any other way to wait onServicesDiscovered, onCharacteristicWrite and onCharacteristicChanged to finish successful.
In your btnTestWriteOnClick just show progress bar, then wait for result in onServicesDiscovered , if you got result then hide progress bar and do your stuff (call displayGattServicesTest in onServicesDiscovered), you can disable button while discovering is in progress to prevent user from clicking it and starting new discover.

Android VpnService block packets

Edit:- i'm able to start the internet using vpn.The other issues is that now i'm receiving packets in my service in this piece of code of my VpnService.But i can't think of a proper way to block particular website.I've tried using name resolution using InnetAddress but that's not giving the expected result :
**#Override
public void run()
{
Log.i(TAG, "Started");
FileChannel vpnInput = new FileInputStream(vpnFileDescriptor).getChannel();
FileChannel vpnOutput = new FileOutputStream(vpnFileDescriptor).getChannel();
try
{
ByteBuffer bufferToNetwork = null;
boolean dataSent = true;
boolean dataReceived;
while (!Thread.interrupted())
{
if (dataSent)
bufferToNetwork = ByteBufferPool.acquire();
int readBytes = vpnInput.read(bufferToNetwork);
if (readBytes > 0)
{
dataSent = true;
bufferToNetwork.flip();
Packet packet = new Packet(bufferToNetwork);
Log.e("loggg packet",packet.toString());
if (packet.isUDP())
{
deviceToNetworkUDPQueue.offer(packet);
}
else if (packet.isTCP())
{
deviceToNetworkTCPQueue.offer(packet);
}
else
{
Log.w(TAG, "Unknown packet type");
dataSent = false;
}
}
else
{
dataSent = false;
}
ByteBuffer bufferFromNetwork = networkToDeviceQueue.poll();
if (bufferFromNetwork != null)
{
bufferFromNetwork.flip();
vpnOutput.write(bufferFromNetwork);
dataReceived = true;
ByteBufferPool.release(bufferFromNetwork);
}
else
{
dataReceived = false;
}
if (!dataSent && !dataReceived)
Thread.sleep(10);
}
}
catch (InterruptedException e)
{
Log.i(TAG, "Stopping");
}
catch (IOException e)
{
Log.w(TAG, e.toString(), e);
}
finally
{
closeResources(vpnInput, vpnOutput);
}
}**
I'm receiving a packet in this format:
Packet{ip4Header=IP4Header{version=4, totalLength=40, protocol=TCP, headerChecksum=14192, sourceAddress=10.0.8.1, destinationAddress=216.58.196.100}, tcpHeader=TCPHeader{sourcePort=39217, destinationPort=443, sequenceNumber=800911985, acknowledgementNumber=823271551, headerLength=20, window=29596, checksum=32492, flags= ACK}, payloadSize=0}
I'm using THIS CODE for starter and unable to block packets.
Apps like greyshirts no root firewall and mobiwool no root firewall works perfectly and they are also vpn based.Any suggestion is most welcomed.

Checking for escape character(0x1B,\033) while reading from socket

Ok, so I'm designing an Android MUD client as part of my school project. I'm having an issue, however, while implementing ANSI color parsing. I read in the data on a byte-by-byte basis. I've tried setting the character "hex" as '\033', '27', and '0x1B' but I can never seem to get it to detect the escape character. Is there anything you guys can see wrong with my checking of it? Also, the line "char check = String.valueOf(j).charAt(0);" is temporary, I was originally trying to check the character variable "hex" against the byte "j". Is there possibly a better way of checking for the character?
while(isConnected) {
int j = 0;
try {
int i = arrayOfByte.length;
j = streamInput.read(arrayOfByte, 0, i);
char check = String.valueOf(j).charAt(0);
Log.d("Console","Char is - " + check);
if (j == -1)
{
Log.d("Console","j = -1");
throw new Exception("Error while reading socket.");
} else if (j == 0) {
Log.d("Console","Continuing");
continue;
} else if (check == hex) {
Log.d("Console","Yo, daddio!");
} else {
final String strData = new String(arrayOfByte, 0, j).replace("\r", "");
runOnUiThread(new Runnable() {
public void run() {
textContent.append(strData);
scrollToBottom();
}
});
}
} catch (Exception e) {
Handler handlerException = GameWindow.this.mHandler;
String strException = e.getMessage();
final String strMessage = "Error while receiving from server:\r\nConnection terminated";
Runnable rExceptionThread = new Runnable()
{
public void run()
{
Toast.makeText(context, strMessage, 3000).show();
}
};
handlerException.post(rExceptionThread);
if(strException.indexOf("reset") != -1 || strException.indexOf("rejected") != -1)
{
isConnected = false;
try
{
connectionSocket.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
isConnected = false;
}
}
Well, you're checking the number of bytes read instead of each individual byte.
j = streamInput.read(arrayOfByte, 0, i);
returns the number of bytes read and put in arrayOfByte those bytes.
Therefore you need to do the following:
for (int n=0; n < j; n++)
{
if (arrayOfByte[n] == hex) Log.d("Console", "Yo, daddio!");
}

Connect IMAP to Android .any example

any example of IMAP and if possible source code of IMAP and Android.
http://code.google.com/hosting/search?q=label%3AAndroid+IMAP&projectsearch=Search+projects
protected InputStream getContentStream() throws MessagingException {
InputStream is = null;
boolean pk = message.getPeek(); // acquire outisde of message cache lock
// Acquire MessageCacheLock, to freeze seqnum.
synchronized(message.getMessageCacheLock()) {
try {
IMAPProtocol p = message.getProtocol();
// Check whether this message is expunged
message.checkExpunged();
if (p.isREV1() && (message.getFetchBlockSize() != -1))
return new IMAPInputStream(message, sectionId, bs.size, pk);
// Else, vanila IMAP4, no partial fetch
int seqnum = message.getSequenceNumber();
BODY b;
if (pk)
b = p.peekBody(seqnum, sectionId);
else
b = p.fetchBody(seqnum, sectionId);
if (b != null)
is = b.getByteArrayInputStream();
} catch (ConnectionException cex) {
throw new FolderClosedException(
message.getFolder(), cex.getMessage());
} catch (ProtocolException pex) {
throw new MessagingException(pex.getMessage(), pex);
}
refer this

Categories

Resources