[英]Access connected MTP device without receiving USB_DEVICE_ATTACHED Intent?
[英]Asking intent to access USB device
我正在嘗試使用 UsbManager 從 USB 設備讀取數據。 我試圖從中讀取數據的設備是相機(但 android 將其識別為 USB 設備)我目前遇到的問題是它不允許我請求訪問 USB 設備的權限。 我在清單中擁有必要的使用權限和啟用的主機模式。
有什么建議為什么會發生以及如何解決?
我嘗試了兩種請求權限的方法
private fun requestPermission() {
requestCameraPermissionIfMissing { granted ->
if (granted){
usbComm = Camera(this, intArrayOf(7758))
usbComm.startUsbConnection()//find device from connected devices and establish connection
}
else
Toast.makeText(this, "Please Allow the Permission", Toast.LENGTH_LONG).show()
}
}
// Show popup to request permission to access camera if granted return true otherwise false
private fun requestCameraPermissionIfMissing(onResult: ((Boolean) -> Unit)) {
if (ContextCompat.checkSelfPermission(
this,
ACTION_USB_PERMISSION
) == PackageManager.PERMISSION_GRANTED
)
onResult(true)
else
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
onResult(it)
}.launch(ACTION_USB_PERMISSION)
}
另一個是:
val intent: PendingIntent = PendingIntent.getBroadcast(
context,
0,
Intent(ACTION_USB_PERMISSION),
PendingIntent.FLAG_MUTABLE
)
manager.requestPermission(m_device, intent)
兩者都沒有向我顯示設備上的請求,而且我在調試中也沒有看到已授予權限
而且,當我未經許可嘗試進行休閑時,我的應用程序崩潰了
var intf = m_device?.getInterface(0)!!
endpoint = intf.getEndpoint(0)
if (m_connection!!.claimInterface(intf, true)) { // Crashed here
Log.i("serial", "The interface was claimed successfully")
} else {
Log.i("serial", "The interface could not be claimed")
}
我找到了解決此問題的方法,以防萬一其他人也遇到過,您需要請求該應用程序的許可,在我的情況下,它是 Manifest.permission.CAMERA、Manifest.permission.RECORD_AUDIO,然后請求訪問許可設備,它將廣播意圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.