使用本机获取卫星数量 [英] Getting the number of satellites using react-native

查看:74
本文介绍了使用本机获取卫星数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 react-native 创建我的android应用程序.我已经安装了 react-native-fused-location 以获得更好的位置. 我想找到我的GNSS传感器可以找到的卫星数量,并知道已经修复了多少颗卫星. 我知道我的GNSS传感器可以流式传输 NMEA格式,但是我不知道该怎么办我得到带有react-native的NMEA文件.我的GNSS传感器已通过蓝牙连接到我的android手机. GNSS传感器是 Hi-Target Qbox系列GIS数据收集器. 谢谢你.

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.

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

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

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

我发现正在追踪08颗卫星.
所以,我只需要获取NMEA字符串即可.

I can find that 08 satellites being tracked.
so, I just need to get NMEA string.

推荐答案

此问题通过创建本机模块解决.

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

  }

 }

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

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

最后,我将以下代码添加到App.js文件中,以使用我的本地模块: App.js或android.index.js

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

这篇关于使用本机获取卫星数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