I am currently developing a BLE-enabled Android app targeting API 27 using Kotlin.
I am attempting to override a function within android.bluetooth.BluetoothGatt. There are a number of callbacks available to be overridden to enable the handling of certain BLE events.
For example, I override onConnectionStateChange() in the following way:
private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
/* do stuff */
}
This works just fine.
My issue stems from trying to override onConnectionUpdated(). This callback is defined in the same way as onConnectionStateChange() in the BLE API source, so how come I can't override it? This is how I am attempting to override it (still within the BluetoothGattCallback() object):
fun onConnectionUpdated(gatt: BluetoothGatt, interval: Int, latency: Int, timeout: Int, status: Int) {
/* do stuff */
}
EDIT: I forgot to mention that, when I add the override keyword it provides the error message: OnConnectionUpdated overrides nothing..
Forgive my naivety, I don't often work with Kotlin/Java, thanks.
You should not use this method, it is only for internal use and not part of the public API. Therefore it is hidden via #hide. For more information about #hide and how to access it regardless see What does #hide mean in the Android source code?
Note that using reflection to access it as described in the link above is discouraged
The method you want to use is on the dark-greylist with the following restrictions:
dark-greylist:
For apps whose target SDK is below API level 28: each use of a dark
greylist interface is permitted.
apps whose target SDK is API level 28 or higher: same behavior as blacklist
blacklist: restricted regardless of target SDK. The platform will behave as if the interface is absent. For example, it will throw NoSuchMethodError/NoSuchFieldException whenever the app is trying to use it, and will not include it when the app wants to know the list of fields/methods of a particular class.
Related
I'm attempting to create a Xamarin Binding Library for the Android UseButton library. I created my binding project, included the .aar and set Build Action = LibraryProjectZip. As was expected, a bunch of errors popped up and I've managed to get rid of most using the Metadata.xml file, except for a bunch that follow the same pattern:
Error CS0102 The type 'CrossBridgeCommunicator' already contains a definition for 'WebViewDismiss'
I checked the CrossBridgeCommunicator class, and found it has 2 copies of about 12 events. Here's a snippet of one of these duplicates
#region "Event implementation for Com.Usebutton.Sdk.Internal.Bridge.BridgeMessageParser.IListener"
...
public event EventHandler WebViewDismiss {
add {
global::Java.Interop.EventHelper.AddEventHandler<global::Com.Usebutton.Sdk.Internal.Bridge.BridgeMessageParser.IListener, global::Com.Usebutton.Sdk.Internal.Bridge.BridgeMessageParser.IListenerImplementor>(
ref weak_implementor___SetMainBridge,
__CreateIListenerImplementor,
__v => MainBridge = __v,
__h => __h.OnWebViewDismissHandler += value);
}
...
}
...
public event EventHandler WebViewDismiss {
add {
global::Java.Interop.EventHelper.AddEventHandler<global::Com.Usebutton.Sdk.Internal.Bridge.BridgeMessageParser.IListener, global::Com.Usebutton.Sdk.Internal.Bridge.BridgeMessageParser.IListenerImplementor>(
ref weak_implementor___SetWidgetBridge,
__CreateIListenerImplementor,
__v => WidgetBridge = __v,
__h => __h.OnWebViewDismissHandler += value);
}
...
}
The only difference between them is that they access different attributes in their bodies (the "weak_implementor__"). I checked the original class in a Java decompiler and it doesn't implement any of these events; in fact, it doesn't even implement the interface. What it does have are 2 fields of this interface type. I'm guessing Xamarin creates these methods for some reason, but I don't know why or how. They don't even appear on the api.xml (nor do the fields).
I tried to change the name of the events using the Metadata.xml file, but, since these events don't even exist in the Java class, I don't know how to find them. I even tried to remove the fields using "remove-node", but am getting the warning no nodes matched. Does anyone know how can I change these events' names? Again, they come from the same interface, but are created directly on the class that has 2 fields of the interface type.
Thanks in advance.
I'm creating a chat app and according to the tutorial I should create this:
Tutorial example
The problem is that nowadays this function (FirebaseInstanceIdService) no longer exists and therefore I cannot use it.
Would anyone advise me with what code to achieve the same result?
Thank you
FirebaseInstanceIdService has been depracated and replaced with FirebaseMessagingService
https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceIdService
onTokenRefresh is now onNewToken.
Within the class extending FirebaseMessagingService, in which you are already overriding onMessageReceived() method, override the onNewToken(token: String) method (this replaces the old onTokenRefresh(), so all the logic you had there, must be put here).
With Kotlin,
override fun onMessageReceived(remoteMessage: RemoteMessage) {
//the logic here
}
override fun onNewToken(token: String) {
//all the logic of the old FirebaseInstanceIdService.onTokenRefresh() here
//usually, to send to the app server the instance ID token
sendTokenToTheAppServer(token)
}
I see in the documentation of Plot Project that GeotriggerHandlerReceiver should be used, but according to Android Studio for the version com.plotprojects:plot-android:2.4.0-beta this Receiver is already deprecated.
Image from Android Studio
3.5 Geotrigger handler
When you want to handle your geotriggers, or use them as trigger events for your own code, you can use the geotrigger handler. Plot automatically detects whether a service is registered in AndroidManifest.xml that extends from the class GeotriggerHandlerReceiver. Implementations of GeotriggerHandlerReceiver must implement the method public List handleGeotriggers(List geotriggers). When the service is defined, Plot will send the geotriggers to this method before considering them as handled. This allows you to add custom code that is triggered by entering (or exiting) a geofence or beacon region.
//Example implementation for handleGeotriggers:
public class MyGeotriggerHandlerReceiver extends GeotriggerHandlerReceiver {
#Override
public List<Geotrigger> handleGeotriggers(List<Geotrigger> geotriggers) {
List<Geotrigger> passedGeotriggers = new ArrayList<Geotrigger>();
for (Geotrigger geotrigger : geotriggers) {
String data = geotrigger.getData();
if (data.equals("pass")) {
passedGeotriggers.add(geotrigger);
}
}
return passedGeotriggers;
}
}
Indeed we're deprecating the GeotriggerHandlerReceiver and will remove it in the next version. In order to maintain your code up to date follow this guide. The guide explains why and how to do this.
We're in the process of updating our documentation to make it more organised and complete.
Cheers!
I've been checking ContextWrapper.java in android sdk and noticed that there are some methods which are annotated in javadoc by #remove. I assume, it is an indicator to give lint warnings in IDE but that'd be great to know exactly what it is and why they needed such as thing in the first place instead of removing completely.
/** #removed */
#Override
public SharedPreferences getSharedPreferences(File file, int mode) {
return mBase.getSharedPreferences(file, mode);
}
Like #hide, it causes Doclava (a Javadoc doclet Android uses to generate SDK documentation and API signature text files) to omit the annotated member from generated documentation and, if the -api flag is passed, from the generated API signature text file. If the -removedApi flag is also used, members annotated #remove will be included in the removed.txt signature file.
I'm developing an Android app with Google Cloud Endpoints, I have read a lot of documentation and tutorials about it, but there is still one information missing: How can I choose which backend's methods will be visible for Android client?
Let's say I have a FriendshipEndpoint, FriendshipEndpoint has this method:
#ApiMethod(name = "listFriendship", path = "listFriendship")
public CollectionResponse<Friendship> listFriendship(
#Nullable #Named("cursor") String cursorString,
#Nullable #Named("limit") Integer limit)
EDIT FriendshipEndpoint is annotated with #Api annotation
This method should be visible only in the backend (not by Android client).
I have tried to remove #ApiMethod anotation -> doesn't work, the method is still visible in the api explorer in my browser.
Setting access modifier to private is not a solution for me, because I want to call this method from other Endpoint.
Removing access modifier is also no solution for me, because I need this method to be visible from other packages (test package).
Is there a solution for this problem at all?
Unfortunately, this scenario is not supported right now. The best solution is to move the method to a helper class and have it shared among all the endpoint classes.