【问题标题】:Android naming a P2P deviceAndroid 命名 P2P 设备
【发布时间】:2015-02-22 22:31:56
【问题描述】:

我想知道是否有人可以对有关 WifiP2pDevice 的名称字段的问题有所了解。

您可能已经知道,WifiP2pDevice 有一个设备名称字段,但没有“设置”设备名称的方法,只能读取它。

如本文所述: How change the device name in WiFi direct p2p?

可以通过反射设置设备的名称。我知道这是不好的习惯,但由于 API 不支持此功能并且我的应用程序中需要此功能,我该怎么做呢?

到目前为止,我的代码:

    try
    {
        method = myManager.getClass().getMethod("setDeviceName",  new Class[] { WifiP2pManager.Channel.class, String.class,
                WifiP2pManager.ActionListener.class });
        method.invoke(myManager, myChannel, getIntent().getStringExtra(NETWORK_NAME));
        Toast.makeText(this, "Name set successful", Toast.LENGTH_SHORT).show();
    }

当前,应用程序到达这一点并返回 NoSuchMethodException,暗示 setDeviceName 不存在。关于 java 反射的任何帮助/替代方法?

编辑 1:根据建议,我已将代码更改为使用 WifiP2pManager 而不是 WifiP2pDevice 来执行 getClass() 调用,但是,程序在 method.invoke 行崩溃,声称 setDeviceName 方法期望3 个争论,但只收到 2 个(即使现在的模式代码正在发送 3 个)。

【问题讨论】:

    标签: java android reflection p2p wifip2p


    【解决方案1】:

    answer to the question you linked的变量命名来看:

    Method m = wpm.getClass().getMethod(
                "setDeviceName",
                new Class[] { WifiP2pManager.Channel.class, String.class,
                        WifiP2pManager.ActionListener.class });
    

    您可能应该在 WifiP2pManager 对象上而不是在 WifiP2pDevice 对象上调用此方法 (getClass().getMethod(...))(我认为 wpm 代表 WifiP2p M经理)。

    【讨论】:

    • 我也无法完成这项工作,但我仍在调整语法以查看是否在此洞察力方面取得任何进展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多