(android)How to Switch to Run in "SDLThread"

In Bluetooth Low Energy(BLE) subsystem, if scaned one pheripheral, system
will call app-provied fucntion onLeScan. This function run in system defined
thread, for example “Binder_2”. Based on security and other reasons, Android
recommends to switch to UI thread. Below is example.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@Override

public void onLeScan(final BluetoothDevice device, int rssi, byte[]
scanRecord) {

mActivity.runOnUiThread(new Runnable() {

    @Override

    public void run() {

        mLeDeviceListAdapter.addDevice(device);

        mLeDeviceListAdapter.notifyDataSetChanged();

    }

});

}

In SDL framework, the better way is to switch to the thread named
"SDLThread". What am I to do?

I believe both mean the same, as the UI thread is the “main” thread of your
app, spawned by the system when your app starts and the only one allowed to
do rendering.

I would suggest that you modify SDLActivity.java with the functionality you
need, resp. a derived class of it (I think the README suggests to simply
derive from SDLActivity.java and do your modifications there). In the end
that would be a good idea anyway, because your app needs to have a dedicated
package identifier (you can’t have tons of different “org.libsdl.app” :wink:

It may be a lot easier for you to do the BT LE stuff completely in Java, and
only export the relevant results to native C/C++. You cannot reuse any of
the Android specifics on other platforms, so there’s no downside to not
doing it in C/C++.

Regards,

Daniel

---------- P?vodn? zpr?va ----------
Od: ancientcc
Komu: SDL
Datum: 20. 4. 2016 1:55:35
P?edm?t: [SDL] (android)How to Switch to Run in “SDLThread”

"

In Bluetooth Low Energy(BLE) subsystem, if scaned one pheripheral, system
will call app-provied fucntion onLeScan. This function run in system defined
thread, for example “Binder_2”. Based on security and other reasons, Android
recommends to switch to UI thread. Below is example. ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@Override

public void onLeScan(final BluetoothDevice device, int rssi, byte[]
scanRecord) {

??? mActivity.runOnUiThread(new Runnable() {

??? @Override

??? public void run() {

??? mLeDeviceListAdapter.addDevice(device);

??? mLeDeviceListAdapter.notifyDataSetChanged();

??? }

??? });

}

?

In SDL framework, the better way is to switch to the thread named
"SDLThread". What am I to do?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org"

You said: I believe both mean the same, as the UI thread is the “main” thread of your app, spawned by the system when your app starts and the only one allowed to do rendering.

<--------

Why I think “SDLThread” and “main” are different threads? See below code from SDActivity.java.---------------------------------------

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

          ......

          if (SDLActivity.mSDLThread == null) {

                       final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");

                        enableSensor(Sensor.TYPE_ACCELEROMETER, true);

                       sdlThread.start();

                       ......

          }

}

class SDLMain implements Runnable {

@Override

public void run() {

    // Runs SDL_main()

    SDLActivity.nativeInit(SDLActivity.mSingleton.getArguments());

}

}


nativeInit is function Java_org_libsdl_app_SDLActivity_nativeInit in JNI. It will call main, the entry point to the C/C++ app.

Yes, you are correct. Looks like I’m not always guessing right :frowning:

The only thing that I’m wondering though is how native code can do
rendering, if it’s not being executed from the UI thread. Anyway, it
obviously works :slight_smile:

---------- P?vodn? zpr?va ----------
Od: ancientcc
Komu: ‘SDL Development List’
Datum: 20. 4. 2016 16:19:54
P?edm?t: Re: [SDL] (android)How to Switch to Run in “SDLThread”

"

You said: I believe both mean the same, as the UI thread is the "main"
thread of your app, spawned by the system when your app starts and the only
one allowed to do rendering.

?

<--------

Why I think “SDLThread” and “main” are different threads? See below code
from SDActivity.java.---------------------------------------

public void surfaceChanged(SurfaceHolder holder, int format, int width, int
height) {

??? …

??? if (SDLActivity.mSDLThread == null) {

??? final Thread sdlThread = new Thread(new SDLMain()
, “SDLThread”);

??? ??? enableSensor(Sensor.TYPE_ACCELEROMETER, true);

??? sdlThread.start();

??? …

??? }

}

?

class SDLMain implements Runnable {

??? @Override

??? public void run() {

??? // Runs SDL_main()

??? SDLActivity.nativeInit(SDLActivity.mSingleton.getArguments());

??? }

}


nativeInit is function Java_org_libsdl_app_SDLActivity_nativeInit in JNI. It
will call main, the entry point to the C/C++ app.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org"

I begin to write Android code a few days ago, and also don’t know how to render.

In Android thread mechanism, same as swithing to UI thread, is there API or method can switch to a specific thread?

Not as far as I know. The “runOnUiThread” functionality is the only thing I
know about, being a simple convenience function to be capable to do UI
stuff, without adding an extra Handler and passing it all data as Message.

Typically you do not “switch threads”, but write your program in a way that
everything you want is already done in the correct thread. To pass data from
one thread to another, you can use the Handler class on Android, and pass it
the data it needs in the Message object to “handleMessage()”. This is
typically done to decouple the thread that delivers input events from the
thread that does UI work (the former is not allowed to). You might find taht
mechanism useful for your purpose as well.

Regards,

Daniel

---------- P?vodn? zpr?va ----------
Od: ancientcc
Komu: ‘SDL Development List’
Datum: 22. 4. 2016 1:53:23
P?edm?t: Re: [SDL] (android)How to Switch to Run in “SDLThread”

"

I begin to write Android code a few days ago, and also don’t know how to
render.

?

In Android thread mechanism, same as swithing to UI thread, is there API or
method can switch to a specific thread?

?_______________________________________________
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org"

System decides which thread to run that code, I can not run it in “SDLMain” directly. Since no API or method, I use cache to achieve it.

  1. Other thread write event code and context data to cache.

  2. SDL_PumpEvents read cache and execute special event. Of course, SDL_PumpEvents must be called in “SDLThread” thread.

Detail code is in this file:

https://github.com/freeors/SDL/blob/master/SDL2-2.0.4/src/peripheral/android/SDL_sysble.c