问题
I'm using react-native to create my android application. I've installed react-native-fused-location to get better location. I want to find the number of satellites that my GNSS sensor can find and know how many of them has been fixed. I know that my GNSS sensor can stream NMEA format but I don't know how should i get the NMEA file with react-native. my GNSS sensor has been connected to my android phone by Bluetooth. GNSS sensor is Hi-Target Qbox series GIS data collector. Thank you.
Update:
I found that It will be possible to get number of satellite by NMEA format.
This is a GGA response:
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
I can find that 08 satellites being tracked.
so, I just need to get NMEA string.
回答1:
I have written a react native library to get NMEA sentences using NmeaLocationListener. https://github.com/ivanstan/react-native-nmea-library
It works only on Android since IOS API doesn't expose NMEA information. Also this is highly hardware dependent, so there is no guarantee it will work on every phone.
回答2:
This problem solved by creating a native module.
I created two files in bellow directory to support "number of satellite" for my project.
C:\Users\user\Desktop\project_name\android\app\src\main\java\com\project_name
AnExampleReactPackage.java
package com.facebook.react.modules.toast;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AnExampleReactPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new ToastModule1(reactContext));
return modules;
}
}
ToastModule1.java
package com.facebook.react.modules.toast;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.content.Context;
import com.facebook.react.bridge.ReadableMap;
import android.os.Bundle;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class ToastModule1 extends ReactContextBaseJavaModule {
ReactApplicationContext mReactContext;
private LocationManager locationManager;
public ToastModule1(ReactApplicationContext reactContext) {
super(reactContext);
ReactApplicationContext mReactContext = reactContext;
locationManager = (LocationManager) mReactContext.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public String getName() {
return "ToastExample";
}
@ReactMethod
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
@ReactMethod
public void getCoors(){
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
show("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()+", Satellites:" +location.getExtras().getInt("satellites"),1);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
After that i added this line in my MainApplication.java file:
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new AnExampleReactPackage() // <-- Add this line with your package name.
);
}
...
Finally, I added this code in my App.js file to use my native module: App.js or android.index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import {NativeModules} from 'react-native';
function findNumberOfSatellites(context){
NativeModules.ToastExample.show("It's Starting to find satellites!", 1);
var count = "searching...";
NativeModules.ToastExample.getCoors();
context.setState({satellites : count});
}
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {satellites: null}
}
componentDidMount(){
setTimeout(findNumberOfSatellites,500,this);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Satellite Counter!
</Text>
<Text style={styles.instructions}>
{this.state.satellites}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
来源:https://stackoverflow.com/questions/48839312/getting-the-number-of-satellites-using-react-native