android usb读写数据,Android Usb主机接收数据

我尝试从我的评估板中读取数据.它是Stellaris EKK-LM4F232 Evalutaion Kit.它有五个按钮.我按下一个按钮并将数据发送到我的Android设备.例如,我按一次,然后发送到1,第二次发送到2 ….当我第一次按下按钮时,我从Android设备收到第一个值(它意味着1).但是当我再次按下按钮时,我无法接收任何其他值,如2,3,4,….

这是我的阅读代码.它在启动时连续读取.你能帮助我吗?

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();

}

在与@Mike Ortiz讨论之后,我将所有代码放在这里.当我点击按钮发送android设备发送“START”命令到设备然后读取开始.

当我发送到Android设备的第一个值,我可以得到它,但在第一个值后,我没有从USB设备获得任何价值.我确信usb设备会发送值.

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");

}

}

解决方法:

循环中哪个条件失败? (request.queue(buffer,bufferMaxLength)== true)或(connection.requestWait()== request)?如果是第二个,我可能建议使用.equals()而不是参考比较==.

您是否尝试过使用批量转移而不是请求?

private UsbManager mManager;

private UsbDevice mDevice;

private UsbDeviceConnection mDeviceConnection;

private UsbInterface mInterface;

private UsbEndpoint mEndpointIn;

public void run() {

while (mEndpointIn != null && mDeviceConnection != null) {

byte[] recordIn = new byte[mEndpointIn.getMaxPacketSize()];

int receivedLength = mDeviceConnection.bulkTransfer(mEndpointIn, recordIn,

recordIn.length, mTimeout);

if (receivedLength > -1) passDataToListener(recordIn);

}

}

public void initializeCommunication(Context context) throws UsbDeviceException {

mInterface = findHidInterface();

if (mManager != null && mDevice != null && mInterface != null) {

mDeviceConnection = mManager.openDevice(mDevice);

if (mDeviceConnection != null) {

if (mDeviceConnection.claimInterface(mInterface, true)) {

UsbEndpoint usbEndpointOut = null;

UsbEndpoint usbEndpointIn = null;

//

// Look for Interrupt endpoints

//

for (int i = 0; i < mInterface.getEndpointCount(); i++) {

UsbEndpoint usbEndpoint = mInterface.getEndpoint(i);

if (usbEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {

if (usbEndpoint.getDirection() == UsbConstants.USB_DIR_OUT) {

usbEndpointOut = usbEndpoint;

} else {

usbEndpointIn = usbEndpoint;

}

}

}

if (usbEndpointOut == null || usbEndpointIn == null) {

Log.w(DEBUG, "No USB endpoints found.");

}

mEndpointOut = usbEndpointOut;

mEndpointIn = usbEndpointIn;

} else {

mDeviceConnection.close();

Log.w(DEBUG, "USB claim interface failed.");

}

} else {

Log.w(DEBUG, "USB open device failed.");

}

} else {

Log.w(DEBUG, "Some essential USB variables were null.");

}

}

/**

* Find the HID interface.

*

* @return

* Return the HID interface if found, otherwise null.

*/

private UsbInterface findHidInterface() {

if (mDevice != null) {

final int interfaceCount = mDevice.getInterfaceCount();

for (int interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) {

UsbInterface usbInterface = mDevice.getInterface(interfaceIndex);

//

// Can add UsbInterface.getInterfaceSubclass() and

// UsbInterface.getInterfaceProtocol() for more specifics.

//

if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_HID) {

return usbInterface;

}

}

Log.w(DEBUG, "HID interface not found.");

}

return null;

}

标签:android,usb,host

来源: https://codeday.me/bug/20190624/1281981.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值