【问题标题】:Programatically get connected Powerbank device information and battery capacity of Powerbank device以编程方式获取已连接的移动电源设备信息和移动电源设备的电池容量
【发布时间】: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


【解决方案1】:

大多数适用于 Android 的移动电源都符合“电池充电”spec 的要求,只需短接 D+ 和 D- 数据线,防止任何 USB 数据流。

这样一来,Android 设备就无法判断它是连接到移动电源还是仅仅连接到插入墙上插座的充电器。

如果没有适当的数据渠道,您也无法获取品牌名称等信息。

从 OP 链接的应用程序有很多负面评论和 cmets - 不要指望它可以正常工作。这可能只是一个聪明的推广告或加密货币矿工的假货。

【讨论】:

    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多