onBatchScanResults is not called in Android BLE - java

I am now using the new BLE api in android developing.
Basic idea is using bluetooth scanning result to inflate the recyclerview(list);
I followed the BLE guide on google developer
Now I have two problem:
1. onBatchScanResults listener is never triggered, butonScanResult works well, is that because the scanner only sense 1 sensor nearby?
my BLE scanner is much slower compared with other applications.
The following is the two core functions' code snippet.
private void scanBLE(boolean enable) {
final BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if (enable) {
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
} else {
if (mScanning) {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
}
Log.i(TAG, "now the scanning state is" + mScanning);
}
// Device scan callback.
private ScanCallback mScanCallback =
new ScanCallback() {
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
addBeaconTolist(result, beaconsList);
mAdapter.notifyDataSetChanged();
};
public void onScanFailed(int errorCode) {
Log.i(TAG, "error code is:" + errorCode);
};
public void onBatchScanResults(java.util.List<android.bluetooth.le.ScanResult> results) {
Log.i(TAG, "event linstener is called!!!!");
Log.i(TAG, "batch result are:" + results);
beaconsList.clear();
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
addBeaconTolist(result, beaconsList);
}
mAdapter.notifyDataSetChanged();
};
};
in MainFragment is like following:
beaconsList = new ArrayList<BeaconsInfo>();
mAdapter = new BeaconsAdapter(beaconsList);
mRecyclerView.setAdapter(mAdapter);
scannBLE(true);

