send/receive OSC messages to Android with JavaOSC

2.6k views Asked by At

I'm trying to write sample Android application that allows to send and receive OSC messages using JavaOSC library. In the example we are sending the current value of a simple Android seekBar with OSC message.

The LogCat says:

09-03 04:56:25.695: I/System.out(1235): can not send

Here is my sample code, unfortunately it does not work (it seems that i have an exception while trying to send message). Maybe someone knows what is it wrong here?

I'm totally new in Android dev., thank's for all advices...

 package com.remote.secondtest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;





public class MainActivity extends Activity implements OnSeekBarChangeListener{

    private SeekBar myBar;
    private TextView textProgress, textAction;
    public OSCPortOut sender = null;
    private OSCPortIn  receiver;
    public InetAddress targetIP;
    int port = 5001;
    protected int sliderValue;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.myscreen);

        myBar = (SeekBar)findViewById(R.id.seekBar1);
        myBar.setOnSeekBarChangeListener(this);        
        textProgress = (TextView)findViewById(R.id.textViewProgress);
        textAction = (TextView)findViewById(R.id.textViewAction);

    } 

    public void setConnection(){
        try {
            targetIP = InetAddress.getByName("192.168.0.104");
            //targetIP = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {          
            e.printStackTrace();        }        

        try {
            sender = new OSCPortOut(targetIP, 4444); //------set up outgoing ------
        } catch (SocketException e) {
            e.printStackTrace();
        }

        /*try {                                     //------set up incoming-------
            receiver = new OSCPortIn(4444);
        } catch (SocketException e) {
            e.printStackTrace();
        } */ 

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    } 


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textProgress.setText("The value is: " + progress);
        textAction.setText("changing");

        sliderValue = progress;
        sendMyOscMessage();
        //receiveMyOscMessage();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {     
        textAction.setText("starting to track touch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        seekBar.setSecondaryProgress(seekBar.getProgress());
        textAction.setText("ended tracking touch");     

    }

    public void sendMyOscMessage(){
        Object args [] = new Object[2];
        args[0] = "Current_Value_is: ";
        args[1] = sliderValue;
        OSCMessage msg = new OSCMessage("/test_from_device", args);

        try {
            sender.send(msg);
            System.out.println("OSC message sent!");
            System.out.println(args[0].toString() + args[1]);
        } catch (Exception e) {
            System.out.println("can not send");
            //e.printStackTrace();
        }

    }

    /*public void receiveMyOscMessage(){
        OSCListener myListener = new OSCListener() {

            @Override
            public void acceptMessage(Date time, OSCMessage message) {

                System.out.println("myMessage_received!");              
                //Object [] args = message.getArguments();
                //String myTitle = args[0].toString();
                //int currentValue = (Integer)args[1];
            }
        }; 

        receiver.addListener("/test_to_device", myListener);
        receiver.startListening();

    }*/


}

UPD: the OSCMessage constructor was deprecated but after i modified it for a valid constructor there is no changes...

OSCMessage msg = new OSCMessage();
    msg.setAddress("192.168.0.104");
    msg.addArgument(args);

Between a lot of android errors log says:

09-03 07:56:00.794: E/AndroidRuntime(2408): FATAL EXCEPTION: main 09-03 07:56:00.794: E/AndroidRuntime(2408): java.lang.NullPointerException 09-03 07:56:00.794: E/AndroidRuntime(2408): at com.remote.secondtest.MainActivity.sendMyOscMessage(MainActivity.java:132) 09-03 07:56:00.794: E/AndroidRuntime(2408): at com.remote.secondtest.MainActivity.onProgressChanged(MainActivity.java:93) 09-03 07:56:00.794: E/AndroidRuntime(2408): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)

1

There are 1 answers

0
JonasE On

You need permissions to access Internet.

Since the line numbers don't match your errors i can't be sure but I'm guessing this is the most common error, leading to "can not send".