Gears Android WIFI/基站定位源代码分析

本文详细介绍了如何利用Gears实现在不同平台上获取WIFI接入点和移动网络基站的位置信息,包括监听基站和WIFI变化、数据更新通知、位置请求处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章出处:http://www.limodev.cn/blog
作者联系方式:李先静 <xianjimli at hotmail dot com>

Broncho A1还不支持基站和WIFI定位,Android的老版本里是有NetworkLocationProvider的,它实现了基站和WIFI定位,但从 android 1.5之后就被移除了。本来想在broncho A1里自己实现NetworkLocationProvider的,但一直没有时间去研究。我知道 gears(http://code.google.com/p/gears/)是有提供类似的功能,昨天研究了一下Gears的代码,看能不能移植到 android中来。
1.下载源代码
[url]svn checkout http://gears.googlecode.com/svn/trunk/ gears-read-only[/url]

定位相关的源代码在gears/geolocation目录中。

2.关注android平台中的基站位置变化。

JAVA类AndroidRadioDataProvider是 PhoneStateListener的子类,用来监听Android电话的状态变化。当服务状态、信号强度和基站变化时,就会用下面代码获取小区信息:
Java代码 复制代码 收藏代码
  1. RadioDataradioData=newRadioData();
  2. GsmCellLocationgsmCellLocation=(GsmCellLocation)cellLocation;
  3. //Extractthecellid,LAC,andsignalstrength.
  4. radioData.cellId=gsmCellLocation.getCid();
  5. radioData.locationAreaCode=gsmCellLocation.getLac();
  6. radioData.signalStrength=signalStrength;
  7. //ExtractthehomeMCCandhomeMNC.
  8. Stringoperator=telephonyManager.getSimOperator();
  9. radioData.setMobileCodes(operator,true);
  10. if(serviceState!=null){
  11. //Extractthecarriername.
  12. radioData.carrierName=serviceState.getOperatorAlphaLong();
  13. //ExtracttheMCCandMNC.
  14. operator=serviceState.getOperatorNumeric();
  15. radioData.setMobileCodes(operator,false);
  16. }
  17. //Finallygettheradiotype.
  18. inttype=telephonyManager.getNetworkType();
  19. if(type==TelephonyManager.NETWORK_TYPE_UMTS){
  20. radioData.radioType=RADIO_TYPE_WCDMA;
  21. }elseif(type==TelephonyManager.NETWORK_TYPE_GPRS
  22. ||type==TelephonyManager.NETWORK_TYPE_EDGE){
  23. radioData.radioType=RADIO_TYPE_GSM;
  24. }
RadioData radioData = new RadioData();
      GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
 
      // Extract the cell id, LAC, and signal strength.
      radioData.cellId = gsmCellLocation.getCid();
      radioData.locationAreaCode = gsmCellLocation.getLac();
      radioData.signalStrength = signalStrength;
 
      // Extract the home MCC and home MNC.
      String operator = telephonyManager.getSimOperator();
      radioData.setMobileCodes(operator, true);
 
      if (serviceState != null) {
        // Extract the carrier name.
        radioData.carrierName = serviceState.getOperatorAlphaLong();
 
        // Extract the MCC and MNC.
        operator = serviceState.getOperatorNumeric();
        radioData.setMobileCodes(operator, false);
      }
 
      // Finally get the radio type.
      int type = telephonyManager.getNetworkType();
      if (type == TelephonyManager.NETWORK_TYPE_UMTS) {
        radioData.radioType = RADIO_TYPE_WCDMA;
      } else if (type == TelephonyManager.NETWORK_TYPE_GPRS
                 || type == TelephonyManager.NETWORK_TYPE_EDGE) {
        radioData.radioType = RADIO_TYPE_GSM;
      }


然后调用用C代码实现的onUpdateAvailable函数。

2.Native函数onUpdateAvailable是在 radio_data_provider_android.cc里实现的。

声明Native函数
Java代码 复制代码 收藏代码
  1. JNINativeMethodAndroidRadioDataProvider::native_methods_[]={
  2. {"onUpdateAvailable",
  3. "(L"GEARS_JAVA_PACKAGE"/AndroidRadioDataProvider$RadioData;J)V",
  4. reinterpret_cast<void*>(AndroidRadioDataProvider::OnUpdateAvailable)
  5. },
  6. };
JNINativeMethod AndroidRadioDataProvider::native_methods_[] = {
  {"onUpdateAvailable",
   "(L" GEARS_JAVA_PACKAGE "/AndroidRadioDataProvider$RadioData;J)V",
   reinterpret_cast<void*>(AndroidRadioDataProvider::OnUpdateAvailable)
  },
};


JNI调用好像只能调用静态成员函数,把对象本身用一个参数传进来,然后再调用对象的成员函数。
Java代码 复制代码 收藏代码
  1. voidAndroidRadioDataProvider::OnUpdateAvailable(JNIEnv*env,
  2. jclasscls,
  3. jobjectradio_data,
  4. jlongself){
  5. assert(radio_data);
  6. assert(self);
  7. AndroidRadioDataProvider*self_ptr=
  8. reinterpret_cast<AndroidRadioDataProvider*>(self);
  9. RadioDatanew_radio_data;
  10. if(InitFromJavaRadioData(env,radio_data,&new_radio_data)){
  11. self_ptr->NewRadioDataAvailable(&new_radio_data);
  12. }
  13. }
void AndroidRadioDataProvider::OnUpdateAvailable(JNIEnv* env,
                                                 jclass cls,
                                                 jobject radio_data,
                                                 jlong self) {
  assert(radio_data);
  assert(self);
  AndroidRadioDataProvider *self_ptr =
      reinterpret_cast<AndroidRadioDataProvider*>(self);
  RadioData new_radio_data;
  if (InitFromJavaRadioData(env, radio_data, &new_radio_data)) {
    self_ptr->NewRadioDataAvailable(&new_radio_data);
  }
}


先判断基站信息有没有变化,如果有变化则通知相关的监听者。
Java代码 复制代码 收藏代码
  1. voidAndroidRadioDataProvider::NewRadioDataAvailable(
  2. RadioData*new_radio_data){
  3. boolis_update_available=false;
  4. data_mutex_.Lock();
  5. if(new_radio_data&&!radio_data_.Matches(*new_radio_data)){
  6. radio_data_=*new_radio_data;
  7. is_update_available=true;
  8. }
  9. //Avoidholdingthemutexlockedwhilenotifyingobservers.
  10. data_mutex_.Unlock();
  11. if(is_update_available){
  12. NotifyListeners();
  13. }
  14. }
void AndroidRadioDataProvider::NewRadioDataAvailable(
    RadioData* new_radio_data) {
  bool is_update_available = false;
  data_mutex_.Lock();
  if (new_radio_data && !radio_data_.Matches(*new_radio_data)) {
    radio_data_ = *new_radio_data;
    is_update_available = true;
  }
  // Avoid holding the mutex locked while notifying observers.
  data_mutex_.Unlock();
 
  if (is_update_available) {
    NotifyListeners();
  }
}


接下来的过程,基站定位和WIFI定位是一样的,后面我们再来介绍。下面我们先看 WIFI定位。

3.关注android平台中的WIFI变化。

JAVA类AndroidWifiDataProvider扩展了 BroadcastReceiver类,它关注WIFI扫描结果:
Java代码 复制代码 收藏代码
  1. IntentFilterfilter=newIntentFilter();
  2. filter.addAction(mWifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  3. mContext.registerReceiver(this,filter,null,handler);
IntentFilter filter = new IntentFilter();
    filter.addAction(mWifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    mContext.registerReceiver(this, filter, null, handler);


当收到WIFI扫描结果后,调用Native函数 onUpdateAvailable,并把WIFI的扫描结果传递过去。
Java代码 复制代码 收藏代码
  1. publicvoidonReceive(Contextcontext,Intentintent){
  2. if(intent.getAction().equals(
  3. mWifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
  4. if(Config.LOGV){
  5. Log.v(TAG,"Wifiscanresulstavailable");
  6. }
  7. onUpdateAvailable(mWifiManager.getScanResults(),mNativeObject);
  8. }
  9. }
 public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(
            mWifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
      if (Config.LOGV) {
        Log.v(TAG, "Wifi scan resulst available");
      }
      onUpdateAvailable(mWifiManager.getScanResults(), mNativeObject);
    }
  }


4.Native函数onUpdateAvailable是在 wifi_data_provider_android.cc里实现的。
Java代码 复制代码 收藏代码
  1. JNINativeMethodAndroidWifiDataProvider::native_methods_[]={
  2. {"onUpdateAvailable",
  3. "(Ljava/util/List;J)V",
  4. reinterpret_cast<void*>(AndroidWifiDataProvider::OnUpdateAvailable)
  5. },
  6. };
  7. voidAndroidWifiDataProvider::OnUpdateAvailable(JNIEnv*/*env*/,
  8. jclass/*cls*/,
  9. jobjectwifi_data,
  10. jlongself){
  11. assert(self);
  12. AndroidWifiDataProvider*self_ptr=
  13. reinterpret_cast<AndroidWifiDataProvider*>(self);
  14. WifiDatanew_wifi_data;
  15. if(wifi_data){
  16. InitFromJava(wifi_data,&new_wifi_data);
  17. }
  18. //Wenotifyregardlessofwhethernew_wifi_dataisempty
  19. //ornot.Thearbitratorwilldecidewhattodowithanempty
  20. //WifiDataobject.
  21. self_ptr->NewWifiDataAvailable(&new_wifi_data);
  22. }
  23. voidAndroidWifiDataProvider::NewWifiDataAvailable(WifiData*new_wifi_data){
  24. assert(supported_);
  25. assert(new_wifi_data);
  26. boolis_update_available=false;
  27. data_mutex_.Lock();
  28. is_update_available=wifi_data_.DiffersSignificantly(*new_wifi_data);
  29. wifi_data_=*new_wifi_data;
  30. //Avoidholdingthemutexlockedwhilenotifyingobservers.
  31. data_mutex_.Unlock();
  32. if(is_update_available){
  33. is_first_scan_complete_=true;
  34. NotifyListeners();
  35. }
  36. #ifUSING_CCTESTS
  37. //ThisisneededforrunningtheWiFitestontheemulator.
  38. //Seewifi_data_provider_android.hfordetails.
  39. if(!first_callback_made_&&wifi_data_.access_point_data.empty()){
  40. first_callback_made_=true;
  41. NotifyListeners();
  42. }
  43. #endif
  44. }
JNINativeMethod AndroidWifiDataProvider::native_methods_[] = {
  {"onUpdateAvailable",
   "(Ljava/util/List;J)V",
   reinterpret_cast<void*>(AndroidWifiDataProvider::OnUpdateAvailable)
  },
};
 
void AndroidWifiDataProvider::OnUpdateAvailable(JNIEnv*  /* env */,
                                                jclass  /* cls */,
                                                jobject wifi_data,
                                                jlong self) {
  assert(self);
  AndroidWifiDataProvider *self_ptr =
      reinterpret_cast<AndroidWifiDataProvider*>(self);
  WifiData new_wifi_data;
  if (wifi_data) {
    InitFromJava(wifi_data, &new_wifi_data);
  }
  // We notify regardless of whether new_wifi_data is empty
  // or not. The arbitrator will decide what to do with an empty
  // WifiData object.
  self_ptr->NewWifiDataAvailable(&new_wifi_data);
}
 
void AndroidWifiDataProvider::NewWifiDataAvailable(WifiData* new_wifi_data) {
  assert(supported_);
  assert(new_wifi_data);
  bool is_update_available = false;
  data_mutex_.Lock();
  is_update_available = wifi_data_.DiffersSignificantly(*new_wifi_data);
  wifi_data_ = *new_wifi_data;
  // Avoid holding the mutex locked while notifying observers.
  data_mutex_.Unlock();
 
  if (is_update_available) {
    is_first_scan_complete_ = true;
    NotifyListeners();
  }
 
#if USING_CCTESTS
  // This is needed for running the WiFi test on the emulator.
  // See wifi_data_provider_android.h for details.
  if (!first_callback_made_ && wifi_data_.access_point_data.empty()) {
    first_callback_made_ = true;
    NotifyListeners();
  }
#endif
}


从以上代码可以看出,WIFI定位和基站定位的逻辑差不多,只是前者获取的WIFI的扫描结果,而后者获取的基站信息。后面代码的基本上就统一起来了,接下来我们继续看。

5.把变化(WIFI/基站)通知给相应的监听者。
Java代码 复制代码 收藏代码
  1. AndroidWifiDataProvider和AndroidRadioDataProvider都是继承了DeviceDataProviderImplBase,DeviceDataProviderImplBase的主要功能就是管理所有Listeners。
  2. staticDeviceDataProvider*Register(ListenerInterface*listener){
  3. MutexLockmutex(&instance_mutex_);
  4. if(!instance_){
  5. instance_=newDeviceDataProvider();
  6. }
  7. assert(instance_);
  8. instance_->Ref();
  9. instance_->AddListener(listener);
  10. returninstance_;
  11. }
  12. staticboolUnregister(ListenerInterface*listener){
  13. MutexLockmutex(&instance_mutex_);
  14. if(!instance_->RemoveListener(listener)){
  15. returnfalse;
  16. }
  17. if(instance_->Unref()){
  18. deleteinstance_;
  19. instance_=NULL;
  20. }
  21. returntrue;
  22. }
AndroidWifiDataProvider和AndroidRadioDataProvider都是继承了DeviceDataProviderImplBase,DeviceDataProviderImplBase的主要功能就是管理所有Listeners。
 
  static DeviceDataProvider *Register(ListenerInterface *listener) {
    MutexLock mutex(&instance_mutex_);
    if (!instance_) {
      instance_ = new DeviceDataProvider();
    }
    assert(instance_);
    instance_->Ref();
    instance_->AddListener(listener);
    return instance_;
  }
 
  static bool Unregister(ListenerInterface *listener) {
    MutexLock mutex(&instance_mutex_);
    if (!instance_->RemoveListener(listener)) {
      return false;
    }
    if (instance_->Unref()) {
      delete instance_;
      instance_ = NULL;
    }
    return true;
  }


6.谁在监听变化(WIFI/基站)

NetworkLocationProvider在监听变化(WIFI/基站):
Java代码 复制代码 收藏代码
  1. radio_data_provider_=RadioDataProvider::Register(this);
  2. wifi_data_provider_=WifiDataProvider::Register(this);
radio_data_provider_ = RadioDataProvider::Register(this);
  wifi_data_provider_ = WifiDataProvider::Register(this);


当有变化时,会调用函数DeviceDataUpdateAvailable:
Java代码 复制代码 收藏代码
  1. //DeviceDataProviderInterface::ListenerInterfaceimplementation.
  2. voidNetworkLocationProvider::DeviceDataUpdateAvailable(
  3. RadioDataProvider*provider){
  4. MutexLocklock(&data_mutex_);
  5. assert(provider==radio_data_provider_);
  6. is_radio_data_complete_=radio_data_provider_->GetData(&radio_data_);
  7. DeviceDataUpdateAvailableImpl();
  8. }
  9. voidNetworkLocationProvider::DeviceDataUpdateAvailable(
  10. WifiDataProvider*provider){
  11. assert(provider==wifi_data_provider_);
  12. MutexLocklock(&data_mutex_);
  13. is_wifi_data_complete_=wifi_data_provider_->GetData(&wifi_data_);
  14. DeviceDataUpdateAvailableImpl();
  15. }
// DeviceDataProviderInterface::ListenerInterface implementation.
void NetworkLocationProvider::DeviceDataUpdateAvailable(
    RadioDataProvider *provider) {
  MutexLock lock(&data_mutex_);
  assert(provider == radio_data_provider_);
  is_radio_data_complete_ = radio_data_provider_->GetData(&radio_data_);
 
  DeviceDataUpdateAvailableImpl();
}
 
void NetworkLocationProvider::DeviceDataUpdateAvailable(
    WifiDataProvider *provider) {
  assert(provider == wifi_data_provider_);
  MutexLock lock(&data_mutex_);
  is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_);
 
  DeviceDataUpdateAvailableImpl();
}


无论是WIFI还是基站变化,最后都会调用 DeviceDataUpdateAvailableImpl:
Java代码 复制代码 收藏代码
  1. voidNetworkLocationProvider::DeviceDataUpdateAvailableImpl(){
  2. timestamp_=GetCurrentTimeMillis();
  3. //Signaltotheworkerthreadthatnewdataisavailable.
  4. is_new_data_available_=true;
  5. thread_notification_event_.Signal();
  6. }
void NetworkLocationProvider::DeviceDataUpdateAvailableImpl() {
  timestamp_ = GetCurrentTimeMillis();
 
  // Signal to the worker thread that new data is available.
  is_new_data_available_ = true;
  thread_notification_event_.Signal();
}


这里面只是发了一个signal,通知另外一个线程去处理。

7.谁在等待thread_notification_event_

线程函数NetworkLocationProvider::Run在一个循环中等待 thread_notification_event,当有变化(WIFI/基站)时,就准备请求服务器查询位置。

先等待:
Java代码 复制代码 收藏代码
  1. if(remaining_time>0){
  2. thread_notification_event_.WaitWithTimeout(
  3. static_cast<int>(remaining_time));
  4. }else{
  5. thread_notification_event_.Wait();
  6. }
if (remaining_time > 0) {
      thread_notification_event_.WaitWithTimeout(
          static_cast<int>(remaining_time));
    } else {
      thread_notification_event_.Wait();
    }


准备请求:
Java代码 复制代码 收藏代码
  1. if(make_request){
  2. MakeRequest();
  3. remaining_time=1;
  4. }
    if (make_request) {
      MakeRequest();
      remaining_time = 1;
    }


再来看MakeRequest的实现:

先从cache中查找位置:
Java代码 复制代码 收藏代码
  1. constPosition*cached_position=
  2. position_cache_->FindPosition(radio_data_,wifi_data_);
  3. data_mutex_.Unlock();
  4. if(cached_position){
  5. assert(cached_position->IsGoodFix());
  6. //Recordthepositionandupdateitstimestamp.
  7. position_mutex_.Lock();
  8. position_=*cached_position;
  9. position_.timestamp=timestamp_;
  10. position_mutex_.Unlock();
  11. //Letlistenersknowthatwenowhaveapositionavailable.
  12. UpdateListeners();
  13. returntrue;
  14. }
 const Position *cached_position =
      position_cache_->FindPosition(radio_data_, wifi_data_);
  data_mutex_.Unlock();
  if (cached_position) {
    assert(cached_position->IsGoodFix());
    // Record the position and update its timestamp.
    position_mutex_.Lock();
    position_ = *cached_position;
    position_.timestamp = timestamp_;
    position_mutex_.Unlock();
 
    // Let listeners know that we now have a position available.
    UpdateListeners();
    return true;
  }


如果找不到,再做实际的请求
Java代码 复制代码 收藏代码
  1. returnrequest_->MakeRequest(access_token,
  2. radio_data_,
  3. wifi_data_,
  4. request_address_,
  5. address_language_,
  6. kBadLatLng,//Wedon'thaveapositiontopass
  7. kBadLatLng,//totheserver.
  8. timestamp_);
  return request_->MakeRequest(access_token,
                               radio_data_,
                               wifi_data_,
                               request_address_,
                               address_language_,
                               kBadLatLng,  // We don't have a position to pass
                               kBadLatLng,  // to the server.
                               timestamp_);


7.客户端协议包装

前面的request_是NetworkLocationRequest实例,先看 MakeRequest的实现:

先对参数进行打包:
Java代码 复制代码 收藏代码
  1. if(!FormRequestBody(host_name_,access_token,radio_data,wifi_data,
  2. request_address,address_language,latitude,longitude,
  3. is_reverse_geocode_,&post_body_)){
  4. returnfalse;
  5. }
  if (!FormRequestBody(host_name_, access_token, radio_data, wifi_data,
                       request_address, address_language, latitude, longitude,
                       is_reverse_geocode_, &post_body_)) {
    return false;
  }


通知负责收发的线程
Java代码 复制代码 收藏代码
  1. thread_event_.Signal();
  thread_event_.Signal();


8.负责收发的线程
Java代码 复制代码 收藏代码
  1. voidNetworkLocationRequest::Run(){
  2. while(true){
  3. thread_event_.Wait();
  4. if(is_shutting_down_){
  5. break;
  6. }
  7. MakeRequestImpl();
  8. }
  9. }
  10. voidNetworkLocationRequest::MakeRequestImpl(){
  11. WebCacheDB::PayloadInfopayload;
void NetworkLocationRequest::Run() {
  while (true) {
    thread_event_.Wait();
    if (is_shutting_down_) {
      break;
    }
    MakeRequestImpl();
  }
}
 
void NetworkLocationRequest::MakeRequestImpl() {
  WebCacheDB::PayloadInfo payload;


把打包好的数据通过HTTP请求,发送给服务器
Java代码 复制代码 收藏代码
  1. scoped_refptr<BlobInterface>payload_data;
  2. boolresult=HttpPost(url_.c_str(),
  3. false,//Notcapturing,sofollowredirects
  4. NULL,//reason_header_value
  5. HttpConstants::kMimeApplicationJson,//Content-Type
  6. NULL,//mod_since_date
  7. NULL,//required_cookie
  8. true,//disable_browser_cookies
  9. post_body_.get(),
  10. &payload,
  11. &payload_data,
  12. NULL,//was_redirected
  13. NULL,//full_redirect_url
  14. NULL);//error_message
  15. MutexLocklock(&is_processing_response_mutex_);
  16. //is_aborted_maybetrueevenifHttpPostsucceeded.
  17. if(is_aborted_){
  18. LOG(("NetworkLocationRequest::Run():HttpPostrequestwascancelled.\n"));
  19. return;
  20. }
  21. if(listener_){
  22. Positionposition;
  23. std::stringresponse_body;
  24. if(result){
  25. //IfHttpPostsucceeded,payload_dataisguaranteedtobenon-NULL.
  26. assert(payload_data.get());
  27. if(!payload_data->Length()||
  28. !BlobToString(payload_data.get(),&response_body)){
  29. LOG(("NetworkLocationRequest::Run():Failedtogetresponsebody.\n"));
  30. }
  31. }
 scoped_refptr<BlobInterface> payload_data;
  bool result = HttpPost(url_.c_str(),
                         false,            // Not capturing, so follow redirects
                         NULL,             // reason_header_value
                         HttpConstants::kMimeApplicationJson,  // Content-Type
                         NULL,             // mod_since_date
                         NULL,             // required_cookie
                         true,             // disable_browser_cookies
                         post_body_.get(),
                         &payload,
                         &payload_data,
                         NULL,             // was_redirected
                         NULL,             // full_redirect_url
                         NULL);            // error_message
 
  MutexLock lock(&is_processing_response_mutex_);
  // is_aborted_ may be true even if HttpPost succeeded.
  if (is_aborted_) {
    LOG(("NetworkLocationRequest::Run() : HttpPost request was cancelled.\n"));
    return;
  }
  if (listener_) {
    Position position;
    std::string response_body;
    if (result) {
      // If HttpPost succeeded, payload_data is guaranteed to be non-NULL.
      assert(payload_data.get());
      if (!payload_data->Length() ||
          !BlobToString(payload_data.get(), &response_body)) {
        LOG(("NetworkLocationRequest::Run() : Failed to get response body.\n"));
      }
    }


解析出位置信息
Java代码 复制代码 收藏代码
  1. std::string16access_token;
  2. GetLocationFromResponse(result,payload.status_code,response_body,
  3. timestamp_,url_,is_reverse_geocode_,
  4. &position,&access_token);
std::string16 access_token;
    GetLocationFromResponse(result, payload.status_code, response_body,
                            timestamp_, url_, is_reverse_geocode_,
                            &position, &access_token);


通知位置信息的监听者。
Java代码 复制代码 收藏代码
  1. boolserver_error=
  2. !result||(payload.status_code>=500&&payload.status_code<600);
  3. listener_->LocationResponseAvailable(position,server_error,access_token);
  4. }
  5. }
 bool server_error =
        !result || (payload.status_code >= 500 && payload.status_code < 600);
    listener_->LocationResponseAvailable(position, server_error, access_token);
  }
}


有人会问,请求是发哪个服务器的?当然是google了,缺省的URL是:
Java代码 复制代码 收藏代码
  1. staticconstchar16*kDefaultLocationProviderUrl=
  2. STRING16(L"https://www.google.com/loc/json");
static const char16 *kDefaultLocationProviderUrl =
    STRING16(L"https://www.google.com/loc/json");


回过头来,我们再总结一下:

1.WIFI和基站定位过程如下:


2.NetworkLocationProvider和 NetworkLocationRequest各有一个线程来异步处理请求。

3.这里的NetworkLocationProvider与android中的 NetworkLocationProvider并不是同一个东西,这里是给gears用的,要在android的google map中使用,还得包装成android中的NetworkLocationProvider的接口。

4.WIFI和基站定位与平台无关,只要你能拿到WIFI扫描结果或基站信息,而且能访问google的定位服务器,不管你是Android平台,Windows Mobile平台还是传统的feature phone,你都可以实现WIFI和基站定位。



附: WIFI和基站定位原理

无论是WIFI的接入点,还是移动网络的基站设备,它们的位置基本上都是固定的。设备端(如手机)可以找到它们的ID,现在的问题就是如何通过这些ID找到对应的位置。网上的流行的说法是开车把所有每个位置都跑一遍,把这些设备的位置与 GPS测试的位置关联起来。


参考资料:
Gears:
[url] http://gears.googlecode.com/[/url]
Google 地图 API:[url] http://code.google.com/intl/zh-CN/apis/maps/documentation/reference.html[/url]
wifi定位技术:[url] http://blog.youkuaiyun.com/NewMap/archive/2009/03/17/3999337.aspx
[/url]
zhua
随着人们对基于位置的服务(Location Based Service,LBS)需求日益增大,以及无线通信技术的快速发展,无线定位技术成为了一个研究热点。人们在室外广泛使用目前较成熟的GPS,A-GPS等定位系统进行定位,但是在复杂的室内环境中,这些技术的定位精度不高,不能满足室内定位的需求。WIFI网络具有通信快速、部署方便的特点,它在室内场所广受欢迎.Android系统从几年前发布以来在智能手机操作系统市场占有率不断升高,成为目前使用最为广泛的智能手机操作系统,同时Android移动终端自身具备WIFI无线连接功能。指纹定位算法以其独特的优势减小了对室内难以精确定义的信号传播模型的依赖性,成为定位技术中的一个研究热点。基于此,本课题重点研究并改进指纹定位算法,设计实现基于AndroidWIFI室内定位系统。 首先,通过阅读大量相关的文献资料,对比分析了当前国内外WIFI室内指纹定位技术的研究现状对其中涉及到的相关技术的原理和特点进行介绍分析,包括WIF1无线通信技术,室内无线定位技术以及位置指纹定位技术,并根据室内WIFI指纹定位技术的特征对定位过程中的影响因素进行分析。 其次,根据前面提到的定位过程中的关键影响因素,介绍了对应的解决方案。分析与研究了几种典型的指纹定位算法,包括最近邻法(NN).K近邻法(KNN)、K加权近邻法(WKNN),并提出算法的改进方案,使用MATLAB软件进行算法的仿真分析,寻求其中的最佳参数值以及定位性能差异。通过分析几种算法的性能仿真结果,拟定了基于最强AP法的改进算法作为定位系统采纳的算法。 然后,通过对基于AndroidWIFI室内定位系统的需求分析,提出了一种基于Android 的WIF1室内定位系统设计方案。接着介绍了定位系统软件开发环境,并设计了定位系统总体架构,以及定位系统的各个功能模块。在各项设计确定以后,采用JAVA语言编程实现定位系统的各项功能。 最后,搭建了WIFI室内定位实验环境,使用完成的室内定位系统结合硬件资源,在实验环境下,进行离线阶段创建数据库以及在线阶段的定位测试,并记录呈现在定位客户端上定位结果,分析对应的定位性能.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值