1

I'm building a React Native module with Kotlin. I have an external Java SDK that can discover peripherals on multiple protocols / networks / servers.

There is a Discovery class like this :

class Discovery(params: Params) {

  fun start() {
    // ...
  }

  fun stop() {
    // ...
  }

}

I want to pass a startDiscovery() and stopDiscovery() functions to React Bridge.

The client can search for many devices on multiple protocols / servers / ... at the same time. So it would require the instantiation of many Discovery class at the same time and stop some of them if necessary. Some sort of a Pool of discoveries.

So I would like to pass a reference to the instantiated object to Javascript so it can give back to me each time it want to call another method. But React Bridges doesn't allow to pass Java objets to JavaScript. Is there any good pattern to do so ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lucien Perouze
  • 590
  • 1
  • 4
  • 13

1 Answers1

1

Just try to make async actions in java so the thread not get stuck and you don't lose performance(return promises if you have async actions).

public class DummyModule extends ReactContextBaseJavaModule {
MyDummyClass dummy // this context

public DummyModule(final ReactApplicationContext reactContext){
super(reactContext);
}

@Override
    // getName is required to define the name of the module represented in
    // JavaScript
    public String getName() {
        return "DummyModule";
    }
@ReactMethod
    public void startMyClass() {
       this.dummy = new MyDummyClass();
    }

@ReactMethod
    public void fooActionClass() {
       if(this.dummy != null){
           this.dummy.fooAction();
       }
    }
}

In your javascript code

import { NativeModules } from 'react-native';
const dummyModule = NativeModules.DummyModule;

dummyModule.startMyClass();
// Make sure that u call the action when the class is instanciated.
dummyModule.fooActionClass();
anthony willis muñoz
  • 2,346
  • 3
  • 16
  • 33
  • Thank you for your time. In my case the client could need to call startMyClass() multiple time to create multiple objects. And then call fooActionClass() on any of them. So I need somehow to store a map of object maybe with a String identifier associated to retrieve them later with the identifier. Is there any good way to so ? – Lucien Perouze Sep 08 '20 at 09:10
  • Why you need to instanciate it multiple times ? it not make sense – anthony willis muñoz Sep 08 '20 at 09:50
  • In my case I use an external Java SDK that has a class that you instantiate and start to create a network listener on multiple protocols (Bluetooth, wifi, nfc, ...). You can instantiate "n" listeners at the same time to listen on multiple IPs, equipments, etc... I would like my JavaScript package to follow this logic. I need a function that can instantiate and keep a Java Classes for further interactions. In your example each call overwrite the object instantiation. My question is how do you store a "pool" of Java objects or is there any better pattern to use for React Native. – Lucien Perouze Sep 08 '20 at 10:06
  • So I guess instead of instanciate alot of times you could use listeners like your external library https://reactnative.dev/docs/native-modules-ios#sending-events-to-javascript. just handle an array of strings that ur user subscribe then when you want to subscribe call another method that return that strings with the events subscribed. In the RN screen just loop that array and subscribe to all the events. – anthony willis muñoz Sep 08 '20 at 10:13