Whether or not you get batch results or individual results depends on your scan settings.
In order to get batch results, you need to adjust the ScanSettings. Check out the documentation for the ScanSettings.Builder, and try using SCAN_MODE_LOW_POWER, which batches up results. You can also try adjusting the batch interval with setReportDelay(long reportDelayMillis); You can see a blog post I wrote about the power benefits of these settings here.
It's not totally clear what you mean by "my BLE scanner is much slower compared with other applications", but it may be that the app's UI lags because you are not updating it on the UI thread. Try wrapping your calls to notifyDatasetChanged like this:
runOnUiThread(new Runnable() {
#Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});

Try setUseHardwareBatchingIfSupported(true). This solves the problem for me on moto360 2nd gen. I think this is auto implemented for newer API.

Related

How to update the Flutter app state after navigating back from Android Java activity?

From the Flutter side, using the PlatformChannel, I am navigating to an Android Java activity, and doing some processes.
The activity successfully opens and I'm able to do the functionality and have the final result of it.
How may I navigate back to the Flutter side to a specific page and pass a value?
P.S.: without going back to the same page and then redirecting to the
next page.
On the Flutter side:
I have these variables
/// Filters Method Channel
final filtersChannel = const MethodChannel('flutter.native/filters');
/// Filters Method Channel
final filtersResultChannel = const MethodChannel("flutter.native/result_filters");
I have a floatingActionButton with this function which invokes a MethodChannel
Future<void> startNewActivity() async {
try {
await filtersChannel.invokeMethod('open_filters');
} on PlatformException catch (e) {
debugPrint("Failed to Invoke: '${e.message}'.");
}
}
On the MainActivity.java
On the protected void onCreate(#Nullable Bundle savedInstanceState) function, I'm starting an activity which has the AR video recording like this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, FiltersActivity.class);
startActivity(intent);
}
On the FiltersActivity.java
On the public void configureFlutterEngine(#NonNull FlutterEngine flutterEngine) function
Iā€™m defining and invoking my two channels:
The flutter.native/result_filters channel which builds the UI and
the functionality.
The flutter.native/filters channel which returns the final result.
Here:
#Override
public void configureFlutterEngine(#NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
String resultFiltersChannelIdentifier = "flutter.native/result_filters";
filtersResultChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), resultFiltersChannelIdentifier);
String filtersChannelIdentifier = "flutter.native/filters";
MethodChannel filtersChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), filtersChannelIdentifier);
filtersChannel.setMethodCallHandler(this::filtersMethodCallHandler);
}
Then, the flutter.native/filters displays the UI using the filtersMethodCallHandler function. Here:
private void filtersMethodCallHandler(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("open_filters")) {
openUI();
} else {
result.notImplemented();
}
}
In the openUI function, I'm assigning the record button a function, here:
recordButton.setOnClickListener(this::toggleRecording);
And here's the toggleRecording function:
public void toggleRecording(View unusedView) {
boolean recording = videoRecorder.onToggleRecord();
if (recording) {
recordButton.setImageResource(R.drawable.round_stop);
Toast.makeText(this, "Started Recording", Toast.LENGTH_SHORT).show();
} else {
recordButton.setImageResource(R.drawable.round_videocam);
Toast.makeText(this, "Recording Stopped", Toast.LENGTH_SHORT).show();
videoPath = videoRecorder.getVideoPath().getAbsolutePath();
Toast.makeText(this, "Video saved: " + videoPath, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Video saved: " + videoPath);
// Send notification of updated content.
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.TITLE, "Sceneform Video");
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, videoPath);
getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
filtersResultChannel.invokeMethod("filters_result", videoPath);
finish();
}
}
As shown above, I'm invoking the filters_result method for the filtersResultChannel channel and I'm adding the videoPath to it.
And then, I'm calling the finish(); method to close the FiltersActivity and return back to the MainAvtivity which successfully returns me to the Flutter page!
BACK to the Flutter side,
I'm listening to the filtersResultChannel like this:
#override
void initState() {
super.initState();
filtersResultChannel.setMethodCallHandler(_filtersResultHandler);
}
Future _filtersResultHandler(MethodCall methodCall) async {
if (methodCall.method == "filters_result") {
final videoPath = methodCall.arguments;
if (videoPath != null && videoPath.length >= 0) {
SchedulerBinding.instance.addPostFrameCallback((_) {
debugPrint("YES YES YES => $videoPath");
setState(() {
reportStatus = videoPath;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoShow(clipPath: videoPath),
),
);
});
});
}
return null;
} else {
return null;
}
}
As shown above, I have a debugPrint statement, this statement prints the returned videoPath from the filtersResultChannel
<--------->
THE PROBLEM
<--------->
Even though I'm successfully getting the videoPath value and successfully returning back to the Flutter page, I'm NOT able to use it!!
The setState(); doesn't update the UI NOR navigate to the next screen, the VideoShow screen!
HOW MAY I FIX SUCH AN ISSUE?

How do I run a BLE scan in the background only if my application is alive and in the background as well and not destroyed?

