我打算将条形码扫描仪连接到我的手机并使用它。
为了在建立 USB 连接时检测事件,我编写了如下代码。
AndroidManifest.xml
<uses-feature android:name="android.hardware.usb.host" />
..
<application
..
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<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/device_filter" />
>
</application>
xml/设备过滤器
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1504" product-id="5889"/> <!--0x0d28 / 0x0204: ARM mbed-->
<usb-device vendor-id="1504" product-id="6400"/>
</resources>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val ACTION_USB_PERMISSION = "com.example.test.USB_PERMISSION"
private lateinit var usbManager: UsbManager
private lateinit var serialDevice: UsbSerialDevice
private lateinit var usbDeviceConnection: UsbDeviceConnection
private lateinit var statusView: TextView
private lateinit var resultView: TextView
private lateinit var permissionIntent: PendingIntent
private var usbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (ACTION_USB_PERMISSION == intent?.action) {
printStatus("usbReceiver")
synchronized(this) {
val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
device?.apply {
printResult("Permission Granted for device $device")
}
}else {
printResult("Permission denied for device $device")
usbManager.requestPermission(device, permissionIntent)
}
}
}
}
}
private var attachUsbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (UsbManager.ACTION_USB_DEVICE_ATTACHED == intent?.action) {
printStatus("attachUsbReceiver")
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
device?.let {
printDeviceList()
}
}
}
}
private var detachUsbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (UsbManager.ACTION_USB_DEVICE_DETACHED == intent?.action) {
printStatus("detachUsbReceiver")
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
device?.let {
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
statusView = findViewById(R.id.text_status)
resultView = findViewById(R.id.text_result)
usbManager = getSystemService(UsbManager::class.java)
permissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0)
val filter = IntentFilter(ACTION_USB_PERMISSION)
registerReceiver(usbReceiver, filter)
val attachFilter = IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED)
registerReceiver(attachUsbReceiver, attachFilter)
val detachFilter = IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED)
registerReceiver(detachUsbReceiver, detachFilter)
handleIntent(intent)
var availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager)
if (availableDrivers.isEmpty()) {
printStatus("Serial is Empty")
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
if (device != null) {
//printStatus("device is not null")
printDeviceList()
}else {
printStatus("device is null")
}
}
private fun printDeviceList() {
val connectedDevices = usbManager.deviceList
if (connectedDevices.isEmpty()) {
printResult("No Devices Currently Connected")
}else {
val builder = buildString {
append("Connected Device Count: ")
append(connectedDevices.size)
append("\n\n")
for (device in connectedDevices.values) {
append(device.describeContents())
append("\n\n")
}
}
printResult(builder)
}
}
private fun printStatus(status: String) {
statusView.text = status
Toast.makeText(this, "Print Status " + status, Toast.LENGTH_LONG).show()
}
private fun printResult(result: String) {
resultView.text = result
Toast.makeText(this, "Print Result " + result, Toast.LENGTH_LONG).show()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
val pressedKey = event?.getUnicodeChar();
var barcode: String = ""
barcode += "" + pressedKey;
Toast.makeText(getApplicationContext(), "barcode--->>>" + barcode, Toast.LENGTH_SHORT)
.show();
return true
}
}
但是,我无法获取设备列表。当我将条码扫描器连接到安卓手机时,它返回连接的设备是空的。在您看来,如果设置有任何问题,请告诉我。
请帮我