You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ADB向Movesense发PUT请求持续遇BAD_REQUEST错误求助

解决Movesense sens_output_test_app中PUT请求BAD_REQUEST错误及自定义OnPutRequest实现

问题背景

  • 使用Movesense官方sens_output_test_app示例,通过ADB GET命令可成功获取Sample/SensorOutput/Config参数:
    adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type get --es path Sample/SensorOutput/Config --es value '''{}'''
    
  • 但执行以下PUT命令时持续返回BAD_REQUEST(400):
    adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type put --es path Sample/SensorOutput/Config --es value '{\"config\": {\"path\": \"UNKNOWN\", \"period\": 60, \"sampleRate\": 100}}'
    
  • 错误日志:
    2023-03-24 14:49:49.772 6471-6503/com.movesense.showcaseapp E/Komposti: [SDS RESPONSE] type: PUT status: BAD_REQUEST header: {\"TaskId\": 46, \"Uri\": \"suunto://223130000601/Sample/SensorOutput/Config\", \"Content-Length\": 0, \"Reason\": \"BAD_REQUEST\", \"Status\": 400} error: expected '\"' in object, got 'c' (99) 
    2023-03-24 14:49:49.773 6471-6503/com.movesense.showcaseapp D/MdsOperationHandler: SDSInternalCallback with call type: 4 header: {\"TaskId\": 46, \"Uri\": \"suunto://223130000601/Sample/SensorOutput/Config\", \"Content-Length\": 0, \"Reason\": \"BAD_REQUEST\", \"Status\": 400} 
    2023-03-24 14:49:49.778 6471-6471/com.movesense.showcaseapp E/AdbBridge: onError() put com.movesense.mds.MdsException: Failed status: 400, reason: BAD_REQUEST at com.movesense.mds.internal.operation.MdsRestOperation.onResponse(MdsRestOperation.java:81) at com.movesense.mds.internal.operation.MdsResponseOperation.protectedRun(MdsResponseOperation.java:26) at com.movesense.mds.internal.workqueue.QueueOperation.run(QueueOperation.java:70) at com.movesense.mds.internal.workqueue.WorkQueueImpl.runOperation(WorkQueueImpl.java:79) at com.movesense.mds.internal.workqueue.WorkQueueImpl$1.run(WorkQueueImpl.java:30) at java.lang.Thread.run(Thread.java:1012)
    

错误原因分析

从错误日志expected '\"' in object, got 'c'可以明确:ADB命令中的JSON转义格式错误,导致MDS解析请求体时出现语法错误。Windows Shell的转义规则与Movesense期望的格式不匹配,当前的转义方式让实际传递的JSON结构失效。

解决方法

修正ADB PUT命令的JSON格式

针对Windows环境,调整JSON转义方式,使用正确的引号处理:

adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type put --es path Sample/SensorOutput/Config --es value "{\"config\": {\"path\": \"UNKNOWN\", \"period\": 60, \"sampleRate\": 100}}"

或者直接用单引号包裹JSON(避免多层转义干扰):

adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type put --es path Sample/SensorOutput/Config --es value '{"config": {"path": "UNKNOWN", "period": 60, "sampleRate": 100}}'

注:不同Shell环境(cmd/PowerShell)转义规则有差异,若用PowerShell需进一步调整转义方式。

规避本地Shell转义干扰

先进入设备Shell再执行命令,减少本地Shell的转义影响:

adb shell
am broadcast -a "android.intent.action.MOVESENSE" --es type put --es path Sample/SensorOutput/Config --es value '{"config": {"path": "UNKNOWN", "period": 60, "sampleRate": 100}}'

自定义OnPutRequest参数设置示例

结合sens_output_test_app的实现逻辑,以下是自定义PUT请求处理的核心步骤:

1. 在YAML中定义PUT接口结构

在服务的YAML配置文件中声明PUT接口的请求体规范:

/Sample/SensorOutput/Config:
  put:
    description: 更新传感器输出配置
    requestBody:
      contentType: application/json
      schema:
        type: object
        properties:
          config:
            type: object
            properties:
              path:
                type: string
              period:
                type: integer
              sampleRate:
                type: integer
    responses:
      200:
        description: 请求成功
      400:
        description: 请求参数错误

2. 在Service中实现OnPutRequest处理逻辑

在Service类中重写OnPutRequest方法,完成参数解析与业务处理:

#include "SensorOutputService.h"
#include <nlohmann/json.hpp>

using json = nlohmann::json;

void SensorOutputService::OnPutRequest(const std::string& path, const json& requestJson, MdsResponse& response)
{
    // 检查请求体是否包含config字段
    if (!requestJson.contains("config"))
    {
        response.statusCode = MdsResponse::BAD_REQUEST;
        response.reason = "缺少'config'字段";
        return;
    }

    const json& configJson = requestJson["config"];
    
    // 验证必填参数
    if (!configJson.contains("path") || !configJson.contains("period") || !configJson.contains("sampleRate"))
    {
        response.statusCode = MdsResponse::BAD_REQUEST;
        response.reason = "config中缺少必填字段";
        return;
    }

    // 解析参数
    std::string sensorPath = configJson["path"];
    int32_t period = configJson["period"];
    int32_t sampleRate = configJson["sampleRate"];

    // 执行自定义业务逻辑(例如更新传感器配置)
    m_sensorConfig.path = sensorPath;
    m_sensorConfig.period = period;
    m_sensorConfig.sampleRate = sampleRate;

    // 返回成功响应
    response.statusCode = MdsResponse::OK;
    response.reason = "配置更新成功";
}

3. 注册服务到MDS

在应用初始化代码中注册自定义Service,确保MDS能将PUT请求路由到对应处理逻辑:

#include "SensorOutputService.h"
#include <Mds.h>

void AppInit()
{
    Mds& mdsInstance = Mds::GetInstance();
    // 注册自定义服务
    mdsInstance.RegisterService(std::make_shared<SensorOutputService>());
}

内容的提问来源于stack exchange,提问作者Govind Padhiyar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 13:32:56