Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

android - Flutter bluetooth scan - Not detecting raspberry

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()),
                );
              },
            ),
          ),
        ]),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like @DimaRostopira said is an issue from the library that I used.

So i find a new flutter library that work with raspberry (basically with RFCOMM) : flutter_bluetooth_serial


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...