I am using the Sumup SDK to create a bridge to React Native. Most of the hard work is done but I am trying to call a specific function to wake up the card reader before a transaction is processed.
The original code I had was this:
@ReactMethod
public void prepareCardTerminal() {
SumUpAPI.prepareForCheckout();
}
}
The RN bridge then calls this function like this:
static prepareCardTerminal() {
NativeRNSumup.prepareCardTerminal();
}
How ever this gives me a React Native error of:
Must be called on main thread
I read that this could mean it needs to be run on the UI thread so I rewrote the function to be:
@ReactMethod
public void prepareCardTerminal() {
new Thread(new Runnable() {
public void run() {
SumUpAPI.prepareForCheckout();
}
});
}
However this doesn't have the intended results (even though it doesn't show any errors).
Any tips would be much appreciated.
Edit: I found a solution to this issue. I used UiThreadUtil:
import com.facebook.react.bridge.UiThreadUtil;
...
@ReactMethod
public void prepareCardTerminal() {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
SumUpAPI.prepareForCheckout();
}
});
}
You can do something like this:
@ReactMethod
public void prepareCardTerminal() {
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
SumUpAPI.prepareForCheckout();
}
};
mainHandler.post(myRunnable);
}
Or even simpler:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
SumUpAPI.prepareForCheckout();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With