如何为当前设备设置WifiP2pDevice.deviceName? [英] How to WifiP2pDevice.deviceName for current device?
问题描述
我知道有人问类似的问题,但答案对我没有用.我尝试了此答案,但它抛出了空指针异常.我还看到了此答案,但是WifiP2pManager没有任何可返回设备名称的属性或方法.
I know similar questions are asked but the answers didn't work for me. I tried this answer but it throws null pointer exception. I also saw this answer but WifiP2pManager does not have any property or method that returns device name.
任何人都可以帮忙吗?
我基本上想向用户显示其设备名称,并让他们在要设置自定义名称时对其进行更改.
I basically want to show user their device name and let them change it if they want to set custom name.
谢谢.
推荐答案
如果您仍在寻找答案,请按以下步骤操作:
If you're still looking for the answer, here's how:
- 标识自己的设备名称
在您的广播接收器中收到 WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
意向后,此功能便可用.只需使用 deviceName
成员变量,如下所示:
This becomes available upon receiving the WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
intent in your broadcast receiver. Simply use the deviceName
member variable like so:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
WifiP2pDevice self = (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
// Now self.deviceName gives you own device name
} else if(WifiP2pManager.WIFI_P2P...) {
...
2.更改自己的设备名称
2. Change own device name
没有按照开发者文档使用 WifiP2pManager
更改设备名称的方法,尽管源代码中存在公共的 setDeviceName()
方法,但是您可以不能在您的对象上调用它(可能使开发人员无法在代表附近对等设备的对象上调用它).对我有用的是获得一个表示该方法的 Method
对象,并在我的 WifiP2pManager
实例上调用它:
There's no method to change the device name using the WifiP2pManager
as per the develper docs, and although a public setDeviceName()
method exists in the source code, you can't call it on your object (probably to keep devs from calling it on an object representing a nearby peer device). What worked for me was to obtain a Method
object representing said method and invoking it on my WifiP2pManager
instance:
private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
...
public void changeDeviceName(String deviceNewName) {
try {
Method method = manager.getClass().getMethod("setDeviceName", WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);
method.invoke(manager, channel, deviceNewName, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Name successfully changed", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int reason) {
Toast.makeText(MainActivity.this, "Request failed: " + reason, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Name change failed: " + reason);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
或者,用户可以从高级WiFi设置(偏好设置">高级">"WiFi Direct">配置设备")手动重命名其设备,
Alternatively, users can rename their device manually from the advanced WiFi settings (Preferences > Advanced > WiFi Direct > Configure Device),
从Pie开始,非SDK接口(本质上是用@hide标记的类,变量或方法,您可以使用反射访问)的使用受到限制,最终将被禁止.上面的方法目前已列入灰色列表(这意味着将来可能会删除对反映它的支持).在此处阅读更多信息: https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces
Starting with Pie, use of non-SDK interfaces (essentially classes, variables or methods marked with @hide, which you access using reflection) is being restricted and will eventually be disallowed. The above method is currently greylisted (which means support for reflecting it might be removed in the future). Read up more here: https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces
这篇关于如何为当前设备设置WifiP2pDevice.deviceName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!