# MoYoung Glasses Flutter SDK / 魔样智能眼镜 Flutter SDK

[English](#english) | [中文](#chinese)

---

## English

A comprehensive Flutter plugin for communicating with MoYoung smart glasses via Bluetooth. This plugin provides a unified API for both Android and iOS platforms, wrapping the native SDKs to enable Flutter applications to seamlessly interact with MoYoung smart glasses.

## Chinese

一个全面的 Flutter 插件，用于通过蓝牙与魔样智能眼镜通信。该插件为 Android 和 iOS 平台提供了统一的 API，封装了原生 SDK，使 Flutter 应用程序能够无缝地与魔样智能眼镜交互。

[![pub package](https://img.shields.io/pub/v/moyoung_glasses_ble_plugin.svg)](https://pub.dev/packages/moyoung_glasses_ble_plugin)
[![Platform](https://img.shields.io/badge/platform-android%20%7C%20ios-blue.svg)](https://pub.dev/packages/moyoung_glasses_ble_plugin)
[![Language](https://img.shields.io/badge/language-%E4%B8%AD%E6%96%87%20%7C%20English-orange.svg)](https://pub.dev/packages/moyoung_glasses_ble_plugin)

## Features / 功能特性

- 🔍 **Device Scanning / 设备扫描** - Scan and discover nearby MoYoung smart glasses / 扫描并发现附近的 MoYoung 智能眼镜
- 🔗 **Device Connection / 设备连接** - Connect, disconnect and manage device bonding / 连接、断开和管理设备绑定
- 📱 **Device Control / 设备控制** - Restart, shutdown, factory reset / 重启、关机、恢复出厂设置
- 🔋 **Battery Monitoring / 电池监控** - Real-time battery level and charging status / 实时电池电量和充电状态
- 📸 **Camera Control / 相机控制** - Take photos and record videos / 拍照和录像
- 🎵 **Audio Features / 音频功能** - Audio control, recording and playback / 音频控制、录音和播放
- 📁 **File Management / 文件管理** - Access and manage files on device / 访问和管理设备上的文件
- 📶 **Wi-Fi Support / Wi-Fi 支持** - Enable/disable Wi-Fi, file synchronization / 开启/关闭 Wi-Fi，文件同步
- 🤖 **AI Functions / AI 功能** - AI voice reply and image recognition / AI 语音回复和图像识别
- ⏰ **Time & Language / 时间和语言** - Sync time and set language preferences / 同步时间和设置语言偏好
- 🔄 **OTA Updates / OTA 升级** - Over-the-air firmware updates / 空中固件升级
- 📍 **Location Services / 位置服务** - GPS and positioning features / GPS 和定位功能
- 🏃 **Health Monitoring / 健康监测** - Step counting, heart rate, sleep tracking / 计步、心率、睡眠跟踪

## Installation

Add this to your package's `pubspec.yaml` file:

```yaml
dependencies:
  moyoung_glasses_ble_plugin: ^1.0.0
```

Then run:

```bash
flutter pub get
```

## Platform Setup

### Android

Add the following permissions to your `android/app/src/main/AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
```

### iOS

Add the following to your `ios/Runner/Info.plist`:

```xml
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app needs Bluetooth access to connect with smart glasses</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>This app needs Bluetooth access to connect with smart glasses</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs location access to scan for Bluetooth devices</string>
```

## Quick Start

```dart
import 'package:moyoung_glasses_ble_plugin/moyoung_glasses_ble.dart';

class _MyAppState extends State<MyApp> {
  final MoYoungGlassesBle _glasses = MoYoungGlassesBle();
  
  @override
  void initState() {
    super.initState();
    _initGlasses();
  }
  
  Future<void> _initGlasses() async {
    // Check if Bluetooth is enabled
    bool isEnabled = await _glasses.checkBluetoothEnable;
    if (!isEnabled) {
      print('Please enable Bluetooth');
      return;
    }
    
    // Start scanning for devices
    _glasses.startScan(10);
    
    // Listen for scan results
    _glasses.bleScanEveStm.listen((device) {
      print('Found device: ${device.name} (${device.address})');
    });
    
    // Listen for connection state
    _glasses.connStateEveStm.listen((state) {
      print('Connection state: ${state.connectState}');
    });
    
    // Listen for battery updates
    _glasses.batteryEveStm.listen((battery) {
      print('Battery: ${battery['battery']}% - Charging: ${battery['charging']}');
    });
  }
  
  Future<void> _connectDevice(String address) async {
    bool success = await _glasses.connect('{"address":"$address"}');
    if (success) {
      print('Connected successfully');
    }
  }
}
```

## API Reference

### Main Classes

- **`MoYoungGlassesBle`** - Main entry point for all operations
- **`BleScanBean`** - Represents a scanned device
- **`ConnectStateBean`** - Represents connection state

### Key Methods

#### Bluetooth & Scanning
```dart
// Check Bluetooth status
bool isEnabled = await glasses.checkBluetoothEnable;

// Start scanning
await glasses.startScan(10); // 10 seconds

// Stop scanning
await glasses.stopScan();
```

#### Connection Management
```dart
// Connect to device
await glasses.connect('{"address":"XX:XX:XX:XX:XX:XX"}');

// Disconnect
await glasses.disconnect();

// Get connected device
String device = await glasses.connectedDevice;
```

#### Device Information
```dart
// Get battery info
Map<String, dynamic> battery = await glasses.queryBattery();

// Get device version
String version = await glasses.queryDeviceVersion();

// Sync time
await glasses.syncTime();
```

#### Camera Control
```dart
// Take photo
await glasses.takePhoto();

// Start recording (temporarily disabled)
// await glasses.startVideoRecord();

// Stop recording (temporarily disabled)
// await glasses.stopVideoRecord();
```

#### Event Streams
```dart
// Listen to scan results
glasses.bleScanEveStm.listen((BleScanBean device) {
  // Handle scanned device
});

// Listen to connection state
glasses.connStateEveStm.listen((ConnectStateBean state) {
  // Handle connection changes
});

// Listen to battery updates
glasses.batteryEveStm.listen((Map<String, dynamic> battery) {
  // Handle battery changes
});
```

For a complete list of all APIs, please visit: https://github.com/liangqian609/moyoung_glasses_ble_plugin/blob/main/API_LIST.md

## Example App

This repository contains a complete Flutter app demonstrating all features of the plugin. Clone the repository and run `flutter run` to try it out.

GitHub Repository: https://github.com/liangqian609/moyoung_glasses_ble_plugin

## Requirements

- Flutter SDK >= 3.0.0
- Android SDK >= 21 (Android 5.0)
- iOS >= 12.0
- Bluetooth LE support

## iOS Configuration

📖 **详细的iOS接入指南**：[iOS_INTEGRATION_GUIDE.md](https://github.com/liangqian609/moyoung_glasses_ble_plugin/blob/main/ios/iOS_INTEGRATION_GUIDE.md)

For detailed iOS integration guide, please visit: [iOS_INTEGRATION_GUIDE.md](https://github.com/liangqian609/moyoung_glasses_ble_plugin/blob/main/ios/iOS_INTEGRATION_GUIDE.md)

## Support

- **GitHub Repository**: https://github.com/liangqian609/moyoung_glasses_ble_plugin
- **Example App**: https://github.com/liangqian609/moyoung_glasses_ble_plugin (The entire repository is the example app)
- **Technical Support**: jack@moyoung.com
- **Homepage**: https://www.moyoung.com/en/

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for a list of changes in each version.
