当玻璃通过蓝牙连接到我的手机时,如何获得玻璃的IP? [英] How can i get IP of my glass when it is connected by bluetooth to my mobile?

查看:96
本文介绍了当玻璃通过蓝牙连接到我的手机时,如何获得玻璃的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的手机和蓝牙手机配对了.我的手机已连接到WiFi网络(玻璃没有连接到WiFi,而是使用了手机的WiFi).

I have my glass paired by bluetooth with my mobile. My mobile is connected to a WiFi network (and my glass isn't connected to WiFi, it uses the WiFi of my mobile).

我知道我可以使用以下代码获取IP本地ip地址:

I know I can get IP local ip addres with this code:

        WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ip);

如果我在平板电脑或智能手机上运行此代码,就可以正常工作,但是如果我在玻璃上运行该代码,则会获得ip 0.0.0.0(请记住,我的玻璃通过蓝牙连接到了我的手机,而这个是有网络的连接).

It's work OK if i run this code in a tablet o smartphone, but if i run it in my glass i get ip 0.0.0.0 ( remeber that my glass is connected by bluetooth to my mobile and this one is which has network connection).

那么,有什么建议吗?我需要这个,因为我的玻璃上有一个插座,并且需要IP.

So, any suggestions? I need this because i have a Socket in my glass and i need the IP.

谢谢!

推荐答案

现在任何活动都可以调用该代码

The code can now be called by any activity

public String getIP(){
    WifiManager wifiMgr = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ip);
}

对任何有兴趣的人.上面的代码DID在Activity中对我有用.试试:

To anyone who is interested. The above code DID work for me in an Activity. Try:

Toast toast = Toast.makeText(this,
              "IP Address:\n\r"+getIP(this), Toast.LENGTH_LONG);
toast.show();

也许您的用户界面没有更新,或将其打印到日志中.另一种可能性是,您需要在清单中添加以下内容:

Perhaps your UI isn't updating, or print it to the log. Another possibility is that you need to add the following to your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

不确定是否同时需要两者,但我的应用程序同时需要两者,因为我进行了大量的网络连接,听起来也是如此.

Not sure if you need both, but I need both for my application as I do a lot of networking which it sounds like you are too.

这篇关于当玻璃通过蓝牙连接到我的手机时,如何获得玻璃的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