我一直试图在我的flutter应用程序中集成fluter_blue_plus插件,但在扫描可用设备时,我得到了一个空列表,没有任何可用设备可供配对。
在这里,我附上截图和日志相同,以通过。我在这里被击中了你能帮我解决这个问题吗?
控制器类文件:
`import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
class BluetoothController extends GetxController {
FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
Future scanDevices() async {
flutterBlue.startScan(timeout: const Duration(seconds: 5));
print("_________________________________________________________");
print(flutterBlue.scanResults);
print("_________________________________________________________");
flutterBlue.stopScan();
}
Stream<List<ScanResult>> get scanResults => flutterBlue.scanResults;
}`
我调用控制器扫描并在列表视图中显示可用设备的小部件:
`Widget _buildPopupDialog(BuildContext context) {
return new AlertDialog(
title: const Text('List of available devices'),
content: GetBuilder<BluetoothController>(
init: BluetoothController(),
builder: (controller) {
return Container(
height: MediaQuery.of(context).size.height/1.3,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
GestureDetector(
onTap: () {
controller.scanDevices();
},
child: Container(
height: MediaQuery.of(context).size.height / 20,
width: MediaQuery.of(context).size.width / 3,
decoration: BoxDecoration(
color: Colors.blueAccent,
border: Border.all(
color: Colors.black,
width: 1,
),
borderRadius: BorderRadius.circular(10.0),
//gradient: LinearGradient(
//colors: [Colors.indigo, Colors.blueAccent]),
boxShadow: const [
BoxShadow(
color: Colors.white,
blurRadius: 1.0,
offset: Offset(1.0, 1.0))
],
),
child: Center(
child: Text(
"Scan Devices!!!",
style: TextStyle(
fontSize: MediaQuery.of(context).size.width/40,
letterSpacing: 1,
fontWeight: FontWeight.w400,
color: Colors.black),
),
),
),
),
SizedBox(
height: 10,
),
StreamBuilder<List<ScanResult>>(
stream: controller.scanResults,
builder: (context, snapshot){
if(snapshot.hasData)
{
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.name,
selectionColor: Colors.black,),
subtitle: Text(data.device.id.id),
trailing: Text(data.rssi.toString()),
),
);
});
}
else
{
return const Center(
child: Text("No devices found!!!"),
);
}
}
)
],
),
),
);
}),
actions: <Widget>[
new GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: const Text('Close'),
),
],
);
}`
Log output when i call the function:
/ViewRootImpl@7b87c05[MainActivity](13581): ViewPostIme pointer 0
I/ViewRootImpl@7b87c05[MainActivity](13581): ViewPostIme pointer 1
I/flutter (13581): _________________________________________________________
I/flutter (13581): Instance of '_BroadcastStream<List<ScanResult>>'
I/flutter (13581): _________________________________________________________
I/BluetoothAdapter(13581): STATE_ON
I/BluetoothAdapter(13581): STATE_ON
D/BluetoothLeScanner(13581): could not find callback wrapper
I/BluetoothAdapter(13581): STATE_ON
I/BluetoothAdapter(13581): STATE_ON
I/BluetoothAdapter(13581): STATE_ON
I/BluetoothAdapter(13581): STATE_ON
D/BluetoothLeScanner(13581): Start Scan with callback
D/BluetoothLeScanner(13581): onScannerRegistered() - status=0 scannerId=11 mScannerId=0
I/BluetoothAdapter(13581): STATE_ON
I/BluetoothAdapter(13581): STATE_ON
D/BluetoothLeScanner(13581): Stop Scan with callback
我的Android清单文件:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
我试过在我的android手机的设置中给应用程序所有的权限,它运行的是android版本13和一个ui版本5.1。
但仍不会在列表视图中显示扫描的设备。
我已经在手机上为移动的应用程序提供了所有的位置权限。
下面附上空单的移动的UI截图:enter image description here
1条答案
按热度按时间f1tvaqid1#
看看你的代码,你似乎从来没有真正监听过扫描结果。flutter_blue_plus的usage section显示了一个示例:
这可能已经解决了你的问题。