Monday 26 July 2021

Android capture and send notifications through bluetooth

I have two android devices. I have developed two codebases. One is the sender codebase and the other the receiver. Both these are connected via Bluetooth Socket connection and I am able to send simple Strings between them.

On the sender device I have implemented a NotificationListenerService which captures notifications from that device. I am getting an object of type StatusBarNotification for each notification. As per the documentation, this object implements the Parcelable interface.

In order to transmit this object, I have tried using GSON, as well as using Parcel object to marshall and send the data. This was not working, so I stopped worrying about the sending part, and tried to see on the same device whether this would marshall and unmarshall correctly.

Here is the Kotlin code for both,

/*
 * Using Gson for serialization.
 * 
 * Parts of the object e.g. deserializedObject.notification are missing
 */
val serializedString = Gson().toJson(sbn)
val deserializedObject = Gson().fromJson<StatusBarNotification>(serializedString, StatusBarNotification::class.java)

/*
 * Using Parcel for serialization.
 *
 * Throws this error,
 * Caused by: java.lang.RuntimeException: Tried to marshall a Parcel that contained Binder objects.
 */
val parcel = Parcel.obtain()
parcel.writeValue(sbn)
val bytes = parcel.marshall()

val p2 = Parcel.obtain()
p2.unmarshall(bytes, 0, bytes.size)
val result = p2.readValue(StatusBarNotification::class.java.classLoader) as StatusBarNotification

Any direction would be appreciated.

Update

I have tried creating a custom data class object. Serialization and deserialization for that works perfectly fine over bluetooth, and I am getting the data on the 2nd device. However I have narrowed it down and my problem remains that the StatusBarNotification object does not play well with serialization.

val serializedCode = Gson().toJson(statusBarNotification) in debugger gives {"id":1815187780,"pkg":"org.telegram.messenger","uid":10260}

Any leads would be appreciated.



from Android capture and send notifications through bluetooth

No comments:

Post a Comment