# 设备声音与实用功能

> Device 门面的其余部分 —— beep、vibrate、剪贴板、无线电、TTS/STT、平台。

Source: https://crossfly.dev/zh/docs/data-and-services/device-sound-and-misc — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# 设备声音与实用功能

门面 **`Device`** 汇集了小巧且可移植的功能。全部为静态方法。

## 反馈与系统
```java
Device.beep();              // 系统提示音
Device.vibrate(200);        // 震动 200 毫秒
Device.sleep(500);          // 暂停当前线程 500 毫秒
int livre = Device.getFreeMemory();
Device.show("https://exemplo.com");   // 在操作系统的外部应用中打开 URL/资源
```

## 剪贴板
```java
Device.clipboardCopy("texto");
String colado = Device.clipboardPaste();
```

## 身份与平台
```java
if (Device.platform.isMobile()) { ... }     // ANDROID/IPHONE/IPAD
if (Device.platform.isDesktop()) { ... }    // WINDOWS/LINUX/MACOS
if (Device.platform.isIOS()) { ... }        // IPHONE/IPAD
String base = Device.homeFolder;            // 应用的基础目录
```
`Platform`：`JAVA` · `WINDOWS` · `LINUX` · `IPHONE` · `ANDROID` · `IPAD` · `MACOS`。

## 无线电
```java
WirelessState wifi = Device.getWirelessState(WirelessType.WIFI);   // UNSUPPORTED/DISABLED/ENABLED
Device.enableWireless(WirelessType.BLUETOOTH, true);
```
`WirelessType`：`WIFI` · `PHONE` · `BLUETOOTH`。

## 语音 —— TTS 与 STT
```java
Device.textToVoice("Olá, mundo");   // 朗读文本 (TTS)

Device.voiceToText(new Device.VoiceToTextListener() {   // 语音识别 (STT)
   public void onStart() {}
   public void onFinish() {}
   public void onResult(String texto) { Component.onUI(() -> campo.text(texto)); }
   public void onError(int code, String msg) { Component.onUI(() -> Hint.show(msg, 2000)); }
});
```
> STT 的回调会在 **绘制线程之外** 到达 → 通过 `Component.onUI(...)` 返回。

## ⚠️ 迁移说明
- `Sound.*` 和 `RadioState` **已被 `Device` 吸收** —— `Sound.tone`/`play` **已不复存在**。
- 在 **Web/TeaVM** 上，其中大部分为 *stub*（震动、无线电、语音）—— 请处理 Web 目标。

## 最佳实践
- 语音/无线电回调通过 `onUI(...)` 返回 UI。
- 在假定某个无线电存在之前，先检查 `WirelessState.UNSUPPORTED`。

## 常见错误
- 调用 `Sound.play(...)`/`RadioState`（已移除）—— 请使用 `Device`。
- 在 STT 的 `onResult` 中未经 `onUI` 直接操作 UI。

## 另请参阅
- [生物识别](/docs/data-and-services/device-biometry) · [后台工作](/docs/data-and-services/background-work)
