rtl_ir_control 1.0.2
rtl_ir_control: ^1.0.2 copied to clipboard
A utility designed to simplify the setup and configuration of Remotec products using Bluetooth Low Energy (BLE).
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:rtl_ir_control/rtl_ir_control.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: SetupPage(),
),
);
}
}
class SetupPage extends StatefulWidget {
const SetupPage({super.key});
@override
State<SetupPage> createState() => _SetupPageState();
}
class _SetupPageState extends State<SetupPage> {
final _rtlIrControlPlugin = RtlIrControl();
final blufi = RTLBlufiSetupUtils();
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: StreamBuilder(
stream: _rtlIrControlPlugin.startScan(
nameFilter: ['Roomate', 'BLUFI'],
),
builder: (
BuildContext context,
AsyncSnapshot<List<RTLBluetoothScanRecord>> snapshot,
) {
return ListView(
children: [
snapshot.error != null
? Text(snapshot.error.toString())
: Container(),
snapshot.connectionState == ConnectionState.active
? LinearProgressIndicator()
: Container(),
...snapshot.data
?.map(
(e) => Card(
child: Column(
children: [
ListTile(
title: Text(e.device.name ?? ''),
subtitle: Text(e.device.id),
trailing: ElevatedButton(
onPressed: () => setup(e.device.id),
child: Text('setup'),
),
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed:
() => readDeviceInfo(e.device.id),
child: Text('read info'),
),
ElevatedButton(
onPressed:
() => downloadCode(
e.device.id,
dataJson,
),
child: Text('download'),
),
],
),
],
),
),
)
.toList() ??
[],
],
);
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => setState(() {}),
child: Text('Scan'),
),
],
),
],
);
}
/// Reads and displays device information for a given device ID.
///
/// [id] The unique identifier of the device.
///
/// Returns a [Future<void>] that completes when the operation is finished.
/// Throws an exception if the operation fails.
Future<void> readDeviceInfo(String id) async {
try {
// Initialize the device using the provided ID
final device = RtlIrControl().getDevice(id);
// Establish connection to the device
await device.connect();
// Create handler for device operations
final roomateDeviceHandler = RoomateDeviceHandler(device);
var masterKey = await roomateDeviceHandler.readMasterKey();
var macAddress = await roomateDeviceHandler.readMacAddress();
var deviceName = await roomateDeviceHandler.readDeviceName();
var firmwareVersion = await roomateDeviceHandler.readFirmwareVersion();
var hardwareVersion = await roomateDeviceHandler.readHardwareVersion();
var modelNumber = await roomateDeviceHandler.readRemoteModel();
// Format and display device information
final infoMessage = '''
Device Name: $deviceName
MAC Address: $macAddress
Firmware Version: $firmwareVersion
Hardware Version: $hardwareVersion
Model Number: $modelNumber
Master Key: $masterKey
''';
showMessageDialog('Device Information', infoMessage);
} catch (e) {
showMessageDialog('error', e.toString());
}
}
var dataJson = 'your api response';
Future<void> downloadCode(String id, String apiJson) async {
try {
RTLIrCodeset codeset = RTLIrCodeset.fromApiJson(apiJson);
var device = RtlIrControl().getDevice(id);
await device.connect();
RoomateDeviceHandler roomateDeviceHandler = RoomateDeviceHandler(device);
roomateDeviceHandler
.downloadCodeFromCodeset(
channel: 1,
brandName: "LG",
codeNum: "81",
codeset: codeset,
)
.listen((progress) {
// print(progress);
});
} catch (e) {
showMessageDialog('error', e.toString());
}
}
Future<void> setup(String id) async {
try {
_rtlIrControlPlugin.stopScan();
await blufi.setCurrentId(id);
await blufi.connect();
// get device wifi list
var list = await blufi.scanWifiList();
var ssid = await selectWifi(list);
var password = await inputPassword(ssid);
await blufi.configProvision(ssid: ssid, password: password);
showMessageDialog('Setup', 'Completed');
} catch (e) {
if (e is RTLBluetoothException) {
showMessageDialog(e.toString(), e.getMessage());
} else {
showMessageDialog('Unknown Error', e.toString());
}
}
}
void showMessageDialog(String title, String message) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
child: Container(
padding: EdgeInsets.all(16),
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
title,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Text(message, style: TextStyle(fontSize: 16)),
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'),
),
],
),
),
);
},
);
}
Future<String> selectWifi(List<String> wifiList) {
Completer<String> completer = Completer();
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
child: Container(
padding: EdgeInsets.all(16),
width: double.infinity,
child: ListView(
children:
wifiList
.map(
(e) => Card(
child: ListTile(
title: Text(e),
onTap: () {
completer.complete(e);
Navigator.pop(context);
},
),
),
)
.toList(),
),
),
);
},
);
return completer.future;
}
Future<String> inputPassword(String ssid) {
TextEditingController controller = TextEditingController();
Completer<String> completer = Completer();
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
child: Container(
padding: EdgeInsets.all(16),
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: controller,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
ElevatedButton(
onPressed: () {
if (controller.text.toString().isEmpty) return;
completer.complete(controller.text.toString());
Navigator.pop(context);
},
child: Text('OK'),
),
],
),
),
);
},
);
return completer.future;
}
}