flutter_blue_plus is not showing devices after scanning
I have a simple flutter application , i want to scan ble devices and connect to them , the problem is that the scanResults always empty , below is my code for the controller and the view
Controller
import 'dart:async';
import 'dart:io';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
class BleConnexionController extends GetxController {
//TODO: Implement BleConnexionController
late BluetoothDevice connectedDevice;
Stream<List<ScanResult>> get scanResults => FlutterBluePlus.scanResults;
@override
void onInit() {
super.onInit();
initializeBluetooth();
}
@override
void onReady() {
super.onReady();
}
@override
void onClose() {
super.onClose();
}
Future<void> initializeBluetooth() async {
// Request Bluetooth permissions
final status = await Permission.bluetooth.status;
print('the status $status');
if (status.isGranted) {
// Bluetooth permission is granted, now turn on Bluetooth
// Check if Bluetooth is available on the device
if (await FlutterBluePlus.isAvailable) {
// Check if Bluetooth is already on
final state = await FlutterBluePlus.adapterState.first;
if (state == BluetoothAdapterState.off) {
if (Platform.isAndroid) {
try{
await FlutterBluePlus.turnOn();
}catch(e){
print(e);
}
}
}
// You can now use FlutterBlue to work with Bluetooth devices
// Example: List<BluetoothDevice> devices = await flutterBlue.connectedDevices;
} else {
// Bluetooth is not available on this device
print('Bluetooth is not available on this device.');
}
} else {
// Permission not granted
print('Bluetooth permission not granted.');
}
}
var subscription = FlutterBluePlus.scanResults.listen(
(results) {
for (ScanResult r in results) {
print('${r.device.remoteId}: "${r.device.localName}" found! rssi: ${r.rssi}');
}
},
onError: (e) => print(e)
);
Future scan() async {
List<BluetoothDevice> devices = await FlutterBluePlus.connectedSystemDevices;
// Start scanning
await FlutterBluePlus.startScan(timeout: const Duration(milliseconds: 100), androidUsesFineLocation: false); // Stop scanning
await FlutterBluePlus.stopScan();
}
}
the scan function is called in the view when pressing scan button
the view
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import '../controllers/ble_connexion_controller.dart';
class BleConnexionView extends GetView<BleConnexionController> {
const BleConnexionView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BleConnexionView'),
centerTitle: true,
),
body: GetBuilder<BleConnexionController>(
init: BleConnexionController(),
builder: (controller) {
return SingleChildScrollView(
child: Column(children: [
const SizedBox(height: 20,),
Center(
child: ElevatedButton(onPressed: () => controller.scan(),
child: Text("Scan"),),
),
StreamBuilder<List<ScanResult>>(
stream: controller.scanResults,
builder: (context,snapshot){
if (snapshot.data != null && snapshot.data!.isNotEmpty){
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context,index){
final data = snapshot.data![index];
return Card(
elevation: 2,
child: ListTile(
title: Text(data.device.localName),
subtitle: Text(data.device.id.id),
trailing: ElevatedButton(
onPressed: () async {
controller.connectedDevice=data.device;
//controller.connectDevice().then((value) => Get.off(const HomePage()));
},
child: Text("Connect"),
),
),
);
},
);
}else{
return const Center(child: Text("No devices found"),);
}
})
]),
);
},),
);}
}
i tried many flutter versions (3.13.3 3.13.2 3.10.6 (active) 3.10.0 ) the scanResults builder is always empty , and no device is detected , can someone help ?
Source: View source