Android GPS note (1)

Source: Internet
Author: User

1. GPS framework

 


2. Introduction to GPS code

2.1 header files

Let's start with the source file (hardware/libhardware/include/hardware/gps. h) to see what structure is provided in the original android code.

Struct: GpsLocation-Provides longitude and latitude, speed, azimuth, height, precision, and UTC timestamp information;

GpsStatus -- provides the GPS Positioning status. There are five types:

# Define GPS_STATUS_NONE 0 // Unknown Status
# Define GPS_STATUS_SESSION_BEGIN 1 // navigation started
# Define GPS_STATUS_SESSION_END 2 // stop navigation
# Define GPS_STATUS_ENGINE_ON 3 // powered but no navigation
# Define GPS_STATUS_ENGINE_OFF 4 // No power-on status

GpsSvInfo-Visible satellite information, pseudo-random number of satellites, signal strength, altitude, and azimuth (however, it is said that it is a satellite number, signal strength, satellite look-up angle, azimuth, to be verified !)

GpsSvStatus -- Visible satellite status. Number of visible satellites, a group of Visible satellite information (GpsSvInfo), ephemeris, almanac, and satellites involved in positioning.

GpsCallbacks -- callback function. There are eight callback functions: location_callback, status_callback, sv_status_callback, nmea_callback, set_capabilities, acquire_wakelock, release_wakelock, and create_thread.

GpsInterface -- interface definition, which includes eight interfaces: init, start, stop, cleanup, inject_time, inject_location, delete_aiding_data, and set_position_mode.

(Callback and interface can be understood as follows: callback is implemented elsewhere. I call it to return my information (back); initerface is implemented by me, provides an interface (face) for others to call)

Gps_device_t -- inherits from hw_device_tcommon and provides the hardware adaptation interface. It provides an important get_gps_interface interface to the upper layer.

2.2 registration of modules and callback Functions

GPS uses the HAL Stub framework, this framework is summarized as: Three struct, two constants, a function, specific reference http://blog.csdn.net/mr_raptor/article/details/8074549, (thanks to the teacher to explain) here will not be introduced.

During registration, call open_gps (); to get the device, and dev-> get_gps_interface
= Gps _ get_gps_interface to obtain the GpsInterface. The upper layer can use eight interfaces, such as init. The upper layer imports gpsCallbacks when using the Init interface. In this way, the connection is established up and down. Next, gps_state_init initializes the GPS serial port, establishes gps_state_thread, and starts the for (;) loop to control NMEA Data Reading, parsing, and reporting.

3. JNI intermediate layer

The source code of the GPSJNI adaptation layer is located at: frameworks/base/services/jni/com_android_server_location_GpsLocationProvider.cpp

 


First, let's look at the function definition for registering the JNI method:

[Plain] int register_android_server_location_GpsLocationProvider (JNIEnv * env)
{
Return jniRegisterNativeMethods (env, "com/android/server/location/GpsLocationProvider", sMethods, NELEM (sMethods ));
}

 


This function is called by the onload. cpp file in the same directory, where:

[Cpp] extern "C" jint JNI_OnLoad (JavaVM * vm, void * reserved)
{
JNIEnv * env = NULL;
Jint result =-1;
 
If (vm-> GetEnv (void **) & env, JNI_VERSION_1_4 )! = JNI_ OK ){
LOGE ("GetEnv failed! ");
Return result;
}
LOG_ASSERT (env, "cocould not retrieve the env! ");
 
//... Omit other registration code
Register_android_server_location_GpsLocationProvider (env );
 
Return JNI_VERSION_1_4;
}


As you can see from this, JNI registration is performed during JNI initialization, so that upper-layer applications can call c/c ++ local methods through JNI.

Return to the register_android_server_location_GpsLocationProvider function. The variable sMethods is defined as follows:

[Cpp] static JNINativeMethod sMethods [] = {
/* Name, signature, funcPtr */
{"Class_init_native", "() V", (void *) android_location_GpsLocationProvider_class_init_native },
{"Native_is_supported", "() Z", (void *) android_location_GpsLocationProvider_is_supported },
{"Native_init", "() Z", (void *) android_location_GpsLocationProvider_init },
{"Native_cleanup", "() V", (void *) android_location_GpsLocationProvider_cleanup },
{"Native_set_position_mode", "(IIIII) Z", (void *) android_location_GpsLocationProvider_set_position_mode },
{"Native_start", "() Z", (void *) android_location_GpsLocationProvider_start },
{"Native_stop", "() Z", (void *) android_location_GpsLocationProvider_stop },
{"Native_delete_aiding_data", "(I) V", (void *) android_location_GpsLocationProvider_delete_aiding_data },
{"Native_read_sv_status", "([I [F [F [F [I) I", (void *) android_location_GpsLocationProvider_read_sv_status },
{"Native_read_nmea", "([BI) I", (void *) android_location_GpsLocationProvider_read_nmea },
{"Native_inject_time", "(JJI) V", (void *) android_location_GpsLocationProvider_inject_time },
{"Native_inject_location", "(DDF) V", (void *) android_location_GpsLocationProvider_inject_location },
{"Native_supports_xtra", "() Z", (void *) android_location_GpsLocationProvider_supports_xtra },
{"Native_inject_xtra_data", "([BI) V", (void *) android_location_GpsLocationProvider_inject_xtra_data },
{"Native_agps_data_conn_open", "(Ljava/lang/String;) V", (void *) android_location_GpsLocationProvider_agps_data_conn_open },
{"Native_agps_data_conn_closed", "() V", (void *) android_location_GpsLocationProvider_agps_data_conn_closed },
{"Native_agps_data_conn_failed", "() V", (void *) android_location_GpsLocationProvider_agps_data_conn_failed },
{"Native_agps_set_id", "(ILjava/lang/String;) V", (void *) android_location_GpsLocationProvider_agps_set_id },
{"Native_agps_set_ref_location_cellid", "(IIIII) V", (void *) android_location_GpsLocationProvider_agps_set_reference_location_cellid },
{"Native_set_agps_server", "(ILjava/lang/String; I) V", (void *) android_location_GpsLocationProvider_set_agps_server },
{"Native_send_ni_response", "(II) V", (void *) android_location_GpsLocationProvider_send_ni_response },
{"Native_agps_ni_message", "([BI) V", (void *) android_location_GpsLocationProvider_agps_send_ni_message },
{"Native_get_internal_state", "() Ljava/lang/String;", (void *) android_location_GpsLocationProvider_get_internal_state },
{"Native_update_network_state", "(ZIZLjava/lang/String;) V", (void *) android_location_gpslocationprovider_update__state },
};


JNI is the intermediate messenger between the framework and HAL. These functions deserve our careful research. For details about how to obtain the function signature in JNI, refer to http://hi.baidu.com/scuyangl/item/414a039590ef82b5cd80e5dd. Calling between HAL and JNI depends on callback and interface, and between framework and JNI depends on the function Regist (regist is the function ing between JNI and framework, framework can call the corresponding JNI function through the corresponding function) and GetMethodID (the function obtained by getMethodID is implemented in the framework, JNI can be used ).
The above functions are implemented in the. cpp file, for example:


Static jboolean android_location_GpsLocationProvider_is_supported (JNIEnv * env, jclass clazz ){

Return (sGpsInterface! = NULL | get_gps_interface ()! = NULL );
}

4. Java Framework


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.