Reputation: 43
I'm using LIBGDX and with some struggle I've been progressing. But I encountered something that I'm having some difficulties. How, from LibGDX, in an android may I use the NFC? and secondly usb?
other resort solutions:
2.1. If LibGDX doesn't allow it, is there a way to access the native call of Android SDK, than?
2.2. A third solution would be having a second app(hidden) coded in android sdk but is it possible to have owned intents/ app name call reason ? so the calls have only one app that would open with.
My thanks for this great community
Upvotes: 3
Views: 391
Reputation: 7057
Put some interface in main project like this.
public interface NfcCommunicator {
public void initialize();
public void sendData(Object data);
public void setReceiveCallback(NfcReceiveCallback callback);
public void checkForReceivedData();
}
And,
public class NfcReceiveCallback {
public void execute(Object receivedData) {
// Do whatever you want to do with the received data.
}
}
Now put implementation of above interface in android project.
public class AndroidNfcCommunicator implements NfcCommunicator {
private NfcReceiveCallback myCallback;
private Activity myActivity;
public AndroidNfcCommunicator(Activity activity) {
this.myActivity = activity;
}
public void initialize() {
// NFC initialization code if any.
}
public void sendData(Object data) {
// Send data (you have access to android sdk here)
}
public void setReceiveCallback(NfcReceiveCallback callback) {
this.myCallback = callback;
}
public void checkForReceivedData() {
if (/* Data has been received (Use Activity object here. Since data comes from intent.)*/) {
this.myCallback.execute(/* Pass data here. */);
}
}
}
Create object of above class in android project and pass it to the ApplicationListener's constructor. Repeatedly call checkForReceivedData method in render method.
Good luck.
Upvotes: 2
Reputation: 3669
Since you don't have access to the Android SDK in your common project, you can define an interface in your common project and implement that in the Android project. After that, you pass an object with that implementation to your common project when initialising it.
Another way around this would be using reflection, but it's more cumbersome and error-prone.
Disclaimer: This is just a way (the cleanest I could think of) that I used to propagate access to the Android SDK for libGDX, but I haven't used libGDX in over 18 months and some things might have changed.
Upvotes: 2