我想写使用CyberGarage讯连科技的JavaAPI UPnP控制点应用程序为Android。为了测试API我实现了一个非常简单的应用程序。在本申请中的UPnP控制点主动搜索任何的UPnP根设备,侦听响应和设备通知,并打印设备,这是在网络上可用的列表。
I want to write a UPnP control point application for Android using the CyberGarage "CyberLink for Java" API. To test the API I implemented a very simple application. In this application a UPnP control point actively searches for any UPnP root devices, listens for responses and device notifications, and prints a list of the devices, which are available on the network.
在应用程序运行在Android手机上,但是没有我的网络上的UPnP设备的发现。我想这两个不同的Android手机。要检查,如果这是一个Android的具体问题,我实现相同的功能作为一个Java控制台应用程序。有趣的是Java控制台应用程序的工作原理精绝,总是显示所有的UPnP设备我的网络上!
The app runs on an Android phone, but none of the UPnP devices on my network are found. I tried this on two different Android phones. To check out if this is an Android specific problem, I implemented the same functionality as a Java console application. Interestingly enough the Java console application works absolutely fine and always shows all of the UPnP devices on my network!
那么,为什么在Android这个不工作?请注意,在Android应用程序,我不得不实施使用AsyncTask的一个单独的线程在网络特定的功能。否则我得到的错误,因为我不应该在UI线程上运行此。但是,这不应该成为问题,对吗?
So why does this not work on Android? Note, in the Android app I had to implement the network specific functionality on a seperate thread using AsyncTask. Otherwise I get errors, because I am not supposed to run this on the UI thread. But this should not be the problem, am I right?
下面的两个应用程序的源$ C $ C。
Below the source code of the two applications.
Android应用程序:
Android application:
MainActivity.java
MainActivity.java
package com.example.controller_v1;
import org.cybergarage.upnp.DeviceList;
import org.cybergarage.upnp.UPnP;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UPnP.setEnable(UPnP.USE_ONLY_IPV4_ADDR);
new StartControlPointTask().execute();
}
private class StartControlPointTask extends AsyncTask {
public static final String TAG = "StartControlPointTask";
@Override
protected Object doInBackground(Object... params) {
Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
MyControlPoint controlPoint = new MyControlPoint();
controlPoint.start();
// controlPoint.search();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
DeviceList rootDevices = controlPoint.getDeviceList();
int numDevices = rootDevices.size();
if (numDevices > 0) {
for (int i = 0; i < numDevices; i++) {
Log.i(TAG, "device " + i + ": " + rootDevices.getDevice(i).getFriendlyName());
}
} else {
Log.i(TAG, "no root devices found");//
}
return null;
}
}
}
MyControlPoint.java
MyControlPoint.java
package com.example.controller_v1;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.device.NotifyListener;
import org.cybergarage.upnp.device.SearchResponseListener;
import org.cybergarage.upnp.ssdp.SSDPPacket;
import android.util.Log;
public class MyControlPoint extends ControlPoint implements NotifyListener, SearchResponseListener {
public MyControlPoint() {
addNotifyListener(this);
addSearchResponseListener(this);
}
@Override
public void deviceNotifyReceived(SSDPPacket ssdpPacket) { // NotifyListener
final String TAG = "deviceNotifyReceived";
Log.i(TAG, "executed");
}
@Override
public void deviceSearchResponseReceived(SSDPPacket ssdpPacket) { // SearchResponseListener
final String TAG = "deviceSearchResponseReceived";
Log.i(TAG, "executed");
}
}
AndroidManifest.xml中
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.controller_v1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.controller_v1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Java控制台应用程序:
Java console application:
Main.java
Main.java
import org.cybergarage.upnp.DeviceList;
import org.cybergarage.upnp.UPnP;
public class Main {
public Main() {
UPnP.setEnable(UPnP.USE_ONLY_IPV4_ADDR);
MyControlPoint controlPoint = new MyControlPoint();
controlPoint.start();
// controlPoint.search();
try {
Thread.sleep(5000); // wait for devices to be found
} catch (InterruptedException e) {
e.printStackTrace();
}
DeviceList rootDevices = controlPoint.getDeviceList();
int numDevices = rootDevices.size();
if (numDevices > 0) {
for (int i = 0; i < numDevices; i++) {
System.out.println("found device " + i + ": " + rootDevices.getDevice(i).getFriendlyName());
}
} else {
System.out.println("no root devices found");
}
}
public static void main(String[] args) {
new Main();
}
}
MyControlPoint.java
MyControlPoint.java
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.device.NotifyListener;
import org.cybergarage.upnp.device.SearchResponseListener;
import org.cybergarage.upnp.ssdp.SSDPPacket;
public class MyControlPoint extends ControlPoint implements /*DeviceChangeListener,*/ NotifyListener, SearchResponseListener {
public MyControlPoint() {
addNotifyListener(this);
addSearchResponseListener(this);
}
@Override
public void deviceNotifyReceived(SSDPPacket packet) { // NotifyListener
System.out.println("deviceNotifyReceived");
}
@Override
public void deviceSearchResponseReceived(SSDPPacket packet) { // SearchResponseListener
System.out.println("deviceSearchReceived");
}
}
我没有任何想法,为什么Java控制台应用程序的工作原理及Android应用程序没有。我没有得到的文件任何回答。任何人可以帮助我吗?
I don't have any idea, why the java console application works and the android application doesn't. I don't get any answer from the documentation. Can anybody help me?
在我的情况下,它显示的是所有的设备,可能是因为我用电话核对,而仿真器。
In my case it is displaying all the devices, may be because I'm checking with Phone rather emulator.