Android 应用程序,与插入 USB 端口的设备通信

作者:编程家 分类: android 时间:2025-07-31

如今,Android应用程序已经成为人们生活中必不可少的一部分。随着科技的不断发展,人们对于与外部设备的交互需求也越来越高。插入USB端口的设备,如键盘、鼠标、摄像头等,为用户提供了更多的操作选择和功能拓展。本文将探讨如何利用Android应用程序与插入USB端口的设备进行通信。

一、USB通信的基本原理

在Android系统中,USB通信是通过USB Host模式实现的。USB Host模式允许Android设备充当主机,与外部设备进行直接通信。为了使用USB Host模式,需要在AndroidManifest.xml文件中添加相应的权限声明。

二、检测USB设备的连接状态

在与USB设备进行通信之前,首先需要检测USB设备的连接状态。可以通过注册BroadcastReceiver来监听USB设备的连接和断开事件。以下是一个示例代码:

java

public class MainActivity extends AppCompatActivity {

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

private PendingIntent mPermissionIntent;

private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {

// USB设备已连接

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

if (device != null) {

// 请求USB设备权限

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

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

usbManager.requestPermission(device, mPermissionIntent);

}

} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {

// USB设备已断开

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 注册USB设备连接和断开的广播接收器

IntentFilter filter = new IntentFilter();

filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);

filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

registerReceiver(mUsbReceiver, filter);

}

@Override

protected void onDestroy() {

super.onDestroy();

// 取消注册广播接收器

unregisterReceiver(mUsbReceiver);

}

}

三、与USB设备进行数据交互

一旦USB设备连接成功,并获取到了设备的权限,就可以开始与USB设备进行数据交互了。USB通信可以通过读取和写入数据的方式进行。以下是一个示例代码,实现了向USB设备发送数据的功能:

java

public class MainActivity extends AppCompatActivity {

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

private PendingIntent mPermissionIntent;

private UsbDeviceConnection mConnection;

private UsbEndpoint mEndpointOut;

private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {

// USB设备已连接

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

if (device != null) {

// 请求USB设备权限

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

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

usbManager.requestPermission(device, mPermissionIntent);

}

} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {

// USB设备已断开

if (mConnection != null) {

mConnection.close();

mConnection = null;

}

} else if (ACTION_USB_PERMISSION.equals(action)) {

// USB设备权限已获取

synchronized (this) {

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

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

if (device != null) {

// 打开USB设备连接

mConnection = usbManager.openDevice(device);

// 找到USB设备的数据通信端点

if (device.getInterfaceCount() > 0) {

UsbInterface usbInterface = device.getInterface(0);

if (usbInterface.getEndpointCount() > 0) {

mEndpointOut = usbInterface.getEndpoint(0);

}

}

}

}

}

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 注册USB设备连接和断开的广播接收器

IntentFilter filter = new IntentFilter();

filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);

filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

filter.addAction(ACTION_USB_PERMISSION);

registerReceiver(mUsbReceiver, filter);

}

@Override

protected void onDestroy() {

super.onDestroy();

// 取消注册广播接收器

unregisterReceiver(mUsbReceiver);

// 关闭USB设备连接

if (mConnection != null) {

mConnection.close();

mConnection = null;

}

}

private void sendData(byte[] data) {

if (mConnection != null && mEndpointOut != null) {

int bytesSent = mConnection.bulkTransfer(mEndpointOut, data, data.length, 0);

if (bytesSent >= 0) {

// 数据发送成功

} else {

// 数据发送失败

}

}

}

}

四、

通过以上的代码示例,我们可以看到如何利用Android应用程序与插入USB端口的设备进行通信。首先,需要检测USB设备的连接状态,并获取到设备的权限。然后,可以通过读取和写入数据的方式与USB设备进行数据交互。这为用户提供了更多的控制和操作方式,拓展了Android应用程序的功能。

希望本文对于学习Android应用程序与插入USB端口的设备通信的读者有所帮助。通过合理利用USB通信的功能,可以为用户带来更好的用户体验和功能体验。