我正在尝试发送包含按钮按下的文本的数据包,然后在wireshark上捕获它。我正在使用OSC / UDP,虽然代码看似正确,但我没有看到手机发送的任何数据包。
目前,我只使用OSCPortOut:
OSCPortOut reciever ;
OSCListener listener ;
OSCPortOut sender ;
String sendIPaddr;
int sendPort;
然后,OnCreate,我正在连接到指定的IP(我的电脑)。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.playerlogin);
setContentView(R.layout.activity_main);
Size = (SeekBar)findViewById(R.id.size);
Speed =(SeekBar)findViewById(R.id.speed);
Agility=(SeekBar)findViewById(R.id.agility);
Vision =(SeekBar)findViewById(R.id.vision);
text = (TextView) findViewById(R.id.text);
Size.setOnSeekBarChangeListener(this);
Speed.setOnSeekBarChangeListener(this);
Agility.setOnSeekBarChangeListener(this);
Vision.setOnSeekBarChangeListener(this);
try {
InetAddress otherIP = InetAddress.getByName("192.168.1.2");
sender = new OSCPortOut(otherIP,5000);
}catch (Exception e){
text.setText("IO Exc: "
+ e.getClass().getName() + ", "
+ e.getMessage());
}
}
单击单选按钮,我尝试将与单击相对应的文本发送到网络。
public void onRadioClick(View v) {
n = (RadioButton) findViewById(R.id.north);
s = (RadioButton) findViewById(R.id.south);
e = (RadioButton) findViewById(R.id.east);
w = (RadioButton) findViewById(R.id.west);
c = (RadioButton) findViewById(R.id.center);
//String direction = text.setText();
// or, you can check each radiobutton and find which one is checked.
if (n.isChecked()) {
text.setText("North");
} else if (s.isChecked()) {
text.setText("South");
} else if (e.isChecked()) {
text.setText("East");
} else if (w.isChecked()) {
text.setText("West");
} else if (c.isChecked()) {
text.setText("Center");
}
String direction = String.valueOf(text);
try{
sender.send(new OSCMessage(direction));
}catch(Exception E){
}
最初,访问端口时出现问题,但是通过向清单添加权限解决了这个问题。有没有人知道为什么我没有在我捕获的任何UDP数据包中看到North,South,East West或Center?
答案 0 :(得分:0)
在MainCativity中,必须为任何网络通信添加此内容:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}