android usb host hid,Android Usb Host Receive Data

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?

public void startDataRecieve() {

new Thread(new Runnable() {

@Override

public void run() {

UsbEndpoint endpoint = null;

for (int i = 0; i < 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();

}

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.

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 deviceList = usbManager.getDeviceList();

Iterator deviceIterator = deviceList.values().iterator();

while (deviceIterator.hasNext()) {

UsbDevice device = deviceIterator.next();

if (device.getVendorId() == 7358 && 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 < 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 > 0)

Log.e("RESULT : " + result, " VALUE : " + new String(bytes));

}

Log.d("Thread", "STOPPPED");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值