Hướng dẫn sử dụng XenseAR SDK cho Flutter
Chuẩn bị:
- Nội dung các file của Template project:
main.dart,web_view_page.dart,xense_ar.dart - Project cần tích hợp
Quy trình thực hiện:
1. Copy file xense_ar.dart vào project cần tích hợp:
import 'package:flutter/services.dart';
import 'dart:async';
class XenseAR {
static const MethodChannel _channel =
MethodChannel('xense_ar');
static const EventChannel _eventChannel =
EventChannel('xense_ar/events');
static Stream<String> get unityMessages {
return _eventChannel
.receiveBroadcastStream()
.map((event) => event.toString());
}
static Future<bool> openUnity() async {
final result = await _channel.invokeMethod<bool>(
'openUnity',
);
return result ?? false;
}
static Future<String> sendCommand(
String command,
String value,
) async {
final result = await _channel.invokeMethod<String>(
'sendCommand',
{
'command': command,
'value': value,
},
);
return result ?? '';
}
}2. Tham khảo file web_view_page, chuẩn bị các thành phần cần thiết để sử dụng SDK
- Import các package cần thiết
import 'package:flutter/material.dart';
import 'xense_ar.dart';
import 'package:flutter/services.dart';
import 'dart:async';- Chuẩn bị một StatefulWidget, trong đó bổ sung các thành phần thực hiện handle Message và mở sang trải nghiệm AR, lưu ý cập nhật các giá trị
provided_by_xense, liên hệ nhà cung cấp để nhận thông tin chi tiết.
StreamSubscription<String>? _unitySubscription;
@override
void initState() {
super.initState();
debugPrint('WEBVIEW PAGE INIT');
_listenUnityMessages();
}
Future<void> _openUnity() async {
try {
await XenseAR.openUnity();
} catch (e) {
debugPrint('Failed to open Unity: $e');
}
}
void _listenUnityMessages() {
_unitySubscription = XenseAR.unityMessages.listen(
(message) {
debugPrint(
'MESSAGE FROM UNITY: $message',
);
_handleUnityMessage(message);
},
onError: (error) {
debugPrint(
'UNITY EVENT ERROR: $error',
);
},
);
}
Future<void> _handleUnityMessage(String message) async {
if (message.contains('webviewReady')) {
try {
final symmetricKeyResponse = await XenseAR.sendCommand(
'updateSymmetricKey',
'provided_by_xense',
);
debugPrint(
'updateSymmetricKey SUCCESS: $symmetricKeyResponse',
);
final encryptedConfigResponse = await XenseAR.sendCommand(
'updateEncryptedConfig',
'provided_by_xense',
);
debugPrint(
'updateEncryptedConfig SUCCESS: $encryptedConfigResponse',
);
} on PlatformException catch (e) {
debugPrint(
'update config ERROR: ${e.code} - ${e.message}',
);
}
} else if (
message.contains('unityReady') ||
message.contains('backToHomeScreen')
) {
try {
final response = await XenseAR.sendCommand(
'updateHomeScreenUrl',
'provided_by_xense',
);
debugPrint(
'updateHomeScreenUrl SUCCESS: $response',
);
} on PlatformException catch (e) {
debugPrint(
'updateHomeScreenUrl ERROR: ${e.code} - ${e.message}',
);
}
} else if (message.contains('getNativeParameters')) {
try {
final response = await XenseAR.sendCommand(
'updateNativeParameters',
r'{\"userID\": \"abc\", \"auth_key\": \"xyz\"}',
);
debugPrint(
'updateNativeParameters SUCCESS: $response',
);
} on PlatformException catch (e) {
debugPrint(
'updateNativeParameters ERROR: ${e.code} - ${e.message}',
);
}
}
}
@override
void dispose() {
_unitySubscription?.cancel();
super.dispose();
}3. Đã sẵn sàng để chạy
flutter clean
flutter run -d <your-device>Last updated on