无需修改Kernel:Android平板通过USB/网络向Win10发键盘命令(纯Kotlin可行吗?)
Absolutely! You can absolutely build a pure Kotlin Android app to send keyboard commands to a Windows 10 machine—no kernel modifications required, and both USB and network connections are totally feasible. Let’s break down how to approach each method:
Android supports USB HID (Human Interface Device) emulation natively, which lets your tablet act as a standard keyboard that Windows recognizes automatically (no extra drivers needed). This works without kernel tweaks, and you can implement it entirely in Kotlin.
Key Steps:
Declare Permissions & Filters:
Add these to yourAndroidManifest.xmlto handle USB access:<uses-permission android:name="android.permission.USB_PERMISSION" /> <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" />Create a
res/xml/usb_device_filter.xmlfile to target Windows-compatible USB hosts.Handle USB Permission & Communication:
Use Kotlin'sUsbManagerto request access to the connected Windows machine, then send HID-compliant keyboard reports. Here’s a simplified code snippet:private val usbReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (UsbManager.ACTION_USB_PERMISSION == intent.action) { val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE) if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { device?.let { setupHidKeyboard(it) } } } } } private fun setupHidKeyboard(device: UsbDevice) { val usbManager = getSystemService(Context.USB_SERVICE) as UsbManager val hidInterface = device.getInterface(0) // Target the HID interface val endpoint = hidInterface.getEndpoint(0) val connection = usbManager.openDevice(device) ?: return connection.claimInterface(hidInterface, true) // Example: Send "A" key press + release val keyDownReport = byteArrayOf(0, 0x04.toByte(), 0, 0, 0, 0, 0, 0) connection.bulkTransfer(endpoint, keyDownReport, keyDownReport.size, 1000) val keyUpReport = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0) connection.bulkTransfer(endpoint, keyUpReport, keyUpReport.size, 1000) connection.releaseInterface(hidInterface) connection.close() }
Note:
Some Android tablets may have hardware limitations for HID emulation. Test with a basic HID test app first to confirm your device supports this functionality.
If you prefer wireless control, a network-based approach is even more flexible. Your Kotlin Android app will send commands over the network, and a lightweight server on Windows will translate those commands into keyboard inputs. No kernel changes needed here either.
Key Steps:
Choose a Protocol:
- TCP: Reliable for consistent command delivery (best for most use cases).
- HTTP: Simplified implementation using libraries like OkHttp in Kotlin.
- UDP: Faster but less reliable, good for quick, non-critical commands.
Android Client (Kotlin):
Here’s a TCP client example to send keyboard commands:import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import java.net.Socket fun sendTcpCommand(ip: String, port: Int, command: String) = runBlocking { launch(Dispatchers.IO) { try { Socket(ip, port).use { socket -> socket.getOutputStream().use { output -> output.write(command.toByteArray()) output.flush() } } } catch (e: Exception) { e.printStackTrace() } } } // Usage: sendTcpCommand("192.168.1.100", 8080, "PRESS_A")Windows Server:
On Windows, you can build a simple server (using Python, C#, or even PowerShell) that listens for commands and uses native APIs likeSendInput(C#) or libraries likepyautogui(Python) to simulate keyboard presses. For example, a Python server snippet:import socket import pyautogui server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("0.0.0.0", 8080)) server.listen(1) while True: conn, addr = server.accept() command = conn.recv(1024).decode() if command == "PRESS_A": pyautogui.press('a') conn.close()
Note:
Ensure your Windows machine allows incoming connections on the chosen port (adjust firewall settings) and that both devices are on the same network (or use port forwarding for remote access).
Both methods work entirely without kernel modifications, and you can implement the Android side 100% in Kotlin. Choose USB for a wired, low-latency connection, or network for wireless flexibility.
内容的提问来源于stack exchange,提问作者Piwo

