找到的一个 关于用USB连接到手机发送短消息的DEMO CODE

http://www.phpd.cn/attachment.php?id=215

点这个链接下载吧.

转载于:https://www.cnblogs.com/mota/archive/2010/04/09/1708035.html

很抱歉,作为AI语言模型,我没有办法提供完整的代码示例,但我可以提供一些参考。 首先,你需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-feature android:name="android.hardware.usb.host" /> <uses-permission android:name="android.permission.USB_PERMISSION" /> ``` 然后在你的Activity中实现USB设备的连接和通信,可以参考以下代码: ```java public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private UsbManager mUsbManager; private PendingIntent mPermissionIntent; private UsbDevice mDevice; private UsbInterface mInterface; private UsbEndpoint mEndpointIn; private UsbEndpoint mEndpointOut; private UsbDeviceConnection mConnection; private static final int TIMEOUT = 5000; private static final int VID = 0x1234; // USB设备的VID private static final int PID = 0x5678; // USB设备的PID @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 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); // 查找USB设备 for (UsbDevice device : mUsbManager.getDeviceList().values()) { if (device.getVendorId() == VID && device.getProductId() == PID) { mDevice = device; break; } } if (mDevice != null) { // 请求USB设备的权限 mUsbManager.requestPermission(mDevice, mPermissionIntent); } else { Log.e(TAG, "USB Device not found!"); } } private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { @Override 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) { mConnection = mUsbManager.openDevice(device); if (mConnection != null) { // 查找USB设备的接口和端点 for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface usbInterface = device.getInterface(i); if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) { mInterface = usbInterface; break; } } if (mInterface != null) { for (int i = 0; i < mInterface.getEndpointCount(); i++) { UsbEndpoint endpoint = mInterface.getEndpoint(i); if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) { mEndpointIn = endpoint; } else if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { mEndpointOut = endpoint; } } } if (mEndpointIn != null && mEndpointOut != null) { // USB设备连接成功,开始进行数据通信 new Thread(new Runnable() { @Override public void run() { byte[] buffer = new byte[1024]; int length; // 发送数据到USB设备 byte[] data = "Hello USB Device!".getBytes(); int sent = mConnection.bulkTransfer(mEndpointOut, data, data.length, TIMEOUT); Log.d(TAG, "Sent: " + sent); // 接收USB设备发送的数据 while (true) { length = mConnection.bulkTransfer(mEndpointIn, buffer, buffer.length, TIMEOUT); if (length > 0) { String message = new String(buffer, 0, length); Log.d(TAG, "Received: " + message); // 发送接收到的数据回去 sent = mConnection.bulkTransfer(mEndpointOut, buffer, length, TIMEOUT); Log.d(TAG, "Sent: " + sent); } } } }).start(); } } } } } else { Log.e(TAG, "Permission denied!"); } } } } }; @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mUsbReceiver); if (mConnection != null) { mConnection.close(); } } } ``` 在上面的代码中,我们先查找USB设备,然后请求USB设备的权限。如果用户允许了访问权限,我们就打开USB设备,并查找USB设备的接口和端点。最后,我们在一个新线程中进行数据通信,发送数据到USB设备,并不断接收USB设备发送的数据,并将接收到的数据发送回去。 希望这些代码能够帮助你开始编写你的USB通信程序!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值