The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
(android)How to Switch to Run in "SDLThread"
ancientli


Joined: 01 Jul 2015
Posts: 45
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?
(android)How to Switch to Run in "SDLThread"
hardcoredaniel
Guest

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"
Quote:

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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
(android)How to Switch to Run in "SDLThread"
ancientli


Joined: 01 Jul 2015
Posts: 45
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.
(android)How to Switch to Run in "SDLThread"
hardcoredaniel
Guest

Yes, you are correct. Looks like I'm not always guessing right Sad

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 :-)



---------- 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"
Quote:

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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
(android)How to Switch to Run in "SDLThread"
ancientli


Joined: 01 Jul 2015
Posts: 45
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?
(android)How to Switch to Run in "SDLThread"
hardcoredaniel
Guest

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"
Quote:

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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
(android)How to Switch to Run in "SDLThread"
ancientli


Joined: 01 Jul 2015
Posts: 45
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