Android 8 or higher: Check for Google Play Services - java

this method keep returning 0. According to the developer docs this method should return something like SUCCES if the device got the newest version of google play. Does anybody know how to use this?
#Override
public void onResume() {
super.onResume();
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
System.out.println("henkie: " + GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()));
}

It is returning SUCCESS. The documentation clearly states that the method had an int return type, and returns a
status code indicating whether there was an error. Can be one of
following in ConnectionResult: SUCCESS, SERVICE_MISSING,
SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
To check against what was returned, use something like:
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS) {
//Success! Do what you want
}

Please read the documentation: 0 is SUCCESS
public static final int SUCCESS
The connection was successful.
Constant Value: 0 (0x00000000)
Documentation

Simply
int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (statusCode == ConnectionResult.SUCCESS) {
//OK
}

int state = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (state == ConnectionResult.SUCCESS) {
Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
//goAhead();
} else {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(state, this, -1);
dialog.show();
}

Related

Android Google Play Billing - querySkuDetailsAsync resurning empty list

I have an Android project where I want to use com.android.billingclient.api version 4.0.0, which would replace an old billing library that google doesn't allow any more (com.anjlab.android.iab.v3). I've implemented the methods for a one-time purchase, but when querying the SKU Details with billingClient.querySkuDetailsAsync using the SKU string for the product, I get an empty result set. I've been assured that the SKU is correct, so I don't know where the error might be.
Also, the old implementation required to provide a license key, which isn't the case with the new library. Do I need to define it somewhere else in the app?
Here's the code where it fails:
List<String> skuList = new ArrayList<>();
skuList.add(SKU_ID);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);
final Activity v = this;
billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
#Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
Has anyone a suggestion what to do?
This is how I query the SKU details within my app.
You can try to use this example and see if this works for you.
billingClient.startConnection(new BillingClientStateListener() {
#Override
public void onBillingSetupFinished(#NonNull BillingResult billingResult) {
Log.d(TAG, "Connection finished");
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
List<String> skuList = new ArrayList<> ();
skuList.add(ITEM_SKU_ADREMOVAL);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
(billingResult1, skuDetailsList) -> {
// Process the result.
if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (Object skuDetailsObject : skuDetailsList) {
skuDetails = (SkuDetails) skuDetailsObject;
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
if (ITEM_SKU_ADREMOVAL.equals(sku)) {
removeadsPrice = price;
}
else {
Log.d(TAG,"Sku is null");
}
}
Log.d(TAG, "i got response");
Log.d(TAG, String.valueOf(billingResult1.getResponseCode()));
Log.d(TAG, billingResult1.getDebugMessage());
}
else if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.ERROR) {
Toast.makeText(MainActivity.this, "Error in completing the purchase!", Toast.LENGTH_SHORT).show();
}
});
}
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_TIMEOUT) {
Toast.makeText(MainActivity.this, "Service timeout!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Failed to connect to the billing client!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBillingServiceDisconnected() {
restartConnection();
}
});
Getting this one working properly depends on several different factors
Have you published your app to Play Console or at least to an
internal track or something?
Do you have active products or subscriptions on your Google Play
Console?
Have you configured your licensed testers?
Please see the documentation for more info.

unable to use utility method to check if multiple edittext are empty with custom correct edittext id in output toast

