Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Usb Host Receive Data
    primarykey
    data
    text
    <p>I try to reading data from my evalboard. It is Stellaris EKK-LM4F232 Evalutaion Kit. It has five buttons. I push a button on board and send data to my android device. For example, i push one times, and board send to 1, and second times send to 2.... I receive the first value (it mean 1) from android device when i push the button first time. But when i push the button again, i can't receive any other values like 2 ,3 ,4 ,... . Here is my code for reading. It read continuously when it is start. Can you help me?</p> <pre><code>public void startDataRecieve() { new Thread(new Runnable() { @Override public void run() { UsbEndpoint endpoint = null; for (int i = 0; i &lt; intf.getEndpointCount(); i++) { if (intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) { endpoint = intf.getEndpoint(i); break; } } UsbRequest request = new UsbRequest(); // create an URB boolean initilzed = request.initialize(connection, endpoint); if (!initilzed) { Log.e("USB CONNECTION FAILED", "Request initialization failed for reading"); return; } while (true) { int bufferMaxLength = endpoint.getMaxPacketSize(); ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength); if (request.queue(buffer, bufferMaxLength) == true) { if (connection.requestWait() == request) { String result = new String(buffer.array()); Log.i("GELEN DATA : ", result); listener.readData(result); } } } } }).start(); } </code></pre> <p>If you can't understand very well the same question is here asked by one <a href="https://stackoverflow.com/questions/14016943/android-bulktransfer-return-1-when-read-data-but-there-is-really-some-data-in-t">Android bulkTransfer return -1 when read data but there is really some data in the buffer</a></p> <p>After the discusstion with @Mike Ortiz I put all my code in here. When I click the buttonSend android device send "START" command to device then reading start. When i send to android device to first value i can get it but after first value i don't get any value from usb device. I am sure the usb device sends the values. </p> <pre><code>public class MainActivity extends Activity implements Runnable { private static final String TAG = "MAIN ACTIVITY TEST "; private Button buttonConnect; private Button butonSend; private Button buttonDisconnect; private TextView textViewResult; private UsbDevice device; private Thread readThread; private UsbManager usbManager; private UsbDeviceConnection connection; private UsbInterface usbInterface; private UsbEndpoint usbEndpointIn; private UsbEndpoint usbEndpointOut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(usbReceiver, filter); usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); HashMap&lt;String, UsbDevice&gt; deviceList = usbManager.getDeviceList(); Iterator&lt;UsbDevice&gt; deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); if (device.getVendorId() == 7358 &amp;&amp; device.getProductId() == 3) { this.device = device; usbManager.requestPermission(device, mPermissionIntent); break; } } butonSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { usbInterface = device.getInterface(0); connection = usbManager.openDevice(device); connection.claimInterface(usbInterface, false); for (int i = 0; i &lt; usbInterface.getEndpointCount(); i++) { UsbEndpoint end = usbInterface.getEndpoint(i); if (end.getDirection() == UsbConstants.USB_DIR_IN) { usbEndpointIn = end; } else { usbEndpointOut = end; } } //SEND START COMMAND TO THE USB DEVICE; int result = connection.bulkTransfer(usbEndpointOut, "START".getBytes(), "START".getBytes().length, 1000); Log.e("SEND RESULT", result + ""); //START READING in run method readThread = new Thread(MainActivity.this); readThread.start(); } }); buttonDisconnect.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mRunning = false; //readThread.stop(); connection.releaseInterface(usbInterface); connection.close(); Log.e(TAG, "Connection CLosed."); } }); } private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private final BroadcastReceiver usbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (device != null) { Toast.makeText(MainActivity.this, "Cihaza izin verildi", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(MainActivity.this, "Cihaza izin verilmedi.", Toast.LENGTH_LONG).show(); Log.d(TAG, "permission denied for device " + device); } } } } }; private boolean mRunning; private void initView() { butonSend = (Button) findViewById(R.id.buttonSend); buttonConnect = (Button) findViewById(R.id.buttonConnect); textViewResult = (TextView) findViewById(R.id.textViewResult); buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect); } @Override public void run() { mRunning = true; //READ VALUE UNTIL DISCONNECT while (mRunning) { byte[] bytes = new byte[usbEndpointIn.getMaxPacketSize()]; int result = connection.bulkTransfer(usbEndpointIn, bytes, bytes.length, 1000); if(result &gt; 0) Log.e("RESULT : " + result, " VALUE : " + new String(bytes)); } Log.d("Thread", "STOPPPED"); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload