跳到主要内容

发送导航信息

为当前路线步骤创建 NavigationInfoModel,并将其发送到设备。

前置条件

  • 设备已连接并遵循 TurnByTurnNavigationAPI
  • instructionroadName 分别不能超过 128 字节。
  • 距离和时间应使用模型文档规定的单位。

API 参考

框架

AIBuds.xcframework

协议

Swift
/// Turn by turn navigation API
protocol TurnByTurnNavigationAPI: DeviceAPI {
    /// Send navigation info
    /// - Parameters:
    ///   - info: Navigation info
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func sendNavigationInfo(
        _ info: NavigationInfoModel,
        completion: AIBudsStatusCodeCompletionHandler?
    )
}

请在 API Reference 中查看 sendNavigationInfo

导航模型

属性含义
nextStepDistanceText可直接显示的文本,例如 300m1.5km
maneuver导航指令定义的原始值。
instruction导航指令文本,最多 128 字节。
roadName道路名称文本,最多 128 字节。
alertInfo可选的 NavigationAlertInfoModel;请按导航提醒中的取值与参数规则设置。
mapProviderNavigationMapProvider 定义的原始值。
transportModeNavigationTransportMode 定义的原始值。
remainingTime剩余时间,单位为秒。
remainingDistance剩余距离,单位为米。

使用示例

Swift
guard let device = device as? TurnByTurnNavigationAPI else { return }

let info = NavigationInfoModel()
info.nextStepDistanceText = "300m"
info.maneuver = NSNumber(value: NavigationManeuver.right.rawValue)
info.instruction = "Turn right"
info.roadName = "Market Street"
info.remainingTime = 420
info.remainingDistance = 1800

device.sendNavigationInfo(info) { success, statusCode, error in
    guard success else {
        print(error?.localizedDescription ?? "Navigation update failed")
        return
    }
    print("Navigation sent: \(statusCode?.stringValue ?? "Unavailable")")
}

错误处理

发送前应验证字节长度。以 success 作为主要执行结果,并保留设备返回的 statusCode;公开 SDK 当前尚未说明各状态码的具体含义。