Cordova应用通过USB连接Bixolon SRP-350III热敏打印机问题求助
解决Cordova中Bixolon SRP-350III USB打印连接问题
看起来你现在卡在USB打印机的连接环节了,我结合Bixolon SDK的常规用法和Cordova插件开发的经验,给你一步步梳理解决办法:
1. 先把Android端的权限和设备过滤配置到位
msg.obj为空大概率是系统没识别到打印机,或者权限没配置对。首先要在AndroidManifest.xml里补全必要的配置:
<!-- 添加USB权限 --> <uses-permission android:name="android.permission.USB_PERMISSION" /> <uses-feature android:name="android.hardware.usb.host" android:required="false" /> <!-- 在你的主Activity里添加USB设备连接的Intent过滤 --> <activity ...> <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_device_filter" /> </activity>
然后在res/xml目录下创建usb_device_filter.xml,指定Bixolon SRP-350III的VID和PID(我查了官方文档,这款的VID是0x04E8,PID是0x0237,转成十进制就是1256和567):
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="1256" product-id="567" /> </resources>
2. 完善Java代码里的连接逻辑
你现在的代码只做了空判断,缺少实际的权限请求和设备连接步骤。我给你补全这部分:
// 先定义权限请求的常量 private static final String ACTION_USB_PERMISSION = "com.your.app.package.USB_PERMISSION"; private BixolonPrinter mPrinter; private BroadcastReceiver mUsbReceiver; // 在类的初始化方法里注册USB权限广播接收器 private void initUsbReceiver() { mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (device != null) { // 权限通过,打开打印机连接 connectPrinter(device); } } else { Log.d(TAG, "USB权限被拒绝"); BixolonPrint.this.cbContext.error("USB permission denied"); } } } } }; IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(mUsbReceiver, filter); } // 实际连接打印机的方法 private void connectPrinter(UsbDevice device) { mPrinter = new BixolonPrinter(this); int result = mPrinter.openUsbDevice(device); if (result == BixolonPrinter.ERROR_NONE) { Log.d(TAG, "打印机连接成功"); BixolonPrint.this.cbContext.success("Printer connected"); // 这里可以加个测试打印,验证是否能正常工作 printTestContent(); } else { Log.d(TAG, "连接失败,错误码:" + result); BixolonPrint.this.cbContext.error("Failed to connect: error code " + result); } } // 补全你原来的Handler逻辑 @Override public void handleMessage(Message msg) { switch (msg.what) { case BixolonPrinter.MESSAGE_USB_DEVICE_SET: Log.d(TAG, "perform mHandler: MESSAGE_USB_DEVICE_SET"); if (msg.obj == null) { BixolonPrint.this.cbContext.error("USB printer not found"); } else { UsbDevice device = (UsbDevice) msg.obj; UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); // 请求USB权限 usbManager.requestPermission(device, permissionIntent); } break; // 其他case... } } // 别忘了在onDestroy里注销接收器 @Override public void onDestroy() { super.onDestroy(); if (mUsbReceiver != null) { unregisterReceiver(mUsbReceiver); } if (mPrinter != null) { mPrinter.close(); } }
3. 前端调用的注意点
在你的index.html里,要确保在deviceready事件触发后再调用打印机相关方法,比如:
document.addEventListener("deviceready", function() { // 初始化打印机插件 cordova.plugins.yourPrinterPlugin.init(function(success) { // 触发USB设备检测 cordova.plugins.yourPrinterPlugin.detectUsbPrinter(function(deviceFound) { console.log("检测到打印机:", deviceFound); // 这里调用连接方法 cordova.plugins.yourPrinterPlugin.connect(function(connected) { alert("连接成功!"); // 测试打印 cordova.plugins.yourPrinterPlugin.printText("Hello Bixolon Printer!"); }, function(error) { alert("连接失败:" + error); }); }, function(error) { alert("未检测到打印机:" + error); }); }, function(error) { alert("初始化失败:" + error); }); }, false);
4. 常见排查点
- 确认打印机已经开机并通过USB正确连接到Android设备,部分设备需要手动开启「USB主机模式」
- 检查Bixolon SDK版本是否适配SRP-350III,建议去官网下载最新版SDK
- 用Android Studio的Logcat查看详细日志,比如是否有USB设备枚举失败、权限请求被拒绝的信息
内容的提问来源于stack exchange,提问作者nick
相关产品推荐
相关产品推荐

