在开发Google Home Actions时,如果你遇到了“sync不适用于有效的JSON响应”的问题,这通常意味着你的Action在处理SYNC intent时返回的JSON响应格式不符合Google Assistant的期望。SYNC intent是智能家居Action中用于发现设备的一个重要组成部分,它需要返回一个特定格式的JSON响应,以便Google Assistant能够正确识别和处理你的设备。
首先,确保你的SYNC响应遵循了Google Smart Home的官方文档中定义的格式。一个基本的SYNC响应应该包括以下结构:
{
"requestId": "请求ID",
"payload": {
"agentUserId": "用户ID",
"devices": [
{
"id": "设备ID",
"type": "设备类型",
"traits": ["设备特性"],
"name": {
"defaultNames": ["默认名称"],
"name": "设备名称",
"nicknames": ["设备昵称"]
},
"willReportState": false,
"deviceInfo": {
"manufacturer": "制造商",
"model": "型号",
"hwVersion": "硬件版本",
"swVersion": "软件版本"
},
"attributes": {
// 设备特定属性
}
}
]
}
}
使用在线工具如JSONLint来验证你的JSON响应格式是否有效。这可以帮助你快速识别格式错误或遗漏的必要字段。
如果你的响应格式正确但仍然遇到问题,检查你的日志和Google Action的错误信息。这可能会提供更多关于问题的细节。例如,如果某个必需的字段缺失或值不符合预期格式,Google Assistant通常会返回具体的错误信息。
确保你的HTTP响应头中的Content-Type
设置为application/json
。这是一个常见的问题,如果Content-Type设置错误,Google Assistant可能无法正确解析你的JSON响应。
使用Google的模拟工具和测试套件进行测试。这可以帮助你在开发过程中实时查看和调试你的Action的行为。
假设你正在使用Node.js和Firebase Functions开发你的Action,你的SYNC函数可能看起来像这样:
const functions = require('firebase-functions');
const { smarthome } = require('actions-on-google');
const app = smarthome();
app.onSync((body, headers) => {
return {
requestId: body.requestId,
payload: {
agentUserId: "123",
devices: [{
id: "washer",
type: "action.devices.types.WASHER",
traits: [
"action.devices.traits.OnOff"
],
name: {
defaultNames: ["My Washer"],
name: "Washer",
nicknames: ["Washer"]
},
willReportState: false,
deviceInfo: {
manufacturer: "Example Manufacturer",
model: "Example Model",
hwVersion: "1.0",
swVersion: "1.0.1"
},
attributes: {}
}]
}
};
});
exports.smarthome = functions.https.onRequest(app);
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云