你好,处理程序开发人员,
我使用OSCP5连接两个处理草图,一个是来自安卓设备,另一个是在我的计算机中,通过WiFi。
我已经尝试过直接在安装程序()上的新NetAddress上键入我的计算机的IP地址,而且它工作得很好。然而,我认为最好让用户有机会将计算机的IP地址更改到发送消息的地方(它可能会改变IP或改变网络,然后android应用程序就不能工作了)。
我使用一个文本字段让用户输入IP地址,然后在一个名为ipAddress的字符串中使用它,该字符串进入进入myRemoteLocation的NetAddress中。
这是我的代码(这是打电话的):
import oscP5.*;
import netP5.*;
import apwidgets.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
PWidgetContainer widgetContainer;
PEditText textField;
PButton button1;
PImage fondo;
boolean ValidIP = false;
String ipAddress = "192.168.0.107";
void setup() {
size(480,800);
smooth();
fondo = loadImage("fondoAnd.jpg");
widgetContainer = new PWidgetContainer(this);
textField = new PEditText(10,40,380,120);
button1 = new PButton(10,190,200,100,"Conectar");
widgetContainer.addWidget(textField);
widgetContainer.addWidget(button1);
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress(ipAddress,12000);
}
void draw() {
if (ValidIP == true){
widgetContainer.hide();
myRemoteLocation = new NetAddress(ipAddress,12000);
background(fondo);
}
else if (ValidIP == false){
background(0);
textSize(20);
text("Ingrese un IP válido",10,30);
widgetContainer.show();
}
}
void mousePressed(){
OscMessage myMessage = new OscMessage("/test");
myMessage.add(8);
oscP5.send(myMessage, myRemoteLocation);
println("enviomensaje"); //I print this in the console to know if it sends a message
}
void onClickWidget(PWidget widget){
if(widget == button1){
ipAddress = textField.getText();
myRemoteLocation = new NetAddress(ipAddress,12000);
ValidIP = true;
}
}发布于 2013-09-15 11:01:04
我将OSCP5用于一个项目,并将许多Android设备用作OSC客户端,并将计算机用作OSCP5。我建议从广播样本开始。注意,服务器侦听端口32000上任何ip上的"/ server /connect“消息。这样,您就不需要将android设备的ip硬编码到服务器中,但在这个阶段,您仍然需要将服务器的ip硬编码到客户机android设备中。
我克服这个限制的方法是向每个人发送"/server/connect“消息(”255.255.255.255“)。一旦服务器连接到ip,它就会向客户端发送一条消息。然后,客户机将从该消息中获取ip地址,这样它就不需要将所有后续消息发送到"255.255.255.255“。
我在android设备上使用了android,在计算机上使用了处理,所以我的语法看起来不一样,但是您应该能够理解OSC部分。以下是一个简化的版本:
package com.hirschandmann.updclient.network;
import netP5.NetAddress;
import oscP5.OscArgument;
import oscP5.OscEventListener;
import oscP5.OscMessage;
import oscP5.OscP5;
import oscP5.OscStatus;
import android.util.Log;
public class NetMan implements OscEventListener {
public static final int PORT_IN = 12000;
public static final int PORT_OUT = 32000;
public static NetAddress SERVER;
public static NetAddress EVERYTHING = new NetAddress("255.255.255.255",PORT_OUT);
private static final String TAG = "NetMan";
private OscP5 osc;
public NetMan(){
osc = new OscP5(this, PORT_IN);
}
public void oscEvent(OscMessage m) {
String pattern = m.addrPattern();
if(SERVER == null) {
SERVER = new NetAddress(m.address().replace("/", ""), PORT_OUT);
System.out.println(SERVER);
}
}
public void connect(){
Log.d("NetMan","connect");
new ConnectTask().execute();
}
public void disconnect(){
new DisconnectTask().execute();
}
@Override
public void oscStatus(OscStatus status) {
System.out.println(status);
}
}ConnectTask:
package com.hirschandmann.udpclient.network;
import netP5.NetAddress;
import oscP5.OscMessage;
import oscP5.OscP5;
import android.os.AsyncTask;
public class ConnectTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
System.out.println("sending connect message: " + NetMan.PORT_OUT);
OscMessage m = new OscMessage("/server/connect",new Object[0]);
try{
OscP5.flush(m,NetMan.SERVER != null ? NetMan.SERVER : NetMan.EVERYTHING);
}catch(NullPointerException e) {
System.err.println(e.getMessage());
}
if(NetMan.SERVER != null) System.out.println("NetMan.SERVER: " + NetMan.SERVER);
return null;
}
}因此,您应该在计算机上试用溴化样品,如下所示:
import netP5.*;
import apwidgets.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
PWidgetContainer widgetContainer;
PEditText textField;
PButton button1;
PImage fondo;
boolean ValidIP = false;
String ipAddress = "255.255.255.255";
void setup() {
size(480,800);
smooth();
fondo = loadImage("fondoAnd.jpg");
widgetContainer = new PWidgetContainer(this);
textField = new PEditText(10,40,380,120);
button1 = new PButton(10,190,200,100,"Conectar");
widgetContainer.addWidget(textField);
widgetContainer.addWidget(button1);
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress(ipAddress,12000);
}
void draw() {
if (ValidIP == true){
widgetContainer.hide();
myRemoteLocation = new NetAddress(ipAddress,12000);
background(fondo);
}
else if (ValidIP == false){
background(0);
textSize(20);
text("Ingrese un IP válido",10,30);
widgetContainer.show();
}
}
void mousePressed(){
OscMessage myMessage = new OscMessage("/server/connect");
myMessage.add(8);
oscP5.send(myMessage, myRemoteLocation);
println("enviomensaje"); //I print this in the console to know if it sends a message
}
void onClickWidget(PWidget widget){
if(widget == button1){
ipAddress = textField.getText();
myRemoteLocation = new NetAddress(ipAddress,12000);
ValidIP = true;
}
}
void oscEvent(OscMessage m) {
String pattern = m.addrPattern();
if(ipAddress.equals("255.255.255.255")) {
ipAddress = m.address().replace("/", "");
System.out.println("server is at: " + ipAddress);
}
}上面的代码没有经过测试,但应该会有所帮助。
注意,我在UDP模式中使用了OSC。还存在TCP模式。使用UDP,您可以将数据存储到多个ips,但并不能保证您的消息将被访问,并且每条消息的数据最多可达64K。因为UDP数据包上没有确认,所以它比TCP稍快一些。有了TCP,所有的数据包都保证到达对方,但速度昂贵,广播能力不足。此外,您还可以发送超过64K的数据。在我的应用程序中,我必须发送9MP和12 my图像,所以我使用了UDP和TCP:
我遇到的另一个问题是无线网络在Android上的速度。Wifi在android设备上的处理方式与在计算机上的处理方式不同。操作系统是为了尽可能地保留电池,但这也意味着,如果你的设备上没有什么活动,Wifi将进入低延迟模式:它节省电池,但它的连接速度太慢,所以如果你需要快速响应通过OSC,这是一个问题。我还没有找到合适的解决方案的问题,除了锁定wifi和电源要永远开着:用电池寿命换取一点Wifi的响应能力。我要是知道这个问题的解决办法就好了。如果你有任何建议,请分享。
HTH
https://stackoverflow.com/questions/18796913
复制相似问题