capture_flutter_beta 1.1.1
capture_flutter_beta: ^1.1.1 copied to clipboard
This is the beta version of Socket Mobile Inc's Flutter SDK (only works for android with companion).
example/example.dart
import 'package:flutter/material.dart';
import 'package:capture_flutter_beta/capture_flutter_beta.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Capture SDK Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Capture SDK Demo Homepage'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _status = 'starting';
String _message = '--';
List<Map> _devices = [];
String _newName = '';
dynamic _capture, _newCapture, _currentScan;
final _nameController = TextEditingController();
var logger = Logger((message, arg) => {
if (message != null && message.isNotEmpty)
{print(message + " " + arg + '\n\n')}
else
{print(arg + '\n\n')}
});
void _updateVals(String stat, String mess) {
setState(() {
_status = stat;
_message = mess;
});
}
@override
void initState() {
_nameController.addListener(_handleNameChange);
// appInfo is the credentials needed to open capture instance
// first argument is appId, then developerId, then the appKey
final appInfo = AppInfo(
'web:com.socketmobile.reactjs.native.example.example',
'bb57d8e1-f911-47ba-b510-693be162686a',
'MC0CFQC85w0MsxDng4wHBICX7+iCOiSqfAIUdSerA4/MJ2kYBGAGgIG/YHemNr8=');
Capture capture = Capture(logger);
setState(() {
// _capture will be used to represent the root capture
// root capture is used to establish and open capture connection
// subsequent capture instances can be used for
// device management and data capture
_capture = capture;
});
var stat = _status;
var mess = _message;
capture.openClient(appInfo, _onCaptureEvent).then((response) {
if (response.runtimeType == JRpcAndIosError) {
stat = response['error']['code'].toString();
mess = response['error']['message'];
} else {
var breh;
if (response.runtimeType == int) {
breh = response;
} else {
print(response);
breh = response['result']['handle'];
}
stat = 'handle: $breh';
mess = 'capture open success';
}
_updateVals(stat, mess);
});
super.initState();
}
void _handleGetNameProperty() {
// example using friendly name
// to use another property, change id and type
// to correspond to desired property
// CapturePropertyIds
var property = CaptureProperty(CapturePropertyIds().friendlyNameDevice,
CapturePropertyTypes().none, {});
var stat = 'Status -- ', mess = 'Message -- ';
_newCapture.getProperty(property).then((response) {
if (response['error'] != null) {
stat = response['error']['code'].toString();
mess = response['error']['code'] == -32602
? 'JSON parse error'
: response['error']['message'];
} else {
stat = 'Get Property';
mess = 'Successfully Retrieved "name" property for device.';
}
_updateVals(stat, mess);
});
}
void _handleSetNameProperty() {
// example using friendly name
// to use another property, change id and type
// to correspond to desired property
var property = CaptureProperty(CapturePropertyIds().friendlyNameDevice,
CapturePropertyTypes().string, _newName);
_newCapture.setProperty(property).then((response) {
var stat = 'Status -- ', mess = 'Message -- ';
if (response['error'] != null) {
stat = response['error']['code'].toString();
mess = response['error']['code'] == -32602
? 'JSON parse error'
: response['error']['message'];
} else {
stat = 'Set Property';
mess = 'Successfully set "name" property to "$_newName".';
setState(() {
_newName = '';
});
_nameController.text = '';
}
_updateVals(stat, mess);
});
}
void _handleNameChange() {
setState(() {
_newName = _nameController.text;
});
}
_onCaptureEvent(e, handle) {
if (e == null) {
return;
}
// logger is optional but can be helpul for stack tracing
logger.log('onCaptureEvent from: ', '$handle');
switch (e['id']) {
case CaptureEventIds.deviceArrival:
{
var newCapture = Capture(logger);
setState(() {
// _newCapture is used to differentiate root capture (_capture) from new instance
_newCapture = newCapture;
});
var arr = _devices;
var guid = e['value']['guid'];
var name = e['value']['name'];
logger.log('Device Arrival =>', name + ' ($guid)');
newCapture.openDevice(guid, _capture).then((res) {
if (res.runtimeType != int) {
_updateVals("${res['error']['code']}", res['error']['message']);
} else {
if (!arr.contains(e['value'])) {
arr.add(e['value']);
setState(() {
_devices = arr;
});
}
_handleGetNameProperty();
_updateVals('Device Arrival', 'Successfully added "$name"');
}
});
}
break;
case CaptureEventIds.deviceRemoval:
{
var guid = e['value']['guid'];
var name = e['value']['name'];
var arr = _devices;
arr.removeWhere((element) => element['guid'] == guid);
setState(() {
_devices = arr;
_currentScan = null;
});
logger.log('Device Removal =>', name + ' ($guid)');
_updateVals('Device Closed', 'Successfully removed "$name"');
}
break;
case CaptureEventIds.decodedData:
{
setState(() {
//storing scanned data in state for future use
_currentScan = e;
});
_updateVals('Decoded Data', "See 'Current Scan' for data.");
}
break;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(children: <Widget>[
const Text(
'Status: ',
),
Text(
_status,
style: Theme.of(context).textTheme.bodyText1,
),
]),
Row(children: <Widget>[
const Text(
'Message: ',
),
Flexible(
child: Text(
_message,
style: Theme.of(context).textTheme.bodyText1,
),
),
]),
Row(children: [
SizedBox(
width: 300,
child: Padding(
padding: const EdgeInsets.only(
left: 40,
top: 20,
right: 40,
bottom: 20,
),
child: TextField(
decoration: InputDecoration(
suffixIcon: IconButton(
icon: const Icon(Icons.add_box),
onPressed: _newName.isNotEmpty
? _handleSetNameProperty
: null,
),
labelText: 'Edit Device Name',
border: const OutlineInputBorder(),
),
controller: _nameController,
),
),
),
const Flexible(
child: Text(
'*illustrate the get/set property functionality, can be done with any device property'))
]),
Row(children: <Widget>[
const Text('Current Scan: '),
Flexible(
child: Text(
_currentScan != null
? 'scan from ' + _currentScan['value']['name']
: 'No Data',
style: Theme.of(context).textTheme.bodyText1),
)
]),
Row(children: const <Widget>[
Text(
'Devices',
),
]),
_devices.isEmpty
? const Center(child: Text('No Devices Available'))
: ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.only(left: 10, right: 10),
itemCount: _devices.length,
itemBuilder: (BuildContext context, int index) {
return Text('${index + 1}. ' +
_devices[index]['name'] +
': ' +
_devices[index]['guid']);
},
),
],
),
));
}
}