This section explains how to implement NaoSDK in a React project, with Android. Once the Android project is generated, follow the NAO documentation (add dependencies, fill in the manifest, add the MyNaoService class …).
To implement a native module (see official documentation for Android and iOS for more details) in the project, here is an Android example applied to NaoServiceLocation:
Create a NaoModule.java class that implements NAOLocationListener, NAOSensorsListener and inherits from ReactContextBaseJavaModule:
1 | public class NaoModule extends ReactContextBaseJavaModule implements NAOLocationListener, NAOSensorsListener { |
2 | private static ReactApplicationContext reactContext; |
3 | protected NAOSyncListener naoSyncListener; |
4 | private NAOLocationHandle locationHandle; |
6 | NaoModule(ReactApplicationContext context) { |
8 | reactContext = context; |
Add the name that corresponds to the library that we get called from React (JavaScript): NaoExample
2 | public String getName() { |
Add the necessary functions that will be raised to React (JavaScript):
These functions must be preceded by @ReactMethod; For example, the function “syncNao” will allow synchronization with the database.
3 | public void syncNao(String key) { |
4 | if (locationHandle == null ) { |
7 | locationHandle.synchronizeData(naoSyncListener); |
With React: We call from NativesModules: NaoExample.syncNao
3 | import { NativeModules } from 'react-native' ; |
7 | NativeModules.NaoExample.syncNao(key) |
To implement Callback functions or promises, we proceed in the same way, except that we pass our Callback functions to the parameters of the method raised to React, and to call the Callback function, we use the func_callback.invoke(“Message“) method.
3 | public void initNAOResources(String key, Callback onSyncSuccess, Callback onSyncFailure) { |
5 | if (key != null & !key.equals( "" )) { |
6 | locationHandle = new NAOLocationHandle(getReactApplicationContext(), MyNaoService. class , key, this , this ); |
7 | naoSyncListener = new NAOSyncListener() { |
9 | public void onSynchronizationSuccess() { |
10 | Log.d(TAG, "onSynchronizationSuccess" ); |
11 | onSyncSuccess.invoke( "onSynchronizationSuccess" ); |
15 | public void onSynchronizationFailure(NAOERRORCODE errorCode, String message) { |
16 | Log.e(TAG, "onSynchronizationFailure: " + message + "// errorcode : " + errorCode); |
17 | onSyncFailure.invoke( "onSynchronizationFailure: " + message); |
4 | NaoExample.initNAOResources( this .props.navigation.state.params.apiKey, |
7 | ToastAndroid.show(mes, ToastAndroid.SHORT); |
10 | console.log( "sync err" + err) |
11 | ToastAndroid.show(err, ToastAndroid.SHORT); |
14 | NaoExample.syncNao( this .props.navigation.state.params.apiKey) |
Register the module: You must register the module so that it is valid from JavaScript. Create NaoManager.java:
3 | import com.facebook.react.ReactPackage; |
4 | import com.facebook.react.bridge.NativeModule; |
5 | import com.facebook.react.bridge.ReactApplicationContext; |
6 | import com.facebook.react.uimanager.ViewManager; |
8 | import java.util.ArrayList; |
9 | import java.util.Collections; |
12 | public class NaoManager implements ReactPackage { |
15 | public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { |
16 | return Collections.emptyList(); |
20 | public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { |
21 | List<NativeModule> modules = new ArrayList<>(); |
23 | modules.add( new NaoModule(reactContext)); |
The package must be provided in the “getPackages” method of the MainApplication.java file. This file exists under the android folder in your react-native application directory. The path to this file is: android / app / src / main / java / com / your-application-name / MainApplication.java.
3 | protected List<ReactPackage> getPackages() { |
4 | List<ReactPackage> packages = new PackageList( this ).getPackages(); |
5 | packages.add( new ModuleRegistryAdapter(mModuleRegistryProvider)); |
6 | packages.add( new NaoManager()); |