android usb发数据,java-在Usb主机模式下将数据从android发送到已...

在我的应用程序中,正在使用USB主机模式,该模式提供有关已连接的USB大容量存储设备的信息,例如用例中的Usb闪存驱动器.现在,我需要在已连接的闪存驱动器上创建一个文件,并将一些数据保存在该文件中.到目前为止,我发现正在按以下方式连接设备,

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

private Button mCheckForDevice;

private TextView mDeviceInfo;

private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

private PendingIntent mPermissionIntent;

private UsbManager mUsbManager;

private UsbDevice mDeviceFound;

private UsbDeviceConnection mConnection;

private UsbInterface mUsbInterface = null;

private UsbEndpoint mInputEndpoint = null;

private UsbEndpoint mOutputEndpoint = null;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mCheckForDevice = (Button) findViewById(R.id.check);

mDeviceInfo = (TextView) findViewById(R.id.deviceInfo);

count=0;

mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

final HashMap deviceList = mUsbManager.getDeviceList();

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

IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

registerReceiver(mUsbReceiver, filter);

while (deviceIterator.hasNext()) {

final UsbDevice device = deviceIterator.next();

mDeviceFound = device;

i += "

" +

"DeviceID: " + device.getDeviceId() + "

" +

"DeviceName: " + device.getDeviceName() + "

" +

"VendorID: " + device.getVendorId() + "

" +

"ProductID: " + device.getProductId() + "

" +

"Serial Number: " + device.getSerialNumber() + "

";

}

mCheckForDevice.setOnClickListener(new View.OnClickListener() {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

@Override

public void onClick(View v) {

if(deviceList.size() > 0){

checkInfo(mDeviceFound);

}else{

Toast.makeText(getApplicationContext(), "No device found", Toast.LENGTH_SHORT).show();

}

}

});

}

String i = "";

private int count ;

private void checkInfo(UsbDevice device) {

count++;

if(count == 1) {

mUsbManager.requestPermission(device, mPermissionIntent);

mDeviceInfo.setText(i);

} else

Toast.makeText(this, "Already connected", Toast.LENGTH_SHORT).show();

}

@Override

protected void onPause() {

super.onPause();

count=0;

try {

unregisterReceiver(mUsbReceiver);

}catch (Exception e){

e.printStackTrace();

}

}

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

// Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show(); writeToFile("onReceive");

if (ACTION_USB_PERMISSION.equals(action)) {

synchronized (this) {

UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();

connectUsb(device);

} else {

Log.d(TAG, "permission denied for device " + device);

Toast.makeText(context, "permission denied for device" + device, Toast.LENGTH_SHORT).show();

}

}

}

}

};

private void connectUsb(UsbDevice device) {

if(device != null){

for(int i=0;i

mUsbInterface = device.getInterface(i);

UsbEndpoint tOut = null;

UsbEndpoint tIn = null;

int usbEndPointCount = mUsbInterface.getEndpointCount();

if(usbEndPointCount >=2){

for(int j =0;j

if(mUsbInterface.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK){

if(mUsbInterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT){

tOut = mUsbInterface.getEndpoint(j);

}else if(mUsbInterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN){

tIn = mUsbInterface.getEndpoint(j);

}

}

}

if(tIn!=null & tOut !=null){

mInputEndpoint = tIn;

mOutputEndpoint = tOut;

}

}

}

mConnection = mUsbManager.openDevice(device);

if (mConnection.claimInterface(mUsbInterface, true)) {

Toast.makeText(this, "Connected to device", Toast.LENGTH_SHORT).show();

String msg = "Hello world";

byte[] byteArray = msg.getBytes();

int dataTransfered = mConnection.bulkTransfer(mOutputEndpoint,byteArray,byteArray.length, 0);

int controlTransfer = mConnection.controlTransfer( UsbConstants.USB_DIR_OUT, 1,0,0,byteArray,byteArray.length,0);

Toast.makeText(this, "controlTransfer " +controlTransfer, Toast.LENGTH_SHORT).show();

} else {

Log.i(TAG, "Could not connect!");

Toast.makeText(this, "Could not connect", Toast.LENGTH_SHORT).show();

}

}else{

Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();

}

}

activity_main.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/check"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Search for devices "

android:textColor="@color/black"

android:textSize="15sp" />

android:id="@+id/deviceInfo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_below="@+id/check"

android:layout_marginTop="14dp"

android:textColor="@color/black"

android:textSize="15sp" />

在上面的示例中,我尝试将String值发送到连接的设备,并且返回的int值是发送的字符数.但是,此信息在何处存储在连接的设备上,我的意思是位置?

建立与设备的连接后,是否可以在连接的设备上创建文件?指向相关博客文章的链接很有帮助.

我发现堆栈上有this,但是该解决方案没有任何帮助.

感谢您的帮助.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值