0

I am using UsbSerialForAndroid library for implementing a serial port connection to a device.

I need to send a serial port object across activities. The definition for the UsbSerialPort is here.

At the moment, my code is able to return a string specifying port address with the help of putExtra method of Intent. I wish to send the serial port to the other activity (this is because I am not able to recreate the serial port object from address).

In order to send an object along with an Intent the object should be parcelable. But the original library class file is in java.

How to implement parcelable on such a pre-existing java class? I prefer to use kotlin language to achieve this.

EDIT

One option that seems logical is to use the original SerialUsbPort class as a base class and extent the new class to parcelable (with @Parselize annotation) and then cast the old object into new (is that even right?).

2 Answers 2

2

I wish to send the serial port to the other activity (this is because I am not able to recreate the serial port object from address).

Unfortunately, to implement Parcelable you need to do exactly this: tell Android how to write some data to a Parcel and how to recreate this object from the data.

@Parcelize doesn't change this requirement, it just tells the compiler to generate these methods in a standard way.

To access a "live" object which you can't recreate, extend Binder instead (see https://developer.android.com/guide/components/bound-services#Binder for an example in Java). Now you can store a Binder in a Parcel when implementing Parcelable.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, Thanks for the insight. I am looking at the link you shared, it seems to be talking about binders in the context of services, I do not see Binder extension there.
See class LocalBinder. In your case you just need to store val port: UsbSerialPort instead of the getService method.
Alternately, create a service through which you'll access the port (that would be my preference).
Just a bit of more info on implementing Service. Did you mean I should return a UsbSerialPort from the service? Or that the service will take care of I/O so I only communicate text to/from service, no port passing business.
If all activities (or fragments, services, etc) which need to access the port are in one process and a port can safely be used from multiple threads (probably not?), I'd go with the first option, otherwise with the second one.
2

So, your problem, in summary, is that you need to send an instance of a library object; but to send it, it needs to implement an interface. Correct?

There are a few ways of doing this with Java/Kotlin interop, but none of them are particularly original. You're fortunate, actually, that the library you're using was not also written in Kotlin, because Kotlin classes are by default final. Java classes, on the other hand, are not; which is why you are able to do something like this:

MySerialPort.kt

class MySerialPort : UsbSerialPortImpl(), Parcelable {
  ...
}

This is, of course, assuming a few things:

  1. That you're using a UsbSerialPort implementation from the library. The class you linked, after all, is an Interface, rather than a concrete class. You must be getting a concrete class from somewhere to be able to initialize it, so I'm assuming it's a library class.

  2. That the UsbSerialPortImpl class is not final. As mentioned above, this (fortunately for you) isn't the default in Java, so you're probably good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.