My application scans for BLE devices while it is in the background so that notifications can be sent to the user if the devices stop advertising or their RSSI drops below a set limit.
However, what I've noticed is that even though my app is closed and no longer in the background, my application will sometimes still scan for BLE Devices. How can I make it so that these background BLE scans occur in the background only when my app is alive and in the background?
I'm currently disabling scans in the onDestroy callback but I've read that its not recommended since the code in there isn't always executed. This is also why my application will still scan in the background sometimes.
Below is the onDestroy callback and the code I use to start or stop scans.
#Override
public void onDestroy() {
super.onDestroy();
scanLeDevice(false); //stop scanning and stop notification from showing when app closed
try {
unregisterReceiver(mReceiverOn);
unregisterReceiver(mReceiverOff);
//Register or UnRegister your broadcast receiver here
} catch(IllegalArgumentException e) {
e.printStackTrace();
}
stopService();
Log.d("lifecycle", "onDestroy: isAppRunning in on destroy");
}
private void scanLeDevice(final boolean enable) { //need to update to non deprecated code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && PackageManager.PERMISSION_GRANTED != getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION))
{
Toast.makeText(DeviceScanActivity.this, R.string.location_permission_needed, Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 9); //get fine location, needed for android 10
}
else if (enable && !scanRunning) { //starting a scan that is currently not running
mHandler.postDelayed(new Runnable() {
#Override
public void run() { //Stops scanning after a pre-defined scan period. This is required if you are to scan indefinitely
mBluetoothAdapter.stopLeScan(mLeScanCallback );
scanRunning = false;
if(mScanning)
mHandler2.postDelayed(new Runnable() {
#Override
public void run() {
scanLeDevice(true);
}
}, 650);
invalidateOptionsMenu();
}
}, 8000);
mScanning = true;
scanRunning = true;
boolean onScanner = mBluetoothAdapter.startLeScan(uuid, mLeScanCallback);
Log.d(TAG, "scanLeDevice: onScanner " + onScanner + " isDiscovering " + mBluetoothAdapter.isDiscovering());
} else if (enable && appNotDestroyed){ //scanRunning is true
mScanning = true;
mBluetoothAdapter.startLeScan(uuid, mLeScanCallback);
Log.d(TAG, "scanLeDevice: mScanning " + mScanning + " isDiscovering " + mBluetoothAdapter.isDiscovering());
} else { //enable is false
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
if(!clearHandlerHasRunnable) {
clearHandlerHasRunnable = true;
clearHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (!mScanning)
clearUnsavedDevices(mLeDevices);
clearHandlerHasRunnable = false;
}
}, 3000);
}
}
invalidateOptionsMenu();
if(!mScanning){ //clear indicators for all cells so that user isn't confused by certain indicators being on/off
for (int i = 0; i < mLeDevices.size(); i++){
updateViewScanFor(i);
}
}
}
I am no longer starting a background scan in my onPause() and then stopping it in onDestroy() in my main activity.
Instead, I am starting a foreground service in which my scanning takes place. This allows my app to stop the service, and therefore the scanning, when my app is destroyed since nothing is happening in the background.

How can i keep the job service running when the app is closed from recent task list by user

