【发布时间】:2018-07-20 01:37:01
【问题描述】:
我正在开发一个应用程序,它需要在任何 Powerbank 设备使用 USB 连接时检测连接。
每当检测到与 Powerbank 的连接时,我都需要获取该连接的 Powerbank 的 品牌名称、电池容量、制造商等信息设备。
为此,我创建了广播接收器来检测移动电源与我的设备的连接。
代码:
public class PowerBankBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
// Do something when power connected
Toast.makeText(context, "ACTION_POWER_CONNECTED", Toast.LENGTH_SHORT).show();
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
// Do something when power disconnected
Toast.makeText(context, "ACTION_POWER_DISCONNECTED", Toast.LENGTH_SHORT).show();
}
}
}
在 manifest 里面我添加了这个。
<receiver android:name=".utilities.PowerBankBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
每当我连接和断开移动电源设备时,它都会给我 ACTION_POWER_CONNECTED toast 消息。
所以我的问题是:连接后如何获取 Powerbank 设备信息?
我还查看了 Play store 上的一个应用程序,它准确地显示了连接的 Powerbank 设备信息。我想实现同样的东西。
任何帮助将不胜感激。
【问题讨论】:
-
我非常怀疑您能否获得制造商的详细信息,因为大多数移动电源仅使用 USB 端口的电源引脚 +5v 和 gnd,根本不使用数据引脚。至于您关联的应用,评论说明了一切
标签: android usb device usb-drive