0.9.15 • Published 1 year ago
react-native-miio v0.9.15
react-native-miio
react-native-miio is a modified version of the original miio-api by Russtone, adapted for use in React Native environments.
The main goal of this project is to provide basic protocol functions for Xiaomi devices without making assumptions about specific devices, as there are many, and supporting all of them would be a challenging task.
What's New in This Version
- Replaced
cryptowithreact-native-quick-cryptofor cryptographic functions. - Replaced
dgramwithreact-native-udpfor UDP communication. - Replaced
Bufferwith thebufferpackage for compatibility with React Native. - Switched from
debugtoreact-native-logsfor logging. - Bug fixes and improvements for mobile platforms.
Installation
To install the package, run:
npm install react-native-quick-crypto
npm install react-native-udp
npm install react-native-miioUsage
TypeScript Example
import * as miio from "react-native-miio";
type Power = "on" | "off";
type Props = "power" | "humidity";
(async (): Promise<void> => {
let device;
try {
device = await miio.device({
address: "192.168.1.31",
token: "93db466137accd4c9c6204315c542f9c",
});
const info = await device.call<Props[], [Power, number]>("get_prop", [
"power",
"humidity",
]);
console.log(info);
} catch (err) {
console.error("ERROR: " + err);
} finally {
await device?.destroy();
}
})();JavaScript Example
const miio = require("react-native-miio");
(async () => {
let device;
try {
device = await miio.device({
address: "192.168.1.31",
token: "93db466137accd4c9c6204315c542f9c",
});
const info = await device.call("get_prop", ["power"]);
console.log(info);
} catch (err) {
console.error("ERROR: " + err);
} finally {
if (device) {
await device.destroy();
}
}
})();Debugging
To enable debugging, you can configure react-native-logs in your code. Here’s how you can set up the logger and enable debug-level logging:
import { logger } from 'react-native-miio';
// Enable debug-level logging
logger.setSeverity('debug');
(async (): Promise<void> => {
let device;
try {
device = await miio.device({
address: "192.168.1.31",
token: "93db466137accd4c9c6204315c542f9c",
});
const info = await device.call("get_prop", ["power"]);
console.log(info);
} catch (err) {
console.error("ERROR: " + err);
} finally {
await device?.destroy();
}
})();This example enables logging at the debug level for more detailed output. You can adjust the severity level to control what types of messages are logged (debug, info, warn, error, etc.).
Sample Debug Output
[debug] {"extension": "qwvi9ty5"} Starting handshake
[debug] {"extension": "qwvi9ty5"} -> %O { id: 3204069188, method: 'get_prop', params: [ 'power' ] }
[debug] {"extension": "qwvi9ty5"} <- { result: [ 'off' ], id: 3204069188 }License
MIT License. See the LICENSE file for more details.