Thanks for reading this
I am having difficulty a utility method meant to read all 20 edittexts and check if they are empty and return custom toast messages for instances of each editText being empty
The utility method is called inside my onclicklistener as seen below. Its called checkEmpty
btnRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkEmpty(etPickupLocation, getActivity())) return;
}
});
see checkempty below
public static boolean checkEmpty(EditText myEditText, Context context) {
String editTextIdInt= String.valueOf(myEditText.getId());
Log.d(TAG, "checkEmpty: editTextIdInt>> "+ myEditText.getId());
String editTextIdStr = context.getString(Integer.parseInt(editTextIdInt));
Log.d(TAG, "checkEmpty: editTextIdStr >>"+ editTextIdStr);
if (myEditText.getText().toString().trim().length() == 0) {
Toast.makeText(context, editTextIdStr+" is missing.", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
The issue is : I want to return the id of the editTextResource (etPickupLocation) as the second log output but instead i keep getting false
log output
2020-09-20 10:33:21.556 11445-11445/com.packagename.clasname D/UTILITYLOG: checkEmpty: editTextIdInt>> 2131230927
2020-09-20 10:33:21.556 11445-11445/com.packagename.clasname D/UTILITYLOG: checkEmpty: editTextIdStr >>false
All I want is to get the id of the editext bein passed into the method so i can return the right toast method
or is there a better approach to doing this?
Help me out guys
Thanks so much to Mike Ms clarification
I decided to use this approach instead since I cannot use an EditText's ID to retrieve a string resource.
public static boolean checkEmpty(EditText myEditText, Context context) {
Log.d(TAG, "checkEmpty: Obj" + myEditText.toString());
String myObj = myEditText.toString();
String OutputText="";
if (myObj.contains("etPickupLocation")) {
OutputText = "Sender Address";
}else if (myObj.contains("etWhereTo")) {
OutputText = "Recipient Address";
} else {
Log.d(TAG, "checkEmpty: >> DOING NOTHING");
OutputText = "Something";
}
if (myEditText.getText().toString().trim().length() == 0) {
Toast.makeText(context, "Enter "+OutputText+"." , Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

(java, bluetooth-lowenergy, ble) how to get data from android device via BLE

i'm beginner of BLE(Bluetooth-LowEnergy). I wanted to get data from android device via BLE.
I've already read about characteristic in Google Document.
and i've already searched on google too.
my device didn't respond to my request byte code.
i think it's because i set wrong characteristics.
cus i think i didn't understand about characteristics perfectly.
does anybody help me to set right characteristics please ?
Here's custom Uuid(it's better to see added Images on the top.)
CUSTOM SERVICE
0783b03e-8535-b5a0-7140-a304d2495cb7
NOTIFY Uuid : 0783B03E-8535-B5A0-7140-A304D2495CB8
Read Uuid : 00002a19-0000-1000-8000-00805f9b34fb
Write Uuid : 0783b03e-8535-b5a0-7140-a304d2495cba
and Here's Uuid I set
public final UUID serviceUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb7");
public final UUID notifyUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb8");
public final UUID readUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
public final UUID writeUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cba");
and Here's my code
BluetoothHandler.java
targetGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);
BluetoothGattCharacteristic readGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);
if (readGattCharacteristic != null) {
mBleService.setCharacteristicNotification(readGattCharacteristic, true);
} else {
callInterface();
return;
}
(BluetoothService.java)
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString(Define.GetInstance().readUuid.toString()), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
characteristic.addDescriptor(gD);
if (Define.GetInstance().notifyUuid.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(Define.GetInstance().readUuid.toString()));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
i solved it by referring to this site.
https://medium.com/#avigezerit/bluetooth-low-energy-on-android-22bc7310387a

Unable to Read from BLE Device, Write is working (Android)

I am trying to read values from BLE device. Steps I followed:
I am able to discover the BLE device and connect to it.
I am able to find the required characteristic by parsing through the
services and get the GattCharacteristic.
I am able to write a value to the BLE device characteristic and that's
verified.
The properties for that characteristic that I am trying to read the
value from are: READ, WRITE and INDICATE/NOTIFY.
The functions I am using for read and write are as follows:
a) Read function:
public void readCharacteristic(BluetoothGattCharacteristic characteristic)
{
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean status;
Log.w(TAG, "SCBABLe: Writing BLuetooth");
status=mBluetoothGatt.readCharacteristic(characteristic);
if(status) {
Log.w(TAG, "Read Successfully");
}
else
{
Log.w(TAG, "Read Unsuccessfully");
}
}
b) Write function
public void writeCharacteristic(BluetoothGattCharacteristic characteristic)
{
if (mBluetoothAdapter == null || mBluetoothGatt == null)
{
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean status;
Log.w(TAG, "SCBABLe: Writing BLuetooth");
status=mBluetoothGatt.writeCharacteristic(characteristic);
if(status) {
Log.w(TAG, "Write Successfully");
}
else
{
Log.w(TAG, "Write Unuccessfully");
}
}
I am calling the above two from a different activity, after I get the required characteristic(by matching the UUIDs).
//Current Activity Name: DeviceControlActivity
//BluetoothLeService is the other activity where Read and write are defined
private static final DeviceControlActivity holder1 = new DeviceControlActivity();
public BluetoothGattCharacteristic reqChar;
public BluetoothLeService reqService;
private BluetoothLeService mBluetoothLeService;
private void displayGattServices(List<BluetoothGattService> gattServices)
{
if (gattServices == null) return;
String uuid = null;
for (BluetoothGattService gattService : gattServices)
{
uuid = gattService.getUuid().toString();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics)
{
uuid = gattCharacteristic.getUuid().toString();
//SampleGattAttributes.UNKNOWN_CHARACTERISTIC is the hardcoded uuid against which i am checking
if((uuid.equals((SampleGattAttributes.UNKNOWN_CHARACTERISTIC))))
{
holder1.reqChar = gattCharacteristic;
holder1.reqService = mBluetoothLeService;
//Call for write
byte [] byte1= {0x01, 0x10};
holder1.reqChar.setValue(byte1);
holder1.reqService.writeCharacteristic(holder1.reqChar);
//Call for read
holder1.reqService.readCharacteristic(holder1.reqChar);
Result: Read function is returning false and write function is returning true so the value is getting written successfully for the required characteristic.(verified it)
Please, could anyone help and tell why the read is not getting executed? Why is it still returning false when it has Read as property and proper value defined?
The problem is in these few lines.
holder1.reqService.writeCharacteristic(holder1.reqChar);
//Call for read
holder1.reqService.readCharacteristic(holder1.reqChar);
After calling writeCharacteristic, if you do any extra read/write/etc. , the call will not be executed and return false. You have to wait for BluetoothGattCallback.onCharacteristicWrite before doing further operation.
In the Android BLE implementation, the gatt operation calls need to be queued so that only one operation (read, write, etc.) is in effect at a time. So for example, after gatt.readCharacteristic(characteristicX) is called, you need to wait for the gatt callbackBluetoothGattCallback.onCharacteristicRead() to indicate the read is finished. If you initiate a second gatt.readCharacteristic() operation before the previous one completes, the second one will fail (by returning false) This goes for all of the gatt.XXX() operations.
Its a little work, but I think the best solution is to create a command queue for all the gatt operations and run them one at a time. You can use the command pattern to accomplish this.

Unable to change language in Android default TextToSpeech

I was trying to use:
_tts = new TextToSpeech(this, this);
_tts.setLanguage(new Locale("en"));
The result is always -2 (LANG_NOT_SUPPORTED), so I thought that I made a mistake in the string given to the constructor.
Then I tried using (Obviously English is supported, no?)
_tts.setLanguage(Locale.US);
But to no avail and with the same result.
Why can't I change the language of the TTS engine?
My device's Android is version 2.3, TTS engine is PICO.
public void onInit(final int status) {
if (status == TextToSpeech.SUCCESS) {
_init = true; Log.d(TAG, "TTS init completed succesfully.");
setQueueMode(TextToSpeech.QUEUE_FLUSH);
}
}
The problem is that you are trying to set the language before initializing the TTS engine, instead of setting the language after creating the object do it at onInit callback.
public void onInit(final int status) {
if (status == TextToSpeech.SUCCESS) {
_init = true;
Log.d(TAG, "TTS init completed succesfully.");
int result = _tts.setLanguage(Locale.US);
}
}

Categories

Resources