我刚刚创建了一个新应用程序,create-react-native-app
然后将其分离以制作本机模块。
然后,我创建了包含我的包的SensorManagerPackage.java
旁边。MainApplication.java
然后创建一个满足ReactContextBaseJavaModule
实现的模块。
但是当我启动我的 react-native 应用程序并尝试记录 的内容时NativeModules
,结果是一个空对象。你知道为什么它不加载吗?
MainApplication.java
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new SensorManagerPackage()
);
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
SensorManagerPackage.java
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.farmingapp.sensors.AmbientTemperatureSensor;
import com.farmingapp.sensors.LightSensor;
import com.farmingapp.sensors.PressureSensor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SensorManagerPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new AmbientTemperatureSensor(reactContext));
modules.add(new LightSensor(reactContext));
modules.add(new PressureSensor(reactContext));
return modules;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
抽象传感器
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public abstract class AbstractSensor extends ReactContextBaseJavaModule implements SensorEventListener {
private final ReactApplicationContext reactContext;
private final SensorManager sensorManager;
private final Sensor sensor;
private final int sensorId;
private double lastReading = (double) System.currentTimeMillis();
private int interval;
public AbstractSensor(ReactApplicationContext reactContext, int sensorId) {
super(reactContext);
this.sensorId = sensorId;
this.reactContext = reactContext;
this.sensorManager = (SensorManager)reactContext.getSystemService(reactContext.SENSOR_SERVICE);
this.sensor = this.sensorManager.getDefaultSensor(sensorId);
}
// RN Methods
@ReactMethod
public void setUpdateInterval(int newInterval) {
this.interval = newInterval;
}
@ReactMethod
public void startUpdates(Promise promise) {
if (this.sensor == null) {
// No sensor found, throw error
promise.reject(new RuntimeException("No sensor found"));
return;
}
promise.resolve(null);
// Milisecond to Microsecond conversion
sensorManager.registerListener(this, sensor, this.interval * 1000);
}
@ReactMethod
public void stopUpdates() {
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
protected void sendEvent(String eventName, @Nullable WritableMap params) {
try {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} catch (RuntimeException e) {
Log.e("ERROR", "java.lang.RuntimeException: Trying to invoke Javascript before CatalystInstance has been set!");
}
}
protected abstract WritableMap convertEvent(SensorEvent sensorEvent, WritableMap map);
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
double tempMs = (double) System.currentTimeMillis();
if (tempMs - lastReading >= interval){
lastReading = tempMs;
WritableMap map = Arguments.createMap();
if (sensorEvent.sensor.getType() == this.sensorId) {
map.putDouble("timestamp", (double) System.currentTimeMillis());
sendEvent(this.getName(), this.convertEvent(sensorEvent, map));
}
}
}
}
光传感器.java
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
public class LightSensor extends AbstractSensor {
public LightSensor(ReactApplicationContext reactContext) {
super(reactContext, Sensor.TYPE_AMBIENT_TEMPERATURE);
}
@Override
public String getName() {
return "Light";
}
protected WritableMap convertEvent(SensorEvent sensorEvent, WritableMap map) {
map.putDouble("value", sensorEvent.values[0]);
return map;
}
}