【问题标题】:Flutter bluetooth scan - Not detecting raspberry颤振蓝牙扫描 - 未检测到覆盆子
【发布时间】:2021-04-16 11:02:07
【问题描述】:

大家好,感谢您阅读本文。

所以我有一个脚本可以为我的树莓派 4 配置蓝牙并让他被发现。

另一方面,我有一个颤振应用程序,可以检测蓝牙设备并将它们显示在列表中。

问题: Flutter 应用程序不会检测到我的树莓派,但会检测到所有其他设备(蓝牙扬声器、耳机......)。 当我进入我的安卓手机的本机扫描蓝牙时 - 覆盆子被检测到,我可以连接到他。

有人猜猜为什么应用没有检测到树莓派?

谢谢

这是来自覆盆子脚本的代码:

...

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

【问题讨论】:

  • 你应该在这个库的 github repo 上填写一个问题
  • @DimaRostopira 谢谢!你说得对,是图书馆的一个问题(不是唯一一个)。我会在他们的 GitHub 上发布一个问题 :) 对于可能感兴趣的人,我刚刚找到了一个兼容的:flutter_bluetooth_serial

标签: android ios flutter bluetooth raspberry-pi


【解决方案1】:

就像@DimaRostopira 所说的,这是我使用的图书馆的问题

所以我找到了一个可以与树莓派一起使用的新颤振库(基本上与 RFCOMM 一起使用):flutter_bluetooth_serial

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2022-06-13
    • 2015-03-03
    • 1970-01-01
    • 2021-01-03
    • 2018-03-25
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多