Android Wifi定位
2010-08-23 01:31:00 来源:WEB开发网核心提示:AndroidWifiDataProvider *self_ptr =63 reinterpret_cast(self);64 WifiData new_wifi_data;65 if (wifi_data) {66 InitFromJava(wifi_data, &new_wifi_data);67 }68 // W
AndroidWifiDataProvider *self_ptr =63 reinterpret_cast(self);64 WifiData
new_wifi_data;65 if (wifi_data) {66 InitFromJava(wifi_data,
&new_wifi_data);67 }68 // We notify regardless of whether new_wifi_data is
empty69 // or not. The arbitrator will decide what to do with an empty70 //
WifiData object.71 self_ptr->NewWifiDataAvailable(&new_wifi_data);72 }73
74 void AndroidWifiDataProvider::NewWifiDataAvailable(WifiData* new_wifi_data)
{75 assert(supported_);76 assert(new_wifi_data);77 bool is_update_available =
false;78 data_mutex_.Lock();79 is_update_available =
wifi_data_.DiffersSignificantly(*new_wifi_data);80 wifi_data_ =
*new_wifi_data;81 // Avoid holding the mutex locked while notifying observers.82
data_mutex_.Unlock();83 84 if (is_update_available) {85 is_first_scan_complete_
= true;86 NotifyListeners();87 }88 89 #if USING_CCTESTS90 // This is needed for
running the WiFi test on the emulator.91 // See wifi_data_provider_android.h for
details.92 if (!first_callback_made_ &&
wifi_data_.access_point_data.empty()) {93 first_callback_made_ = true;94
NotifyListeners();95 }96 #endif97 }98
从以上代码可以看出,WIFI定位和基站定位的逻辑差不多,只是前者获取的WIFI的扫描结果,而后者获取的基站信息。
后面代码的基本上就统一起来了,接下来我们继续看。
5.把变化(WIFI/基站)通知给相应的监听者。
1 AndroidWifiDataProvider和AndroidRadioDataProvider都是继承了DeviceDataProviderImplBase,DeviceDataProviderImplBase的主要功能就是管理所有Listeners。 2 3 static DeviceDataProvider *Register(ListenerInterface *listener) { 4 MutexLock mutex(&instance_mutex_); 5 if (!instance_) { 6 instance_ = new DeviceDataProvider(); 7 } 8 assert(instance_); 9 instance_->Ref(); 10 instance_->AddListener(listener); 11 return instance_; 12 } 13 14 static bool Unregister(ListenerInterface *listener) { 15 MutexLock mutex(&instance_mutex_); 16 if (!instance_->RemoveListener(listener)) { 17 return false; 18 } 19 if (instance_->Unref()) { 20 delete instance_; 21
更多精彩
赞助商链接