Home > Mobile >  Two by Android + Usb OTG line line, can be connected, but not unable to send and receive data
Two by Android + Usb OTG line line, can be connected, but not unable to send and receive data

Time:10-27

The USB communication to achieve two Android devices

Experimental environment is as follows:
1, two Android phones, red rice (as the host), millet Note (as accessory);
2, OTG line host, accessory connect usb cable, connect OTG again;


Refer to the article and reference code:
https://developer.android.com/guide/topics/connectivity/usb/host.html
https://developer.android.com/guide/topics/connectivity/usb/accessory.html
https://github.com/quandoo/android2android-accessory

current status:
Can be connected to the device, however,
The host side:
With the connection. BulkTransfer (EndPointIn,... ) don't get data, instead of through UsbRequest receive data can be received;
With the connection. BulkTransfer (EndPointOut,... Have been return 1) to send data, using UsbRequst send data is not an error;
The accessory end:
MOutStream. Write can send;
MInStream. Read block, not receive data, unknown reason, need more protocol communication process? How to do?


The Host code
 
Public class UsbHostActivity extends AppCompatActivity {
Private UsbManager mUsbManager;
Private UsbDevice mDevice;
Private List MSendMsgList;//send data list
Private CommunicationThread mCommThread;
Private PendingIntent mPermissionIntent;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
.
MUsbManager=(UsbManager) getSystemService (Context. USB_SERVICE);
MPermissionIntent=PendingIntent. GetBroadcast (this, 0, new Intent (ACTION_USB_PERMISSION), 0).
IntentFilter filter=new IntentFilter (ACTION_USB_PERMISSION);
RegisterReceiver (mUsbReceiver, filter);
//the initial control, click on the button calls checkDevice connected devices
}

Private void initStringControlTransfer (final UsbDeviceConnection deviceConnection,
Final int index,
Final String String) {
DeviceConnection. ControlTransfer (0 x40, 52, 0, index, string. GetBytes (), string. The length (), 100);
}

Private volatile Boolean mExit=false;
Private class CommunicationThread extends Thread {
@ Override
Public void the run () {
MExit=false;
UsbEndpoint endpointIn=null;
UsbEndpoint endpointOut=null;
UsbInterface UsbInterface=mDevice. GetInterface (0);
If (usbInterface==null) {
return;
}
UsbDeviceConnection connection=mUsbManager. OpenDevice (mDevice);
If (connection==null) {
return;
}
if (! Connection. ClaimInterface (usbInterface, true)) {
Connection. The close ();
return;
}
//send the control message
InitStringControlTransfer (connection, 0, "UsbTest Example");//MANUFACTURER
UsbTest initStringControlTransfer (connection, 1, "");//MODEL
InitStringControlTransfer (connection, 2, "Test Usb Host and Accessory");//the DESCRIPTION
InitStringControlTransfer (connection, 3, "0.1");//VERSION
InitStringControlTransfer (connection, 4, "");//URI
InitStringControlTransfer (connection, 5, "42");//SERIAL
Connection. ControlTransfer (0 x40, 53, 0, 0, new byte [] {}, 0, 100);

For (int I=0; i Final UsbEndpoint endpoint=usbInterface. GetEndpoint (I);
If (the endpoint. GetDirection ()==UsbConstants. USB_DIR_IN) {
EndpointIn=the endpoint;
}
If (the endpoint. GetDirection ()==UsbConstants. USB_DIR_OUT) {
EndpointOut=the endpoint;
}
}
Byte buff []=new byte [256].
while (! MExit) {
Final int bytesTransferred=connection. BulkTransfer (endpointIn, buff, buff. Length, 100);
If (bytesTransferred & gt; 0 {
//TODO
} else {
//process until you come here, receive data failure
}
//this way successfully received message
//ByteBuffer buffer=ByteBuffer. The allocate (256);
//UsbRequest request=new UsbRequest ();
//request. The initialize (connection, endpointIn);
//a Boolean ret=request queue (buffer, 256);
//if (ret) {
//if (mConnection requestWait ()==request) {
//}
//}
Synchronized (mSendMsgList) {
if (! MSendMsgList. IsEmpty ()) {
.
Int len=connection. BulkTransfer (endpointOut, sendBuff, sendBuff. Length, 100);
If (len & gt;=0) {
//TODO
} else {
//process until you come here, send failure
}
//this method can send, will not return an error
//UsbRequest request=new UsbRequest ();
//request. The initialize (mConnection, mEndOut);
//ByteBuffer buffer=ByteBuffer. Wrap (text) getBytes ());
//a Boolean ret=request queue (buffer, text getBytes (.) length);
//if (ret) {
//}
}
}
}
.
}
}

Private void checkDevice () {
HashMap DeviceList=mUsbManager. The getDeviceList ();
If (deviceList==null | | deviceList. IsEmpty ()) {
return;
}
Iterator DeviceIterator=deviceList. Values (). The iterator ();
While (deviceIterator. HasNext ()) {
UsbDevice device=deviceIterator. Next ();
MUsbManager. RequestPermission (device, mPermissionIntent);
break;
}
}

Private static final String ACTION_USB_PERMISSION="com. Mobilemerit. Usbhost. USB_PERMISSION";
Private final BroadcastReceiver mUsbReceiver=new BroadcastReceiver () {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related