2013-07-14 153 views
0

我是javax.usb API的新手,我的目的是在特定時間禁用USB端口,以阻止與USB設備的通信,並在一段時間後重新啓用它。以下是我獲取設備信息並識別關閉/禁用端口的代碼。我做了一些研究並瞭解了管道,open();和close();函數在javax.usb API中。請幫助我如何關閉或禁用USB端口。禁用USB端口

import java.io.UnsupportedEncodingException; 
import java.util.*; 
import javax.usb.*; 

public class USBDemo1 
{ 
    public static void main(String[] args) 
    throws UsbException, UnsupportedEncodingException, NullPointerException 
    { 
    UsbServices services = UsbHostManager.getUsbServices(); 
    UsbHub root = services.getRootUsbHub(); 
    List devices = root.getAttachedUsbDevices(); 
    Iterator iterator = devices.iterator(); 
    if(iterator.hasNext()) 
    { 
     UsbDevice device = (UsbDevice) iterator.next(); 
     UsbDeviceDescriptor descriptor = device.getUsbDeviceDescriptor(); 
     byte manufacturerCode = descriptor.iManufacturer(); 
     System.out.println("Manufacturer index: " + manufacturerCode); 
     System.out.println("Manufacturer string: " 
      + device.getString(manufacturerCode)); 
     byte productCode = descriptor.iProduct(); 
     System.out.println("Product index: " + productCode); 
     System.out.println("Product string: " + device.getString(productCode)); 
     byte serialCode = descriptor.iSerialNumber(); 
     System.out.println("Serial number index: " + serialCode); 
     System.out.println("Serial Number string: " + device.getString(serialCode)); 
     System.out.println("Vendor ID: 0x" + Integer.toHexString(descriptor.idVendor())); 
     System.out.println("Product ID: 0x" 
      + Integer.toHexString(descriptor.idProduct())); 
     System.out.println("Class: " + descriptor.bDeviceClass()); 
     System.out.println("Subclass: " + descriptor.bDeviceSubClass()); 
     System.out.println("Protocol: " + descriptor.bDeviceProtocol()); 

    } 
    } 
} 
+1

你究竟想在這裏做什麼?就像,你期待什麼結果「關閉或禁用USB端口」? – duskwuff

+0

@duskwuff在上面的代碼中,我只是獲取有關連接到USB端口的設備的信息(以確定要禁用哪個端口)。我期望禁用USB端口的結果是阻止/關閉與特定設備的通信。 –

+0

@Tdorno像我之前說過的,我是javax.usb API的新手。所以我希望能得到一個關於如何接近它的方向。我試着用close()函數關閉管道。 –

回答

0

安裝devcon應用程序。使用批處理文件中的「devcon remove」命令刪除特定的USB鍵盤,然後用java代碼執行批處理文件。現在鍵盤不會接收任何數據,直到您運行命令「devcon rescan」。