I am using Job Scheduler API in my app to schedule a job for me after specific time interval. It runs fine when the app is running. But whenever the user closes the app or clears it from the recent task list the app stops and the scheduled job never executes afterwards until you open the app and it is rescheduled again from the time it is opened.
Now i want someone to help me to keep the jobs on executing even if the app is closed or cleared from the recent task list.
If there is any alternative solution please tell me.
i am looking for the solution from the past 3 days. Tried everything said by developers on StackOverFlow and other sites and none of them worked for me.
This is where is schedule the job!
ComponentName componentName = new
ComponentName(getActivity().getBaseContext(),WallpaperJobService.class);
JobInfo jobInfo = new JobInfo.Builder(777,componentName)
.setRequiresCharging(sharedPreferences.getBoolean("Charging",false))
.setRequiredNetworkType(sharedPreferences.getBoolean("Wifi",false) ?
JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(sharedPreferences.getInt("Duration",15) * 60 *
1000)
.setPersisted(true)
.build();
JobScheduler scheduler = (JobScheduler)
getContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
scheduler.schedule(jobInfo);
My Job Service Class:
public class WallpaperJobService extends JobService {
private boolean jobCancelled;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
#Override
public boolean onStartJob(JobParameters params) {
Log.i("WallpaperJobService", "Job started!");
changeWallpaper(params);
return true;
}
private void changeWallpaper(final JobParameters params) {
final ArrayList<Image> images = (ArrayList<Image>)
MainActivity.favoritesRoomDatabase.roomDao().getAllFavoriteWallpapers();
sharedPreferences = getSharedPreferences("GridSize", MODE_PRIVATE);
editor = sharedPreferences.edit();
if (images != null && images.size() != 0) {
if (sharedPreferences.getInt("Index", 0) == images.size()) {
editor.putInt("Index", 0);
editor.commit();
}
Picasso.get().load(Constants.domain +
images.get(sharedPreferences.getInt("Index", 0)).getImage_url()).into(new
Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap,
Picasso.LoadedFrom from) {
new Thread(new Runnable() {
#Override
public void run() {
if (jobCancelled) {
Log.i("WallpaperJobService","Returned");
return;
}
try {
//Doing some work here
} catch (IOException e) {
e.printStackTrace();
}
Log.i("WallpaperJobService", "Job finished!");
jobFinished(params, false);
}
}).start();
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable)
{
Log.i("WallpaperJobService", "Bitmap load failed " +
e.getMessage());
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
} else {
Log.i("WallpaperJobService", "Favorite database is null!");
}
}
#Override
public boolean onStopJob(JobParameters params) {
Log.i("WallpaperJobService", "Job cancelled before completion!");
jobCancelled = true;
return true;
}
}
When doing stuff periodically in the background ā€” JobScheduler, WorkManager, AlarmManager, FCM push messages, etc. ā€” you have to take into account that your process might not be around when it is time for you to do your work. Android will fork a process for you, but it is "starting from scratch". Anything that your UI might have set up in memory, such as a database, would have been for some prior process and might not be set up in the new process.

SpeechRecognizer gives no match immediately after the start

I'm trying to make a use of SpeechRecognizer library in my android application, and so far its work leaves me with questions. First of all, it doesn't stop when I stop speaking.
If I try to stop speech recognizing myself, the next time it gives me 'No match!' right away.
My question is: when I use google speech recognition (f. e. when I search on web), it works like a charm. In my app it is far from perfect, though the library is the same. What is wrong with my implementation?
My code (simplified):
Note: I try to use partial results to make speech recognition more flexible, but I can't see any effect except recognition became little faster.
public void setupVoiceRecognition(Activity activity) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
mSpeechRecognizer.setRecognitionListener(this);
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
activity.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,
true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
mContext = activity.getApplicationContext();
if (mMainBtn != null) {
mMainBtn.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
VoiceRecognition.this.onClick();
}
});
}
}
public void forceStop() {
if (mListening) {
toggleListening(false);
}
}
public void onClick() {
toggleListening(!mListening);
}
private void toggleListening(boolean start) {
mPartialLength = 0;
if (start) {
mSpeechRecognizer.startListening(mRecognizerIntent);
} else {
mSpeechRecognizer.stopListening();
}
if (mMainBtn != null) {
mMainBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
if (mSupportBtn != null) {
mSupportBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
mListening = start;
}
...
#Override public void onError(int i) {
if (mListening) {
String errorText;
switch (i) {
case SpeechRecognizer.ERROR_AUDIO:
errorText = MyApp.getContext().getString(R.string.speech_recognition_err3);
break;
...
}
MyApp.showToast(errorText);
toggleListening(false);
if (i == NO_MATCH) {
toggleListening(true);
}
}
}
#Override public void onResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
String fullText = mViewForText.getText().toString();
mViewForText.setText(fullText.substring(0, fullText.length() - mPartialLength) + matches.get(0) + " ");
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
mPartialLength = 0;
forceStop();
}
}
#Override public void onPartialResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(EXTRA_PARTIAL_RESULTS);
if (matches != null) {
mViewForText.setText(mViewForText.getText().toString() + matches.get(0) + " ");
mPartialLength += matches.get(0).length() + 1;
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
}
}
}
Google disabled continuous speech recognition via SpeechRecognizer for third party apps. I assume that is because they have paid API now (https://cloud.google.com/speech/) which works very well but not free.
About NO_MATCH error. Google hears own beep invitation signal and assumes it is the speech start. Such as that beep can't be recognized it returns NO_MATCH error.
There is an option. You may downgrade Google app to have more stable work of the recognition service. The last properly working version of the Google app is 6.2.34

Libgdx and Google In-App-Purchase result

I followed these instructions to integrate both Libgdx and native android code using ActionResolver interface. I have no problem calling the Android method from the Libgdx part of my code. But I am hitting a dead end when I am trying to intergrate Google IAP with Libgdx. According to TrivialDrive example, it uses mPurchaseFinishedListener (outside of calling method).
My question is: how do I pass this IAP resultcode back to Libgdx since the listener is outside the calling method? Currently, purchase process went through, but the libgdx part of my code is not being "informed" of the purchase status/result.
This is my code:
Any help is much appreciated.
ActionResolver:
public interface IActionResolver {
public int requestIabPurchase(int product);
}
MainActivity:
public class MainActivity extends AndroidApplication implements IActionResolver {
// Debug tag, for logging
static final String TAG = "greatgame";
// Does the user have the premium upgrade?
boolean mIsUpgraded = false;
// SKUs for our products: the cat, all, or pow
static final String SKU_UPGRADE = "android.test.purchased";
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10001;
// The helper object
IabHelper mHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new Catland(this), cfg);
}
void iAbStartup() {
String base64EncodedPublicKey = "some key";
// Create the helper, passing it our context and the public key to verify signatures with
Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up in-app billing: " + result);
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
}
// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// Is it a failure?
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
// Do we have the SKU_UPGRADE upgrade?
Purchase thisUpgrade = inventory.getPurchase(SKU_UPGRADE);
mIsUpgraded = (thisUpgrade != null && verifyDeveloperPayload(thisUpgrade));
Log.d(TAG, "User is " + (mIsUpgraded ? "Upgraded" : "Free"));
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
runPurchaseFlow(submitProduct);
}
};
// Run real purchase flow
public void runPurchaseFlow(int product) {
Log.d(TAG, "runPurchaseFlow");
/* TODO: for security, generate your payload here for verification. See the comments on
* verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
* an empty string, but on a production app you should carefully generate this. */
String payload = "";
if (product == 1)
mHelper.launchPurchaseFlow(this, SKU_UPGRADE, RC_REQUEST, mPurchaseFinishedListener, payload);
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null) return;
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
if (!verifyDeveloperPayload(purchase)) {
Log.d(TAG, "Error purchasing. Authenticity verification failed.");
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_CAT)) {
// bought the upgrade!
Log.d(TAG, "Purchase Upgrade. Congratulating user.");
mIsUpgraded = true;
// how do i pass this result to the libgdx?
}
}
};
/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
return true;
}
#Override
public int requestIabPurchase(int product) {
iAbStartup();
return 0; // how do i get the result from mPurchaseFinishedListener?
}
}
PurchaseScreen
result = greatgame.actionResolver.requestIabPurchase(1);
You won't be able to return the result from requestIabPurchase() - the only methods of doing so would block for a long time. The best way, in my opinion, would be to create a listener interface of your own that your LibGdx project implements, and pass that into your request interface. For example:
In your libGdx project somewhere:
interface PurchaseCallback {
public int setPurchaseResult(int result);
}
ActionResolver:
public interface IActionResolver {
public int requestIabPurchase(int product, PurchaseCallback callback);
}
In PurchaseScreen, implement PurchaseCallback:
#override
public int setPurchaseResult(int result) {
// Yay! I have a result from a purchase! Maybe you want a boolean instead of an int? I don't know. Maybe an int (for the product code) and a boolean.
}
...and pass whatever is implementing PurchaseCallback (I'm assuming your PurchaseScreen does itself):
result = greatgame.actionResolver.requestIabPurchase(1, this);
Finally, hook it all up in MainActivity:
PurchaseCallback mCallback = null;
mPurchaseFinishedListener = ... etc. etc.
.
.
.
if (mCallback != null) {
mCallback.setPurchaseResult(0);
}
.
.
.
#Override
public int requestIabPurchase(int product, PurchaseCallback callback) {
mCallback = callback; // save this for later
iAbStartup();
return 0;
}
Note that you should call PurchaseCallback.setPurchaseResult() everywhere that mPurchaseFinishedListener has return, not only at the line // how do i pass this result to the libgdx? - otherwise, you will never know if a purchase failed or is just taking a really long time.

Categories

Resources