鸿蒙 gnss 开关使能流程

先WiFi,后 定位,再从蓝牙到NFC,这个就是我大致熟悉开源鸿蒙代码的一个顺序流程,WiFi 的年前差不多基本流程熟悉了,当然还有很多细节和内容没有写到,后续都会慢慢的丰富起来,这一篇将开启GNSS的篇章,先从GNSS使能开始,代码还是选取开源鸿蒙HarmonyOS 4.0的代码基线。

界面部分代码省略,直接JS看调用哪个接口,往下梳理

代码位置:base/location/frameworks/native/source/locator.cpp ---> locator.cpp 的实现是 LocatorImpl

cpp 复制代码
void LocatorImpl::EnableAbility(bool enable)
{
    if (!Init()) {
        return;
    }
    sptr<LocatorProxy> proxy = GetProxy();
    if (proxy == nullptr) {
        LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
        return;
    }
    LocationErrCode errCode = proxy->EnableAbilityV9(enable);       ---> 使能,继续看这个
    // cache the value
    if (errCode == ERRCODE_SUCCESS) {               ---> 使能成功,保存现在的状态
        if (locationDataManager_ != nullptr) {
            locationDataManager_->SetCachedSwitchState(enable ? ENABLED : DISABLED);
        }
    }
}

// base/location/frameworks/native/source/locator_proxy.cpp
void LocatorProxy::EnableAbility(bool isEnabled)
{
    MessageParcel data;
    MessageParcel reply;
    if (!data.WriteInterfaceToken(GetDescriptor())) {
        return;
    }
    data.WriteBool(isEnabled);
    int error = SendMsgWithDataReply(static_cast<int>(LocatorInterfaceCode::ENABLE_ABILITY), data, reply);
    LBSLOGD(LOCATOR_STANDARD, "Proxy::EnableAbility Transact ErrCodes = %{public}d", error);
}
//处理这个消息 ENABLE_ABILITY
// base/location/services/location_locator/locator/source/locator_skeleton.cpp
int LocatorAbilityStub::PreEnableAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
{
    if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
        LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
            identity.ToString().c_str());
        reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
        return ERRCODE_SYSTEM_PERMISSION_DENIED;
    }
    if (!CheckSettingsPermission(reply, identity)) {
        return ERRCODE_PERMISSION_DENIED;
    }
    auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
    if (locatorAbility == nullptr) {
        LBSLOGE(LOCATOR, "PreEnableAbility: LocatorAbility is nullptr.");
        reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
        return ERRCODE_SERVICE_UNAVAILABLE;
    }
    bool isEnabled = data.ReadBool();
    // 上面主要是权限的check,这里我们看下面这句
    reply.WriteInt32(locatorAbility->EnableAbility(isEnabled)); 
    return ERRCODE_SUCCESS;
}

// base/location/services/location_locator/locator/source/locator_ability.cpp
LocationErrCode LocatorAbility::EnableAbility(bool isEnabled)
{
    LBSLOGI(LOCATOR, "EnableAbility %{public}d", isEnabled);
    int modeValue = isEnabled ? 1 : 0;
    if (modeValue == QuerySwitchState()) {
        LBSLOGD(LOCATOR, "no need to set location ability, enable:%{public}d", modeValue);
        return ERRCODE_SUCCESS;
    }
    // 更新 value 值
    Uri locationDataEnableUri(LOCATION_DATA_URI);
    LocationErrCode errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
        SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, modeValue);
    if (errCode != ERRCODE_SUCCESS) {
        LBSLOGE(LOCATOR, "%{public}s: can not set state to db", __func__);
        return ERRCODE_SERVICE_UNAVAILABLE;
    }
    UpdateSaAbility();   ---> 主要看下这个方法
    std::string state = isEnabled ? "enable" : "disable";
    WriteLocationSwitchStateEvent(state);
    return ERRCODE_SUCCESS;
}

继续看 UpdateSaAbility 方法干个啥。

cpp 复制代码
LocationErrCode LocatorAbility::UpdateSaAbility()
{
    auto event = AppExecFwk::InnerEvent::Get(EVENT_UPDATE_SA, 0);
    if (locatorHandler_ != nullptr) {
        locatorHandler_->SendHighPriorityEvent(event);    ---> 发送EVENT_UPDATE_SA 事件
    }
    return ERRCODE_SUCCESS;
}

// 处理 EVENT_UPDATE_SA 这个事件的地方:
void LocatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
{
	......... ............ .........
    LBSLOGI(LOCATOR, "ProcessEvent event:%{public}d", eventId);
    switch (eventId) {
        case EVENT_UPDATE_SA: {
            if (locatorAbility != nullptr) {
                locatorAbility->UpdateSaAbilityHandler();    ---> 看这个方法
            }
            break;
	......... ............ .........
}

void LocatorAbility::UpdateSaAbilityHandler()
{
    int state = QuerySwitchState();
    LBSLOGI(LOCATOR, "update location subability enable state, switch state=%{public}d, action registered=%{public}d",
        state, isActionRegistered);
    auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
    if (locatorBackgroundProxy == nullptr) {
        LBSLOGE(LOCATOR, "UpdateSaAbilityHandler: LocatorBackgroundProxy is nullptr");
        return;
    }
    locatorBackgroundProxy.get()->OnSaStateChange(state == ENABLED);
}

// base/location/services/location_locator/locator/source/locator_background_proxy.cpp
void LocatorBackgroundProxy::OnSaStateChange(bool enable)
{
    if (proxySwtich_ == enable || !featureSwitch_) {
        return;
    }
    LBSLOGD(LOCATOR_BACKGROUND_PROXY, "OnSaStateChange %{public}d", enable);
    proxySwtich_ = enable;
    if (enable && !requestsList_->empty()) {    ---> 位置打开,如果没有请求就不会Start Locator
        StartLocator();
    } else {
        StopLocator();
    }
}

开源鸿蒙打开location开关使能比较简单,主要是状态上的处理和更新,下一篇章继续记录发起定位的流程。

相关推荐
小冷爱学习!3 小时前
华为动态路由-OSPF-完全末梢区域
服务器·网络·华为
2501_904447744 小时前
华为发力中端,上半年nova14下半年nova15,大力普及原生鸿蒙
华为·智能手机·django·scikit-learn·pygame
MarkHD4 小时前
第十八天 WebView深度优化指南
华为·harmonyos
塞尔维亚大汉5 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【MIPI CSI】
harmonyos·领域驱动设计
别说我什么都不会5 小时前
鸿蒙轻内核M核源码分析系列十五 CPU使用率CPUP
操作系统·harmonyos
feiniao86516 小时前
2025年华为手机解锁BL的方法
华为·智能手机
塞尔维亚大汉7 小时前
OpenHarmony(鸿蒙南向)——平台驱动开发【I3C】
harmonyos·领域驱动设计
VVVVWeiYee7 小时前
BGP配置华为——路径优选验证
运维·网络·华为·信息与通信
今阳9 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
余多多_zZ9 小时前
鸿蒙初学者学习手册(HarmonyOSNext_API14)_组件截图(@ohos.arkui.componentSnapshot (组件截图) )
学习·华为·harmonyos·鸿蒙·鸿蒙系统