Android 에서 usb 권한 을 가 져 오 는 두 가지 방법
최근 작업 중 몇 개의 USB 모듈 이 안 드 로 이 드 플랫폼 에서 적합 하 게 사용 되 는 상황 을 만 났 기 때문에 USB 권한 으로 문 제 를 가 져 와 야 합 니 다.
\#\#USB 권한 획득 은 다음 과 같은 두 가지 방법 이 있 습 니 다.
1.AndroidManifest.xml 파일 에서 다음 과 같이 설정 합 니 다.
<activity
android:name=".DemoCustomAndroidUSBActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- USB -->
<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/device_filter" />
<!-- USB END -->
</activity>
주의해 야 할 것 은:그 중 devicefilter.xml 에 사용 가능 한 usb 장치 가 표시 되 어 있 습 니 다.usb 장치 가 휴대 전화 에 연결 되면 app 은 이 usb 의 권한 을 가 져 올 수 있 는 지 여 부 를 자동 으로 물 습 니 다.
device_filter.xml 배치 위 치 는 다음 그림 과 같 습 니 다.
device_filter.xml 의 내용 은:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1027" product-id="24577" />
<usb-device vendor-id="3405" product-id="567" />
</resources>
usb 장 치 는 vendor-id(제조 업 체 id)와 produt-id(제품 id)를 통 해 정의 되 었 습 니 다.여기 에는 Liux 의 usb 장치 제조 업 체 id 와 제품 id 의 집합 이 있 습 니 다.Android usb 장치 의 참고 가 될 수 있 습 니 다.2.동적 코드 획득
2.1 코드 에서 가 져 오기(전 제 는 USB 권한 을 신청 할 usb device 로 지정 되 어 있 음)
// USB ACTION
private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";
// USB USB
// mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
// HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
// List<UsbDevice> deviceList = new ArrayList<UsbDevice>();
// for (UsbDevice device : devices.values()) {
// if (3540==device.getVendorId() && 567==device.getProductId()) {// vid pid
// currentDevice=device;
// }
// }
// USB
private void getUsbPermission(UsbDevice mUSBDevice) {
UltraLog.d(" USB ");
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
mContext.registerReceiver(mUsbReceiver, filter);
mUsbManager.requestPermission(mUSBDevice, pendingIntent); // , /
// ,
// long start = System.currentTimeMillis();
// while (!mUsbManager.hasPermission(mUSBDevice)) {
// long current = System.currentTimeMillis();
// if ((current - start) > 3500) {
// break;
// }
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
2.2 등록 방송 수신 자
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@SuppressLint("NewApi")
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
mContext.unregisterReceiver(mUsbReceiver);
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
&& currentDevice.equals(device)) {
//TODO , USB
}else{
//
}
}
}
}
};
총결산위 에서 말 한 것 은 소 편 이 여러분 에 게 소개 한 안 드 로 이 드 가 usb 권한 을 얻 는 두 가지 방법 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
만약 당신 이 본문 이 당신 에 게 도움 이 된다 고 생각한다 면,전 재 를 환영 합 니 다.번 거 로 우 시 겠 지만 출처 를 밝 혀 주 십시오.감사합니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.