import 'package:flutter/material.dart'; import 'package:flutter_blue/flutter_blue.dart'; /// scan Results class ScanResultTile extends StatelessWidget { final ScanResult result; final VoidCallback onTap; final String onlyDeviceToShow; const ScanResultTile( {Key key, this.result, this.onlyDeviceToShow, this.onTap}) : super(key: key); Widget _buildTitle(BuildContext context) { if (result.device.name.length > 0) { return Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(result.device.name), Text( result.device.id.toString(), style: Theme.of(context).textTheme.caption, ) ], ); } else { return Text(result.device.id.toString()); } } String getNiceHexArray(List bytes) { return '[${bytes.map((i) => i.toRadixString(16).padLeft(2, '0')).join(', ')}]' .toUpperCase(); } String getNiceManufacturerData(Map> data) { if (data.isEmpty) { return null; } List res = []; data.forEach((id, bytes) { res.add( '${id.toRadixString(16).toUpperCase()}: ${getNiceHexArray(bytes)}'); }); return res.join(', '); } String getNiceServiceData(Map> data) { if (data.isEmpty) { return null; } List res = []; data.forEach((id, bytes) { res.add('${id.toUpperCase()}: ${getNiceHexArray(bytes)}'); }); return res.join(', '); } @override Widget build(BuildContext context) { // Test if device = TouchDemonstrator if (result.advertisementData.localName == onlyDeviceToShow) { return Container( // decoration: FlutterLogoDecoration(), child: Padding( padding: const EdgeInsets.all(8.0), child: new Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _buildTitle(context), // Name of devices RaisedButton( child: Text('CONNECT'), color: Colors.black, textColor: Colors.white, onPressed: (result.advertisementData.connectable) ? onTap : null, ), ], ), ), ); } else { return Container(); } } } /// Service tiles class ServiceTile extends StatelessWidget { final BluetoothService service; final List characteristicTiles; final String onlyServiceToShow; const ServiceTile( {Key key, this.service, this.characteristicTiles, this.onlyServiceToShow}) : super(key: key); @override Widget build(BuildContext context) { if (characteristicTiles.length > 0) { if (service.uuid.toString().toUpperCase().substring(4, 8) == onlyServiceToShow) { return new ExpansionTile( initiallyExpanded: true, title: new Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Service (BLEUart)'), new Text( 'UUID: 0x${service.uuid.toString().toUpperCase().substring(4, 8)}', style: Theme.of(context).textTheme.body1.copyWith( color: Theme.of(context).textTheme.caption.color)) ], ), /// characteristic tiles children: characteristicTiles, ); } else { return Container(); //Text("Service not BLEUart"); } } else { return new ListTile( title: const Text('Service'), subtitle: new Text( '0x${service.uuid.toString().toUpperCase().substring(4, 8)}'), ); } } } /// characteristic tile class CharacteristicTile extends StatelessWidget { final BluetoothCharacteristic characteristic; final List descriptorTiles; //final VoidCallback onReadPressed; // final VoidCallback onWritePressed; final VoidCallback onNotificationPressed; final String onlyCharacteristicToListen; const CharacteristicTile( {Key key, this.characteristic, this.descriptorTiles, //this.onReadPressed, // this.onWritePressed, this.onNotificationPressed, @required this.onlyCharacteristicToListen}) : super(key: key); @override Widget build(BuildContext context) { if(onlyCharacteristicToListen == characteristic.uuid.toString().toUpperCase().substring(4,8)){ print("found ${characteristic.uuid.toString().toUpperCase().substring(4,8)} /// $onlyCharacteristicToListen"); } /// title var title = new Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('CharacteristicTile title'), new Text( '0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}', style: Theme.of(context) .textTheme .body1 .copyWith(color: Theme.of(context).textTheme.caption.color)) ], ); /// subscribe var actions = new Row( mainAxisSize: MainAxisSize.min, children: [ new IconButton( icon: new Icon( characteristic.isNotifying ? Icons.sync_disabled : Icons.sync, color: Theme.of(context).iconTheme.color.withOpacity(0.5)), onPressed: onNotificationPressed, ) ], ); return new ListTile( title: title, subtitle: new Text(characteristic.value.toString()), trailing: actions, ); //onNotificationPressed(); /*if (descriptorTiles.length > 0) { return new ExpansionTile( title: new ListTile( title: title, subtitle: new Text(characteristic.value.toString()), contentPadding: EdgeInsets.all(0.0), ), trailing: actions, children: descriptorTiles, ); } else {*/ } } class DescriptorTile extends StatelessWidget { final BluetoothDescriptor descriptor; /* final VoidCallback onReadPressed; final VoidCallback onWritePressed;*/ const DescriptorTile( {Key key, this.descriptor}) //, this.onReadPressed, this.onWritePressed}) : super(key: key); @override Widget build(BuildContext context) { var title = new Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Descriptor'), new Text( '0x${descriptor.uuid.toString().toUpperCase().substring(4, 8)}', style: Theme.of(context) .textTheme .body1 .copyWith(color: Theme.of(context).textTheme.caption.color)) ], ); return new ListTile( title: title, subtitle: new Text(descriptor.value.toString()), /* trailing: new Row( mainAxisSize: MainAxisSize.min, children: [ new IconButton( icon: new Icon( Icons.file_download, color: Theme.of(context).iconTheme.color.withOpacity(0.5), ), onPressed: null,//onReadPressed, ), new IconButton( icon: new Icon( Icons.file_upload, color: Theme.of(context).iconTheme.color.withOpacity(0.5), ), onPressed: null,//onWritePressed, ) ], ),*/ ); } }