0

アプリで USB 接続イベントをリッスンしているブロードキャスト レシーバーがあります。コードは次のとおりです。

BroadcastReceiver mUsbReceiver; //Initialized elsewhere
void registerMyReceiver() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    this.registerReceiver(this.mUsbReceiver, filter);
}

このコードは、一部のデバイスでは正常に機能しますが、他のデバイスでは機能しません。タブレットなどの大型デバイスは、電話よりも成功しているようです。

このレシーバーが USB 接続を検出するデバイスとそうでないデバイスがあるのはなぜですか?

4

2 に答える 2

0

Samsung S3 Neoなどの一部のデバイスでは、外部電源の USB OTG ハブを使用して、USB ポートで 5V が不足する問題 [2] を解決しました。

于 2020-05-21T07:33:06.533 に答える
0

このような回答は、必要なケーブルと必要なピン接続に関する洞察を与え、これはドライバーと不良ケーブルの前提条件に関する情報を提供しましたが、どちらも上記の私の中心的な質問には答えませんでした.

Android ドキュメントには、検出されたデバイスにアクセスする方法に関する情報が記載されていましたが、実際にデバイスを接続するという私の問題に関しては役に立ちませんでした。

答えは、次のように述べているこの記事から得られました。

We have identified 3 requirements for an Android device to support USB Host Mode and be able to communicate 
with [The USB Device]:

[1] The Android device must be running version 4.1 (Jelly Bean) of the OS, or higher.
[2] The output power on the Android device's USB port should be 5V.
[3] The configuration file android.hardware.usb.host.xml must exist on the Android device in the folder
 /system/etc/permissions. The presence of this configuration file is what enables USB 
Host Mode on your Android device.

問題の xml ファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- This is the standard feature indicating that the device can communicate
    with USB devices as the USB host. -->
<permissions>
    <feature name="android.hardware.usb.host" />
</permissions>

ご覧のとおり、単純な XML ファイルです。ただし、この回答の概要にあるように、アクセスするにはルートレベルで行う必要があります。

ここでの結論は、OS がこのファイルをカーネル レベルで正しいディレクトリに設定するか、デバイスをルート化して事後的に配置する必要があるということです。

ここに画像の説明を入力

これら 2 つの解決策のいずれかで、この問題が解決されます。

于 2020-05-15T06:02:44.903 に答える