caitun_ble_agent_flutter 0.0.1
caitun_ble_agent_flutter: ^0.0.1 copied to clipboard
Flutter SDK for BLE device management and translation services.
caitun_ble_agent_flutter #
Flutter SDK for BLE device management and translation services.
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
caitun_ble_agent_flutter: ^0.0.1
iOS Setup #
1. Configure Permissions #
Add the following permissions to your ios/Runner/Info.plist:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>需要蓝牙权限来连接BLE设备</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要蓝牙权限来连接BLE设备</string>
2. Run pod install #
cd ios
pod install
Android Setup #
1. Configure Permissions #
Add the following permissions to your android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
<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"/>
2. Runtime Permission Request #
On Android 12+, you need to request the following runtime permissions:
BLUETOOTH_SCANBLUETOOTH_CONNECTACCESS_FINE_LOCATION
On iOS, you need to request Bluetooth permission at runtime.
Usage #
Import #
import 'package:caitun_ble_agent_flutter/caitun_ble_agent_flutter.dart';
Initialize the SDK #
final bleAgent = BleAgentFlutter();
await bleAgent.initialize(
userId: 'your_user_id',
organizationId: 'your_organization_id',
secret: 'your_secret',
appPackageName: 'your_app_package_name',
country: 'your_country',
);
Set Up Listeners #
Before scanning or translating, set up listeners to receive device and translation events:
// Device listener
bleAgent.addDeviceListener(_DeviceListener(
onDeviceFoundCallback: (BleDevice device) {
print('Found device: ${device.deviceName} (${device.mac})');
},
onConnectedCallback: (BleDevice device) {
print('Connected: ${device.deviceName}');
},
onDisconnectedCallback: (BleDevice device) {
print('Disconnected: ${device.deviceName}');
},
onErrorCallback: (String error) {
print('Error: $error');
},
onAudioModeUpdatedCallback: (int errorCode, String errorMessage) {
print('Audio mode updated: $errorCode - $errorMessage');
},
onAudioStreamDataCallback: (List<int> audioData) {
// Handle audio stream data (PCM format)
},
));
// Translation listener
bleAgent.addTranslationListener(_TranslationListener(
onRecognitionResultCallback: (String id, String text, bool isFinal, bool isLeft) {
print('Recognition: $text (final: $isFinal, left: $isLeft)');
},
onTranslationCallback: (String id, String sourceText, String translatedText, bool isLeft) {
print('Translation: $sourceText -> $translatedText');
},
onErrorCallback: (int errorCode, String error) {
print('Translation error: [$errorCode] $error');
},
onTtsFileCallback: (String id, String filePath, bool isLeft) {
print('TTS file: $filePath');
},
onConsumeTokensCallback: (String tokenType, int tokens) {
print('Token consumed: $tokenType - $tokens');
},
));
Scan for Devices #
// Start scanning
await bleAgent.scanDevices();
// Stop scanning
await bleAgent.stopScan();
Connect to Device #
await bleAgent.connectDevice(device.deviceId);
Fetch Supported Languages #
final languages = await bleAgent.fetchLanguages();
for (var lang in languages) {
print('${lang.key}: ${lang.value}');
}
Translation #
// Start translation
await bleAgent.startTranslation(
workMode: WorkMode.callTranslation, // telephoneSubtitle, callTranslation, bidirectionalTranslation
sourceLang: 'zh',
targetLang: 'en',
stepMode: StepMode.tts, // asr, translation, tts
);
// Stop translation (also stops recording)
await bleAgent.stopRecordingAndTranslation();
WorkMode enum values:
WorkMode.telephoneSubtitle- 电话字幕WorkMode.callTranslation- 通话翻译WorkMode.bidirectionalTranslation- 双向翻译
StepMode enum values:
StepMode.asr- 仅语音识别StepMode.translation- 识别+翻译StepMode.tts- 识别+翻译+语音合成
Call Recording #
// Start call recording
await bleAgent.startCallRecording();
// Audio stream data is received via the onAudioStreamData callback
// The data is in PCM format (16kHz, dual channel, 16-bit)
// Stop call recording
await bleAgent.stopRecordingAndTranslation();
TTS Cache Management #
// Enable TTS cache
await bleAgent.setTtsCacheEnabled(true);
// Disable TTS cache
await bleAgent.setTtsCacheEnabled(false);
// Check cache status
final isEnabled = await bleAgent.isTtsCacheEnabled();
// Delete all TTS cache files
await bleAgent.deleteAllTtsCacheFiles();
Check Status #
// Check if SDK is initialized
final initialized = await bleAgent.isInitialized();
// Check if device is connected
final connected = await bleAgent.isDeviceConnected();
Disconnect Device #
await bleAgent.disconnectDevice();
Release SDK #
await bleAgent.release();
API Reference #
BleDevice #
| Property | Type | Description |
|---|---|---|
deviceId |
String |
Device ID |
deviceName |
String |
Device name |
mac |
String |
MAC address |
rssi |
int |
Signal strength |
Language #
| Property | Type | Description |
|---|---|---|
key |
String |
Language display name |
value |
String |
Language code |
BleDeviceListener #
| Method | Description |
|---|---|
onDeviceFound(BleDevice) |
Device found during scan |
onConnected(BleDevice) |
Device connected |
onDisconnected(BleDevice) |
Device disconnected |
onError(String) |
Error occurred |
onAudioModeUpdated(int, String) |
Audio mode updated (errorCode != 0 means error) |
onAudioStreamData(List<int>) |
Audio stream data received (PCM) |
TranslationListener #
| Method | Description |
|---|---|
onRecognitionResult(String id, String text, bool isFinal, bool isLeft) |
Speech recognition result |
onTranslation(String id, String sourceText, String translatedText, bool isLeft) |
Translation result |
onError(int errorCode, String error) |
Translation error |
onTtsFile(String id, String filePath, bool isLeft) |
TTS audio file generated |
onConsumeTokens(String tokenType, int tokens) |
Token consumption |
Example #
See the example directory for a complete sample app.
Troubleshooting #
Android Build Errors #
If you encounter NDK errors:
- Open Android Studio
- Go to Preferences → Appearance & Behavior → System Settings → Android SDK
- Select SDK Tools tab
- Install NDK (Side by side)
- Set the NDK version in
android/gradle.properties:android.ndkVersion=28.2.13676358
License #
This project is licensed under the MIT License - see the LICENSE file for details.