quick_usb 0.1.0+2 quick_usb: ^0.1.0+2 copied to clipboard
A cross-platform USB plugin for Flutter
import 'dart:convert';
import 'dart:typed_data';
import 'package:convert/convert.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:quick_usb/quick_usb.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState 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: _buildColumn(),
),
);
}
Widget _buildColumn() {
return Column(
children: [
_init_exit(),
_getDeviceList(),
_has_request(),
_open_close(),
_get_set_configuration(),
_claim_release_interface(),
_bulk_transfer(),
],
);
}
Widget _init_exit() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('init'),
onPressed: () async {
var init = await QuickUsb.init();
print('init $init');
},
),
RaisedButton(
child: Text('exit'),
onPressed: () async {
await QuickUsb.exit();
print('exit');
},
),
],
);
}
List<UsbDevice> _deviceList;
Widget _getDeviceList() {
return RaisedButton(
child: Text('getDeviceList'),
onPressed: () async {
_deviceList = await QuickUsb.getDeviceList();
print('deviceList $_deviceList');
},
);
}
Widget _has_request() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('hasPermission'),
onPressed: () async {
var hasPermission = await QuickUsb.hasPermission(_deviceList.first);
print('hasPermission $hasPermission');
},
),
RaisedButton(
child: Text('requestPermission'),
onPressed: () async {
await QuickUsb.requestPermission(_deviceList.first);
print('requestPermission');
},
),
],
);
}
Widget _open_close() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('openDevice'),
onPressed: () async {
var openDevice = await QuickUsb.openDevice(_deviceList
.firstWhere((element) => element.vendorId == 0x1203));
print('openDevice $openDevice');
},
),
RaisedButton(
child: Text('closeDevice'),
onPressed: () async {
await QuickUsb.closeDevice();
print('closeDevice');
},
),
],
);
}
UsbConfiguration _configuration;
Widget _get_set_configuration() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('getConfiguration'),
onPressed: () async {
_configuration = await QuickUsb.getConfiguration(0);
print('getConfiguration $_configuration');
},
),
RaisedButton(
child: Text('setConfiguration'),
onPressed: () async {
var setConfiguration =
await QuickUsb.setConfiguration(_configuration);
print('setConfiguration $setConfiguration');
},
),
],
);
}
Widget _claim_release_interface() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('claimInterface'),
onPressed: () async {
var claimInterface =
await QuickUsb.claimInterface(_configuration.interfaces[0]);
print('claimInterface $claimInterface');
},
),
RaisedButton(
child: Text('releaseInterface'),
onPressed: () async {
var releaseInterface =
await QuickUsb.releaseInterface(_configuration.interfaces[0]);
print('releaseInterface $releaseInterface');
},
),
],
);
}
UsbEndpoint _endpointOut;
UsbEndpoint get endpointOut => _endpointOut;
Widget _bulk_transfer() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('bulkTransferIn'),
onPressed: () async {
var data = Uint8List.fromList([27, 33, 83]);
var endpointOut = _configuration.interfaces[0].endpoints
.firstWhere((e) => e.direction == UsbEndpoint.DIRECTION_OUT);
var bulkTransferOut =
await QuickUsb.bulkTransferOut(endpointOut, data);
print('bulkTransferOut $bulkTransferOut');
await Future.delayed(Duration(milliseconds: 150));
var endpointIn = _configuration.interfaces[0].endpoints
.firstWhere((e) => e.direction == UsbEndpoint.DIRECTION_IN);
var bulkTransferIn =
await QuickUsb.bulkTransferIn(endpointIn, 1024);
print('bulkTransferIn ${hex.encode(bulkTransferIn)}');
},
),
RaisedButton(
child: Text('bulkTransferOut'),
onPressed: () async {
_endpointOut = _configuration.interfaces[0].endpoints
.firstWhere((e) => e.direction == UsbEndpoint.DIRECTION_OUT);
var width = 1100;
var height = 788;
ByteData imageBytes = await rootBundle.load('images/b1.bmp');
var imageData = imageBytes.buffer.asUint8List();
await send(Uint8List.fromList(
utf8.encode('DOWNLOAD F,"TEST.BMP",${imageData.length},')));
await send(imageData);
await send(Uint8List.fromList(
utf8.encode('SIZE $width dot,${height + 50} dot\n')));
await send(Uint8List.fromList(utf8.encode('CLS\n')));
await send(
Uint8List.fromList(utf8.encode('PUTBMP 0,50,"TEST.BMP"\n')));
await send(Uint8List.fromList(utf8.encode('EOJ\n')));
await send(Uint8List.fromList(utf8.encode('PRINT 1\n')));
await send(Uint8List.fromList(utf8.encode('EOP\n')));
},
),
],
);
}
Future send(Uint8List data) async {
var bulkTransferOut = await QuickUsb.bulkTransferOut(endpointOut, data);
print('bulkTransferOut $bulkTransferOut');
}
}