I’m engaged on a Flutter app that makes use of the flutter_blue_plus package deal to scan and connect with BLE gadgets. I’m dealing with a problem the place I am making an attempt to deal with the system scanning course of and permit the consumer to pick a tool. Nonetheless, I’m getting an error with how I am utilizing FlutterBluePlus.occasion.isScanning and dealing with system choice.
Under is the related code for the Bluetooth system scanning and choice course of:
import 'package deal:flutter/companies.dart';
import 'package deal:flutter_blue_plus/flutter_blue_plus.dart';
import 'package deal:whizpad_lan_flutter/blocs/add_device_bloc.dart';
import 'package deal:supplier/supplier.dart';
import 'package deal:whizpad_lan_flutter/blocs/ble/device_discovery_bloc.dart';
import 'package deal:whizpad_lan_flutter/utils/values.dart';
import 'package deal:whizpad_lan_flutter/widgets/bottom_sheet_background.dart';
import 'package deal:whizpad_lan_flutter/widgets/one_line_text.dart';
import 'package deal:whizpad_lan_flutter/fashions/device_model.dart';
import 'package deal:flutter_gen/gen_l10n/app_localizations.dart';
import 'button_add_device.dart';
class InputDeviceMacBottomSheet extends StatefulWidget {
const InputDeviceMacBottomSheet({
Key? key,
required this.screenSize,
required this.parentContext,
required this.deviceType,
}) : tremendous(key: key);
closing Measurement screenSize;
closing BuildContext parentContext;
closing DeviceType? deviceType;
@override
_InputDeviceMacBottomSheetState createState() =>
_InputDeviceMacBottomSheetState();
}
class _InputDeviceMacBottomSheetState extends State {
TextEditingController textEditingController = TextEditingController();
late AddDeviceBloc addDeviceBloc;
@override
void initState() {
tremendous.initState();
}
@override
Widget construct(BuildContext context) {
addDeviceBloc = Supplier.of(context);
return StreamBuilder(
stream: addDeviceBloc.selectedDevice,
builder: (context, selectedDevice) {
return StreamBuilder(
stream: addDeviceBloc.selectedDeviceId,
builder: (context, selectedDeviceMac) {
if (selectedDeviceMac.hasData == false) {
return SizedBox();
}
textEditingController.textual content = selectedDeviceMac.information!;
textEditingController.choice = TextSelection.collapsed(
offset: textEditingController.textual content.size);
return MyBottomSheetBackground(
screenSize: widget.screenSize,
title: AppLocalizations.of(context)!.enterDeviceMacPage,
subtitle: AppLocalizations.of(context)!.enterDeviceMac,
youngster: Column(
youngsters: [
Container(
color: Colors.white,
child: Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 24.0),
child: Text(AppLocalizations.of(context)!.deviceMac,
style: textStyle45pt),
),
Expanded(
child: TextFieldContainer(
child: TextField(
controller: textEditingController,
onEditingComplete: () {
addDeviceBloc.updateSelectedMac(
textEditingController.text);
FocusScope.of(context).unfocus();
},
cursorColor: primaryBlackColor,
decoration: InputDecoration(
hintText: AppLocalizations.of(context)!.enterMac,
border: InputBorder.none,
hintStyle: textStyle30ptGrey,
),
textCapitalization:
TextCapitalization.characters,
inputFormatters: [
FilteringTextInputFormatter.allow(inputMacRegExp),
LengthLimitingTextInputFormatter(12),
],
),
)),
],
),
),
StreamBuilder(
stream: FlutterBluePlus.occasion.isScanning,
builder: (context, isScanning) {
return Middle(
youngster: Padding(
padding: const EdgeInsets.all(16.0),
youngster: Row(
mainAxisSize: MainAxisSize.min,
youngsters: [
Text(AppLocalizations.of(context)!.anyDeviceMac,
style: textStyleBold),
if (isScanning.hasData && isScanning.data != null)
Container(
width: 16.0,
height: 16.0,
margin: EdgeInsets.only(left: 8.0),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(
Colors.grey,
),
strokeWidth: 2,
),
),
],
),
),
);
}),
Expanded(
youngster: StreamBuilder>(
stream: Supplier.of(
widget.parentContext)
.targetDevices(widget.deviceType),
builder: (context, snapshot) {
if (snapshot.information == null) {
return Container();
}
return ListView.separated(
separatorBuilder: (context, index) {
return Divider(
peak: 2.0,
);
},
itemCount: snapshot.information!.size + 1,
itemBuilder: (context, index) {
if (index == snapshot.information!.size) {
return StreamBuilder(
stream:
FlutterBluePlus.occasion.isScanning,
builder: (context, isScanning) {
if (isScanning.information == null ||
isScanning.information == true) {
return SizedBox();
}
return Padding(
padding:
const EdgeInsets.solely(prime: 8.0),
youngster: Row(
mainAxisAlignment:
MainAxisAlignment.heart,
youngsters: [
Text(AppLocalizations.of(context)!
.bottonDeviceNotFound),
GestureDetector(
child: Text(
AppLocalizations.of(context)!
.refresh,
style: TextStyle(
color: Colors.blue,
decoration:
TextDecoration.underline),
),
onTap: () {
FlutterBluePlus.instance
.startScan(
timeout: Duration(
seconds: 10));
},
)
],
),
);
});
}
String macStr="";
closing Gadget? system = snapshot.information![index];
macStr = system?.getDecodedAdvServiceData();
return ListTile(
onTap: () {
Supplier.of(context, pay attention: false)
.selectDevice(snapshot.information![index]!);
},
tileColor: Colours.white,
title: Align(
alignment: Alignment(-1, 0),
youngster: OneLineText("$macStr")),
trailing: selectedDevice.information?.deviceId ==
snapshot.information![index]?.deviceId
? Icon(Icons.check_circle_sharp, coloration: Colours.inexperienced)
: SizedBox(),
);
},
);
},
),
)
],
),
bottomButton: MyButton(
title: AppLocalizations.of(context)!.add,
onPress: (textEditingController.textual content == "" ||
textEditingController.textual content.size < 12)
? null
: () {
Navigator.pop(context, textEditingController.textual content);
},
),
);
});
});
}
}```
Difficulty:
I'm utilizing flutter_blue_plus to scan BLE gadgets and permit customers to pick them. I am getting points in dealing with the scanning stream with FlutterBluePlus.occasion.isScanning, and in addition I'm not certain if I am dealing with the system choice appropriately.
I've a StreamBuilder for isScanning, however I am undecided if I am utilizing the right API for dealing with the scanning standing or if I ought to use isScanningStream. Moreover, I need assistance confirming that I'm dealing with the system choice course of appropriately by extracting the mandatory particulars from the scan end result.
Questions:
Is FlutterBluePlus.occasion.isScanning the fitting API to test for ongoing scanning, or ought to I be utilizing isScanningStream?
How ought to I deal with the system choice course of correctly utilizing flutter_blue_plus, particularly for acquiring the system's MAC deal with and passing it to my AddDeviceBloc?