0

I've been following a tutorial where I'm trying to use the JavaOSC library to add functionality for a native Android app to send OSC messages.

Below is the code I have for setting up an OSC thread:

    private String myIP = "192.168.0.3";
    private int myPort = 1234;

    private OSCPortOut oscPortOut;

    // This thread will contain all the code that pertains to OSC
  private Thread oscThread = new Thread() {
    @Override
    public void run() {
        try {
            // Connect to some IP address and port
            oscPortOut = new OSCPortOut(InetAddress.getByName(myIP), myPort);
            Log.v(TAG, "CREATED PORT");
        } catch(UnknownHostException e) {
            Log.v(TAG, "error is", e);
            // Error handling when your IP isn't found
            return;
        } catch(Exception e) {
            // Error handling for any other errors
            Log.v(TAG, "error is", e);
            return;
        }


      /* The second part of the run() method loops infinitely and sends messages every 500
       * milliseconds.
       */
        while (true) {
            if (oscPortOut != null) {
                // Creating the message
                Log.v(TAG, "CREATED MESSAGE");
                Object[] thingsToSend = new Object[3];
                thingsToSend[0] = "Hello World";
                thingsToSend[1] = 12345;
                thingsToSend[2] = 1.2345;

          /* The version of JavaOSC from the Maven Repository is slightly different from the one
           * from the download link on the main website at the time of writing this tutorial.
           *
           * The Maven Repository version (used here), takes a Collection, which is why we need
           * Arrays.asList(thingsToSend).
           *
           * If you're using the downloadable version for some reason, you should switch the
           * commented and uncommented lines for message below
           */
                OSCMessage message = new OSCMessage(myIP, Arrays.asList(thingsToSend));
                // OSCMessage message = new OSCMessage(myIP, thingsToSend);


          /* NOTE: Since this version of JavaOSC uses Collections, we can actually use ArrayLists,
           * or any other class that implements the Collection interface. The following code is
           * valid for this version.
           *
           * The benefit of using an ArrayList is that you don't have to know how much information
           * you are sending ahead of time. You can add things to the end of an ArrayList, but not
           * to an Array.
           *
           * If you want to use this code with the downloadable version, you should switch the
           * commented and uncommented lines for message2
           */
                ArrayList<Object> moreThingsToSend = new ArrayList<Object>();
                moreThingsToSend.add("Hello World2");
                moreThingsToSend.add(123456);
                moreThingsToSend.add(12.345);

                OSCMessage message2 = new OSCMessage(myIP, moreThingsToSend);
                //OSCMessage message2 = new OSCMessage(myIP, moreThingsToSend.toArray());

                try {
                    // Send the messages
                    oscPortOut.send(message);
                    oscPortOut.send(message2);
                    Log.v(TAG, "SENDING");

                    // Pause for half a second
                    sleep(500);
                } catch (Exception e) {
                    // Error handling for some error
                }
            }
        }
    }
  };

I ended up getting some network errors, where research showed that I may need to add the following permission lines to the Android.manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

However, after adding those lines and running the app on the Simulator (a Nexus 7), but I keep getting errors saying that "The app has stopped".

This is actually my first Android project, so I'm sure I may be missing something obvious here (such as where to find logs in the case of this crash).

EDIT: I'm on API 27. The only log I see from LogCat is the following:

12-13 17:07:54.299 2981-3026/com.deviantdev.pdsampleproject D/EGL_emulation: eglMakeCurrent: 0xa9f842a0: ver 2 0 (tinfo 0xa9f83300)
12-13 17:07:55.344 2981-2981/com.deviantdev.pdsampleproject I/Choreographer: Skipped 103 frames!  The application may be doing too much work on its main thread.
12-13 17:07:55.362 2981-3026/com.deviantdev.pdsampleproject D/EGL_emulation: eglMakeCurrent: 0xa9f842a0: ver 2 0 (tinfo 0xa9f83300)

                                                                             [ 12-13 17:07:55.416  2981: 2981 D/         ]
                                                                             PlayerBase::stop() from IPlayer
12-13 17:07:55.416 2981-2981/com.deviantdev.pdsampleproject D/AudioTrack: stop() called with 90720 frames delivered
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Input buffer size estimate: 0
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Output buffer size estimate: 0
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Lowest margin: 11968
narner
  • 2,908
  • 3
  • 26
  • 63
  • Go to LogCat and examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 13 '17 at 18:22
  • what is your API version ? – zey Dec 13 '17 at 18:23
  • @zey, I'm on API 27 – narner Dec 13 '17 at 18:27
  • @CommonsWare I just updated the question - ty! – narner Dec 13 '17 at 18:51
  • None of those are a Java stack trace. None even appear to be from LogCat. Those appear to be from macOS. – CommonsWare Dec 13 '17 at 19:15
  • @CommonsWare apologies; should be fixed now... – narner Dec 13 '17 at 21:38
  • 1
    That is a warning (`W/`), not an error. It appears to be coming from Google Play Services (`com.google.android.gms`). And, it is not a Java stack trace. The best answer for showing what a stack trace looks like, on the question that I linked to earlier, is [this one](https://stackoverflow.com/a/38364939/115145). – CommonsWare Dec 13 '17 at 21:46

1 Answers1

0

From Android Marshmallow(API 23) and above , you may need to implement Runtime Permission . Here is an example ,

   if (Build.VERSION.SDK_INT >= 23) {
            String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (!hasPermissions(mContext, PERMISSIONS)) {
                ActivityCompat.requestPermissions((MainActivity) mContext, PERMISSIONS, REQUEST );
            } else {
                //do something
            }
        } else {
            //do something
        }

You can write this code inside onCreate and this is example to get WRITE_EXTERNAL_STORAGE permission at run time .
I think you should modify this line

String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};  

to

String[] PERMISSIONS = {android.Manifest.permission.INTERNET,android.Manifest.permission.ACCESS_NETWORK_STATE,android.Manifest.permission.ACCESS_WIFI_STATE};  

Hope it's helpful .

zey
  • 5,939
  • 14
  • 56
  • 110