大家好,感谢您阅读本文。
所以我有一个脚本可以为我的 raspberry pi 4 配置蓝牙并使他可以被发现。
另一方面,我有一个 Flutter 应用程序,可以检测蓝牙设备并将它们显示在列表中。
问题: flutter 应用程序不会检测到我的树莓,但会检测所有其他设备(蓝牙扬声器、耳机......)。 当我进入 Android 手机的本机扫描蓝牙时 - 检测到树莓并且我可以连接到他。
有人猜测为什么应用没有检测到树莓?
谢谢
这是树莓派脚本中的代码:
...
# Coproc functions
function sendCmd() {
echo "$1" >&"${bluetooth[1]}"
read -r clean <&"${bluetooth[0]}"
while [[ ! -z $clean ]]
do
read -r -t 1 clean <&"${bluetooth[0]}"
done
}
# All commands for bluetoothctl
declare -a commands=("power on" "pairable on" 'discoverable on' 'agent on' 'default-agent')
# Start Coproc with bluetoothclt binding the process with 'bluetooth' name
echo -e "[CONFIGURATION] started"
coproc bluetooth {
bluetoothctl;
}
for cmd in "${commands[@]}"
do
echo -ne "\t'$cmd' "
sendCmd "$cmd"
echo 'done.'
done
echo -e "[CONFIGURATION] ended\n"
...
这是flutter应用程序中的代码:
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
void main() {
runApp(BluetoothApp());
}
class BluetoothApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BluetoothApp',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home() : super();
HomeState createState() => HomeState();
}
class HomeState extends State<Home> {
FlutterBlue flutterBlue = FlutterBlue.instance;
List<BluetoothDevice> devices = List<BluetoothDevice>();
@override
void initState() {
super.initState();
_scanForDevices();
}
void _scanForDevices() async {
print('Scanning...');
flutterBlue.scan().listen((event) {
if (!devices.contains(event.device)) {
setState(() {
devices.add(event.device);
});
}
}).onError((err) {
print('Error => ' + err.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(children: [
Expanded(
child: ListView.builder(
itemCount: devices.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(devices[index].name.length > 0
? devices[index].name
: '[no name]'),
subtitle:
Text('Mac address : ' + devices[index].id.toString()),
);
},
),
),
]),
),
);
}
}
答案 0 :(得分:1)
就像@DimaRostopira 所说的,是我使用的图书馆问题。
所以我找到了一个与 raspberry 一起使用的新 flutter 库(基本上与 RFCOMM 一起使用):flutter_bluetooth_serial