rtl_ir_control 1.0.6
rtl_ir_control: ^1.0.6 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: [
Text(
'To enter pairing mode for BW8459, press and hold the left button until the blue LED flashes continuously',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
Expanded(
child: StreamBuilder(
stream: _rtlIrControlPlugin.startScan(
nameFilter: ['BW8459', 'BLUFI', 'RM2'],
),
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.advName ?? e.device.name!,
),
subtitle: Text(e.device.id),
trailing: ElevatedButton(
onPressed: () =>
verifyAndSetup(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'),
),
],
),
],
);
}
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 handler = BW8459DeviceHandler(device);
var masterKey = await handler.readMasterKey();
var macAddress = await handler.readMacAddress();
var firmwareVersion = await handler.readFirmwareVersion();
var hardwareVersion = await handler.readHardwareVersion();
var modelNumber = await handler.readRemoteModel();
// Format and display device information
final infoMessage = '''
MAC Address: $macAddress
Firmware Version: $firmwareVersion
Hardware Version: $hardwareVersion
Model Number: $modelNumber
Master Key: $masterKey
''';
device.disconnect();
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();
final handler = BW8459DeviceHandler(device);
handler
.downloadCodeFromCodeset(
channel: 1,
brandName: "LG",
codeNum: "81",
codeset: codeset,
)
.listen((progress) {
print(progress);
}).onDone(() {
showMessageDialog('Notice', 'download completed');
device.disconnect();
});
} catch (e) {
showMessageDialog('error', e.toString());
}
}
Future<void> verifyAndSetup(String id) async {
try {
// stop scan
_rtlIrControlPlugin.stopScan();
// 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 handler = BW8459DeviceHandler(device);
// OPTIONAL - check device is already configured
bool isConfigured = await handler.isConfigured();
await handler.enterSetupMode();
// IMPORTANT: release connection for blufi setup
await device.disconnect();
await startBlufiSetup(id);
} catch (e) {
if (e is RTLBluetoothException) {
showMessageDialog(e.toString(), e.getMessage());
} else {
showMessageDialog('Unknown Error', e.toString());
}
}
}
Future<void> startBlufiSetup(String id) async {
try {
await blufi.setCurrentId(id);
await blufi.connect();
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: () {
Navigator.pop(context);
if (controller.text.toString().isEmpty) {
completer.completeError(
Exception('Password cannot be empty'),
);
return;
}
completer.complete(controller.text.toString());
},
child: Text('OK'),
),
],
),
),
);
},
);
return completer.future;
}
}