ll_device_flutter_sdk 1.0.6
ll_device_flutter_sdk: ^1.0.6 copied to clipboard
LinknLink Device Flutter SDK for controlling LinknLink devices via UDP. Provides APIs for smart home app integration with auto-pairing and device management.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_sdk/ll_device_flutter_sdk.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SDK Example',
home: LLDeviceFlutterSDKExample(),
);
}
}
class LLDeviceFlutterSDKExample extends StatefulWidget {
@override
_LLDeviceFlutterSDKExampleState createState() =>
_LLDeviceFlutterSDKExampleState();
}
class _LLDeviceFlutterSDKExampleState extends State<LLDeviceFlutterSDKExample> {
late LLDeviceFlutterSDK sdk;
List<LLDNADeviceInfo> discoveredDevices = [];
String status = 'Ready';
@override
void initState() {
super.initState();
initSDK();
}
void initSDK() {
// Get SDK instance
sdk = LLDeviceFlutterSDK.getInstance();
// Listen to device events
sdk.deviceEvents.listen((event) {
setState(() {
status = 'Device Event: ${event.toString()}';
});
print('Device Event: $event');
});
// Listen to discovery events
sdk.discoveryEvents.listen((event) {
setState(() {
status = 'Discovery Event: ${event.toString()}';
});
print('Discovery Event: $event');
});
}
// Example: Discover devices
Future<void> discoverDevices() async {
try {
setState(() {
status = 'Discovering devices...';
});
final devices = await sdk.discoverDevices(timeout: 5);
setState(() {
discoveredDevices = devices;
status = 'Found ${devices.length} devices';
});
// Print device details
for (final device in devices) {
print('Found: ${device.name} at ${device.host}');
print(
' Type: ${device.typeName} (0x${device.typeId.toRadixString(16)})');
print(' MAC: ${device.macAddress}');
print(
' Capabilities: IR=${device.capabilities.supportsIR}, RF=${device.capabilities.supportsRF}, Power=${device.capabilities.supportsPower}');
}
} catch (e) {
setState(() {
status = 'Discovery failed: $e';
});
}
}
// Example: Connect to device
Future<void> connectDevice(LLDNADeviceInfo device) async {
try {
setState(() {
status = 'Connecting to ${device.name}...';
});
final success = await sdk.connectDevice(device);
setState(() {
status = success
? 'Connected to ${device.name}'
: 'Failed to connect to ${device.name}';
});
} catch (e) {
setState(() {
status = 'Connection error: $e';
});
}
}
// Example: Control SP device
Future<void> controlSPDevice(LLDNADeviceInfo device) async {
try {
// Turn on the device
bool success = await sdk.setSPPower(device.id, true);
print('Turn ON result: $success');
// Wait a moment
await Future.delayed(Duration(seconds: 2));
// Get power state
bool? powerState = await sdk.getSPPower(device.id);
print('Power state: $powerState');
// Turn off the device
success = await sdk.setSPPower(device.id, false);
print('Turn OFF result: $success');
setState(() {
status = 'SP device controlled successfully';
});
} catch (e) {
setState(() {
status = 'SP control error: $e';
});
}
}
// Example: Learn IR code
Future<void> learnIRCode(LLDNADeviceInfo device) async {
try {
setState(() {
status = 'Learning IR code... Point remote and press button';
});
final code = await sdk.learnIRCode(device.id, timeout: 30);
if (code != null) {
print('Learned IR code: $code');
setState(() {
status = 'Learned IR code: ${code.substring(0, 20)}...';
});
// Test sending the learned code
await Future.delayed(Duration(seconds: 2));
final sendSuccess = await sdk.sendIRCode(device.id, code);
setState(() {
status = sendSuccess
? 'IR code learned and tested successfully'
: 'IR code learned but test failed';
});
} else {
setState(() {
status = 'Failed to learn IR code (timeout or no signal)';
});
}
} catch (e) {
setState(() {
status = 'IR learning error: $e';
});
}
}
// Example: Get sensor data
Future<void> getSensorData(LLDNADeviceInfo device) async {
try {
final temperature = await sdk.getRMTemperature(device.id);
final humidity = await sdk.getRMHumidity(device.id);
setState(() {
status =
'Temperature: ${temperature?.toStringAsFixed(1)}°C, Humidity: ${humidity?.toStringAsFixed(1)}%';
});
} catch (e) {
setState(() {
status = 'Sensor reading error: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Broadlink SDK Example'),
),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Status: $status',
style: TextStyle(fontWeight: FontWeight.bold)),
SizedBox(height: 16),
ElevatedButton(
onPressed: discoverDevices,
child: Text('Discover Devices'),
),
SizedBox(height: 16),
Expanded(
child: ListView.builder(
itemCount: discoveredDevices.length,
itemBuilder: (context, index) {
final device = discoveredDevices[index];
return Card(
child: ListTile(
title: Text(device.name),
subtitle: Text('${device.host} • ${device.typeName}'),
trailing: PopupMenuButton<String>(
onSelected: (action) =>
_handleDeviceAction(device, action),
itemBuilder: (context) => [
PopupMenuItem(
value: 'connect', child: Text('Connect')),
if (device.capabilities.supportsPower)
PopupMenuItem(
value: 'control_sp',
child: Text('Control Power')),
if (device.capabilities.supportsIR)
PopupMenuItem(
value: 'learn_ir', child: Text('Learn IR')),
if (device.capabilities.supportsSensors)
PopupMenuItem(
value: 'sensors', child: Text('Read Sensors')),
],
),
),
);
},
),
),
],
),
),
);
}
void _handleDeviceAction(LLDNADeviceInfo device, String action) {
switch (action) {
case 'connect':
connectDevice(device);
break;
case 'control_sp':
controlSPDevice(device);
break;
case 'learn_ir':
learnIRCode(device);
break;
case 'sensors':
getSensorData(device);
break;
}
}
@override
void dispose() {
sdk.dispose();
super.dispose();
}
}