---
name: "sendMqttMessage"
mode: "kit"
versionRequirements:
  - { name: "DeviceKit", version: "3.3.0" }
  - { name: "@ray-js/ray", version: "1.4.21" }
platform:
  - "iOS"
  - "Android"
async: true
title: "sendMqttMessage - 通过 MQTT 通道下发消息。"
---

## sendMqttMessage

> [VERSION] DeviceKit >= 3.3.0 | @ray-js/ray >= 1.4.21

> [PLATFORM] iOS, Android

> ⚡ **支持 Promise 调用** — 不传 success / fail / complete 回调时，该方法返回 Promise。

### 描述

通过 MQTT 通道下发消息。

### 参数

| 属性 | 类型 | 必填 | 默认值 | 最低版本 | 描述 |
| --- | --- | --- | --- | --- | --- |
| `message` | `Record<string, Record<string, any>>` | 是 | - | `3.3.0` | 消息内容 |
| `deviceId` | `string` | 是 | - | `3.3.0` | 设备id |
| `protocol` | `number` | 是 | - | `3.3.0` | 协议号 |
| `options` | `Record<string, Record<string, any>>` | 是 | - | `3.3.0` | 预留下发逻辑配置标记，后续可以拓展，例如下发声音，下发操作后续动作等等 |
| `complete` | `() => void` | 否 | - | - | 接口调用结束的回调函数（调用成功、失败都会执行） |
| `success` | `(params: boolean) => void` | 否 | - | - | 接口调用成功的回调函数 |
| `fail` | `(params: Object) => void` ↓见下方 | 否 | - | - | 接口调用失败的回调函数 |

#### fail 回调参数

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorMsg` | `string` | 错误信息 |
| `errorCode` | `string \| number` | 错误码 |
| `innerError` | `Object` | 错误扩展 |

##### fail(params).innerError 的属性

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorCode` | `string \| number` | 错误扩展码 |
| `errorMsg` | `string` | 错误扩展信息 |


### 示例代码

#### 基础调用

```tsx
import { device } from '@ray-js/ray'

const { sendMqttMessage } = device;

sendMqttMessage({
  message: {},
  deviceId: 'device_001',
  protocol: 0,
  options: {},
  success: (result) => {
    console.log('sendMqttMessage success', result);
  },
  fail: (error) => {
    console.log('sendMqttMessage fail', error.errorMsg);
  },
});
```
