近期在Android2.3上调GPS,使用的是locosys LS2005B模组(gps芯片是atheros ar1520),供应商给的参考资料是2.2/2.1的,自己找到的大部分资料也是该版本的,调试过程参考了系统源码下的sdk/emulator/gps/gps_qemu.c,相对比2.2,需要添加如下内容:
const GpsInterface* gps__get_gps_interface(struct gps_device_t* dev)
{
return &athrGpsInterface;
}
static int open_gps(const struct hw_module_t* module, char const* name,
struct hw_device_t** device)
{
struct gps_device_t *dev = malloc(sizeof(struct gps_device_t));
memset(dev, 0, sizeof(*dev));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (struct hw_module_t*)module;
dev->get_gps_interface = gps__get_gps_interface;
*device = (struct hw_device_t*)dev;
return 0;
}
static struct hw_module_methods_t gps_module_methods = {
.open = open_gps,
};
const struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = GPS_HARDWARE_MODULE_ID,
.name = "Atheros AR1520 GPS Module",
.author = "Qualcomm Atheros USA, Inc.",
.methods = &gps_module_methods,
};
上面内容是我参考修改的,改完后,重大问题又来了,系统能自动调用到该库了(关于2.2与2.3中gps的改动,附言会提及),但每次启动到Android时就挂了,Android遮罩动画不停的走,偶尔能进入Android,也是没一会就重回老路,一直跑动画去了,无奈之下只得查代码了,最终发现在com_android_server_location_GpsLocationProvider.cpp里面的static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length)函数中JNIEnv* env = AndroidRuntime::getJNIEnv();这语句执行后env的值都为NULL,而下面的语句没有判断就调用了env->CallVoidMethod()函数,此时不挂才怪,后来在网上找了一下资料,发现原来问题出在了HAL层,于是针对HAL层的代码进行了如下修改:
在gps_state_init()函数中最后会开线程,使用了如下语句:
if ( pthread_create( &state->thread, NULL, gps_state_thread, state ) != 0 ) {
LOGE("could not create gps thread: %s", strerror(errno));
goto Fail;
}
而这样会造成上面的env获不到数据,故修改成如下语句:
//state->thread =,有看到会把下面语句返回值赋给该量,调试时没有尝试,故先放着。
state->callbacks.create_thread_cb("athr_gps",gps_state_thread, state);
由于此句会使用到callbacks一量,而查找到gps_state_init()是由athr_gps_init()函数调用的,在该函数中,s->callbacks = *callbacks;语句是在gps_state_init(s);之后,故将其修改到之前,至此,终于调通了,数据也能正常上报了。
附录:
关于Android2.2与2.3之间关于GPS模块源码位置的变更:
Android2.2:
hardware/libhardware_legacy/include/hardware_legacy/gps.h
frameworks/base/location/java/com/android/internal/location/GpsLocationProvider.java
frameworks/base/core/jni/android_location_GpsLocationProvider.cpp
Android2.3:
hardware/libhardware/include/hardware/gps.h
frameworks/base/services/java/com/android/server/location/GpsLocationProvider.java
frameworks/base/services/jni/com_android_server_location_GpsLocationProvider.cpp