Hello everyone and thank you to read this.
So I have a script that configure bluetooth to my raspberry pi 4 and make him discoverable.
In the other hand I have a flutter app that detect bluetooth devices and show them in a list.
ISSUE :
The flutter app doesn't detect my raspberry but detect all the others devices (bluetooth speaker, headphones...).
When I go in the native scan bluetooth of my android phone - the raspberry is detected and I can connect to him.
Someone have a guess why the app doesn't detect the raspberry ?
Thank you
This is the code from the raspberry script :
...
# 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 "'$cmd' "
sendCmd "$cmd"
echo 'done.'
done
echo -e "[CONFIGURATION] ended
"
...
This is the code from the flutter app :
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()),
);
},
),
),
]),
),
);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…