使用 react-native 获取卫星数量

Getting the number of satellites using react-native

我正在使用 react-native to create my android application. I've installed react-native-fused-location 以获得更好的位置。 我想知道我的 GNSS 传感器能找到多少颗卫星,并知道其中有多少颗已被固定。 我知道我的 GNSS 传感器可以传输 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。 谢谢你。

更新:
我发现可以通过 NMEA 格式获取卫星数量。
这是 GGA 回复:

$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

我可以找到正在跟踪的 08 颗卫星。
所以,我只需要获取 NMEA 字符串。

通过创建本机模块解决了这个问题。

我在下面的目录中创建了两个文件来支持我的项目 "number of satellite"。

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);

  }

 }

之后,我在 MainApplication.java 文件中添加了这一行:

...
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new AnExampleReactPackage() // <-- Add this line with your package name.
      );
    }
...

最后,我在我的 App.js 文件中添加了这段代码以使用我的 本机模块 App.js 或 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,
  }
});

我已经编写了一个 React Native 库来使用 NmeaLocationListener 获取 NMEA 语句。 https://github.com/ivanstan/react-native-nmea-library

它仅适用于 Android,因为 IOS API 不公开 NMEA 信息。此外,这高度依赖于硬件,因此不能保证它在每个 phone.

上都能正常工作