Android 实现UPNP功能

          通过upnp端口映射 可以实现外网访问到当前android设备。当然你的路由需要打开upnp功能

          使用下面的方法进行端口映射,这里默认使用7878端口也可以另行设置

private void discoverUPNP() {
		int discoveryTiemout = 5000; // 5 secs
		try {
			System.out.println("Looking for Internet Gateway Device...");
			InternetGatewayDevice[] IGDs = InternetGatewayDevice.getDevices(discoveryTiemout);
			if (IGDs != null) {
				for (int i = 0; i < IGDs.length; i++) {
					InternetGatewayDevice testIGD = IGDs[i];
					System.out.println("\t\nFound device " +testIGD.getIGDRootDevice().getModelName());
					System.out.println("External IP address: " +testIGD.getExternalIPAddress());
					Integer natTableSize = testIGD.getNatTableSize() == null ? 0 : testIGD.getNatTableSize();
					 S
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP8266使用UPnP实现发现设备功能的基本流程如下: 1. ESP8266作为UPnP控制点,发送搜索请求(M-SEARCH)到网络中的UPnP设备。 2. 网络中的UPnP设备接收到搜索请求后,返回设备描述文档(Device Description Document)。 3. ESP8266解析设备描述文档,获取设备的基本信息、服务信息等。 4. ESP8266根据设备描述文档中的信息,与设备进行通信,实现相应的功能。 在ESP8266中,可以使用UPnP库来实现UPnP协议栈,包括搜索设备、解析设备描述文档等功能。以下是一个简单的搜索设备的示例代码: ``` #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <UPnP.h> WiFiUDP udp; UPnP upnp(udp); void setup() { Serial.begin(115200); WiFi.begin("ssid", "password"); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); udp.begin(1900); upnp.searchRootDevice(); } void loop() { int n = upnp.parseResponse(); if (n > 0) { Serial.print("Found "); Serial.print(n); Serial.println(" devices"); for (int i = 0; i < n; i++) { Serial.println(upnp.getDeviceType(i)); Serial.println(upnp.getFriendlyName(i)); Serial.println(upnp.getLocation(i)); } } delay(1000); } ``` 该代码中,首先连接WiFi网络,然后创建一个UDP对象和一个UPnP对象。在setup函数中,调用upnp.searchRootDevice()方法发送搜索请求。在loop函数中,调用upnp.parseResponse()方法解析设备描述文档,获取搜索到的设备信息。最后,使用upnp.getDeviceType(i)、upnp.getFriendlyName(i)、upnp.getLocation(i)等方法获取设备的类型、名称、位置等信息。 需要注意的是,UPnP搜索请求需要发送到多播地址239.255.255.250,端口号为1900。同时,设备描述文档可能存在多个版本,需要根据版本进行解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值