2017-01-25 12 views
0
public class HardwareActivity extends AppCompatActivity implements View.OnClickListener { 
    private static final String ACTION_USB_PERMISSION = "com.android.missilelauncher.USB_PERMISSION"; 
    private PendingIntent PermissionIntent; 
    private Button button; 
    private ImageView back; 
    private TextView textInfo; 
    private UsbDevice usbDevice; 
    private UsbManager usbManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String action = getIntent().getAction(); 
     setContentView(R.layout.activity_hardware); 
     button = (Button) findViewById(R.id.check); 
     back = (ImageView) findViewById(R.id.back); 
     textInfo = (TextView) findViewById(R.id.usb_info); 
     back.setOnClickListener(this); 
     button.setOnClickListener(this); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_ATTACHED) || intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { 
      Log.d("USB DEVICE ", " device attached"); 
     } else if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED) || intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { 
      Log.d("USB DEVICE ", " device detached"); 
     } 
    } 

    private void checkInfo() { 
     usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
     PermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
       ACTION_USB_PERMISSION), 0); 
     HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); 
     Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
     String deviceString = ""; 
     while (deviceIterator.hasNext()) { 
      usbDevice = deviceIterator.next(); 
      deviceString += "\n" + "DeviceID: " + usbDevice.getDeviceId() + "\n" + 
        "DeviceName: " + usbDevice.getDeviceName() + "\n" + 
        "DeviceClass: " + usbDevice.getDeviceClass() + " - " + 
        "VendorID: " + usbDevice.getVendorId() + "\n" + 
        "ProductID: " + usbDevice.getProductId() + "\n" ; 
     } 
     Log.d("USB Info", "USB " + deviceString); 
     textInfo.setText(deviceString); 
    } 

答えて

0

を動作しませんでしたお使いのデバイスがUSBホストを有効にするには、システム権限

android.hardware.usb.host.xml内部/システムの/ etc /パーミッション/

を持っていることを確認してください

<permissions> 

:APIのサポートあなたはandroid.hardware.usb.host.xmlと次の行を含む名前のファイルを追加する必要がありますそのフォルダ内のフォルダに

/システムの/ etc /パーミッション は

handheld_core_hardware.xmlまたはtablet_core_hardware.xml という名前のファイルを検索し、

<feature name="android.hardware.usb.host" /> 
into <permissions> section. 

再起動して、あなたのデバイスを追加します。 USBホストAPIが動作するはずです。

手順:ファイルと、指定された

adbのプッシュandroid.hardware.usbとしてandroid.hardware.usb.host.xml作成 ADBプル/system/etc/permissions/tablet_core_hardware.xml

更新.host.xml/system/etc/permissions adb push tablet_core_hardware.xml/system/etc/permissions リブートします。

関連する問題