Sending Objects

AIDL objects can be sent either as a concrete AIDL type or as the type-erased IBinder interface:

birthday_service/aidl/com/example/birthdayservice/IBirthdayInfoProvider.aidl:

  1. package com.example.birthdayservice;
  2. interface IBirthdayInfoProvider {
  3. String name();
  4. int years();
  5. }

birthday_service/aidl/com/example/birthdayservice/IBirthdayService.aidl:

  1. import com.example.birthdayservice.IBirthdayInfoProvider;
  2. interface IBirthdayService {
  3. /** The same thing, but using a binder object. */
  4. String wishWithProvider(IBirthdayInfoProvider provider);
  5. /** The same thing, but using `IBinder`. */
  6. String wishWithErasedProvider(IBinder provider);
  7. }

birthday_service/src/client.rs:

  1. /// Rust struct implementing the `IBirthdayInfoProvider` interface.
  2. struct InfoProvider {
  3. name: String,
  4. age: u8,
  5. }
  6. impl binder::Interface for InfoProvider {}
  7. impl IBirthdayInfoProvider for InfoProvider {
  8. fn name(&self) -> binder::Result<String> {
  9. Ok(self.name.clone())
  10. }
  11. fn years(&self) -> binder::Result<i32> {
  12. Ok(self.age as i32)
  13. }
  14. }
  15. fn main() {
  16. binder::ProcessState::start_thread_pool();
  17. let service = connect().expect("Failed to connect to BirthdayService");
  18. // Create a binder object for the `IBirthdayInfoProvider` interface.
  19. let provider = BnBirthdayInfoProvider::new_binder(
  20. InfoProvider { name: name.clone(), age: years as u8 },
  21. BinderFeatures::default(),
  22. );
  23. // Send the binder object to the service.
  24. service.wishWithProvider(&provider)?;
  25. // Perform the same operation but passing the provider as an `SpIBinder`.
  26. service.wishWithErasedProvider(&provider.as_binder())?;
  27. }
  • Note the usage of BnBirthdayInfoProvider. This serves the same purpose as BnBirthdayService that we saw previously.