我试图编写代码来检测信标,但我遇到了错误,无法解决。

huangapple go评论71阅读模式
英文:

i am try to write code to detect beacons but i am getting error and not able to solve

问题

你的代码似乎用于检测iBeacons,但在运行时遇到了一些问题。以下是你的问题描述以及一些建议:

  1. 权限问题:你已经提到将权限添加到AndroidManifest.xml文件中,这是正确的。但是请确保你的应用程序在设备上确实具有位置权限。在Android模拟器上测试时,通常需要手动授予权限。确保在模拟器设置中授予应用程序位置权限。

  2. 虚拟设备选择:不同的Android模拟器可能会有不同的行为,特别是与硬件相关的功能,如蓝牙。你可能需要尝试不同的虚拟设备来查看哪个设备能够成功运行你的应用。

  3. 错误信息:根据你提供的截图,你的应用似乎遇到了一个错误。错误信息可能会提供有关问题的更多信息。请提供完整的错误信息,以便更容易诊断问题。

  4. 检查依赖:确保你的Flutter应用程序已正确配置flutter_beacon和permission_handler插件。在pubspec.yaml文件中检查依赖项的版本和配置是否正确。

  5. 尝试真机:在某些情况下,模拟器可能无法完全模拟设备上的硬件行为。如果可能的话,尝试在实际的Android设备上运行你的应用程序,以查看它是否正常工作。

根据你的描述,问题可能与模拟器有关,因此尝试在实际设备上运行你的应用程序可能会更有帮助。另外,请确保你的代码没有其他明显的错误,如拼写错误或语法问题。

英文:

so I have 4 beacons and I wrote the code below to detect them

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_beacon/flutter_beacon.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StreamSubscription<RangingResult>? _streamRanging;
  final _regionBeacons = <Region, List<Beacon>>{};
  final _beacons = <Beacon>[];
  @override
  void initState() {
    super.initState();

    // Request location permission
    requestPermission();
  }

  void requestPermission() async {
    if (await Permission.location.request().isGranted) {
      // Location permission granted
      initBeacon();
    } else {
      // Location permission denied
      showDialog(
        context: context,
        builder: (_) => AlertDialog(
          title: Text('Location Permission Required'),
          content: Text(
              'Please grant location permission to enable beacon scanning.'),
          actions: [
            TextButton(
              child: Text('OK'),
              onPressed: () {
                Navigator.pop(context);
                // Open app settings to allow the user to grant permission manually
                openAppSettings();
              },
            ),
          ],
        ),
      );
    }
  }

  // FlutterBeacon.initialize();
  initBeacon() async {
    try {
      await flutterBeacon.initializeScanning;
      print('Beacon scanner initialized');
    } on PlatformException catch (e) {
      print(e);
    }
    // Configure the regions to scan for iBeacons
    final regions = <Region>[];
    if (Platform.isIOS) {
      regions.add(Region(
          identifier: 'MyBeacon',
          proximityUUID: 'a86be991-76c7-4fc4-892a-551acf07c77c',
          major: null,
          minor: null));
    } else {
      regions.add(Region(identifier: 'com.beacon'));
    }
    flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
      // result contains a region, event type and event state
    });
    // Start ranging for beacons
    _streamRanging =
        flutterBeacon.ranging(regions).listen((RangingResult result) {
      if (result != null && result.beacons.isNotEmpty) {
        // Get a list of beacons in range
        List<Beacon> beacons = result.beacons;

        // Iterate through beacons and get their data
        for (var beacon in beacons) {
          String uuid = beacon.proximityUUID;
          int major = beacon.major;
          int minor = beacon.minor;
          int rssi = beacon.rssi;
          // Do something with beacon data
          print(
              'Beacon detected: UUID=$uuid, major=$major, minor=$minor, rssi=$rssi');
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    // Stop ranging for beacons
    _streamRanging?.cancel();
    flutterBeacon.close;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'iBeacon Detection',
      home: Scaffold(
        appBar: AppBar(
          title: Text('iBeacon Detection'),
        ),
        body: Center(
          child: Text('Searching for iBeacons...'),
        ),
      ),
    );
  }
}

but the issue when i run the code it show me this
我试图编写代码来检测信标,但我遇到了错误,无法解决。

while using this emulator
我试图编写代码来检测信标,但我遇到了错误,无法解决。
so I changed the emulator to
我试图编写代码来检测信标,但我遇到了错误,无法解决。
and now it shows me this error
我试图编写代码来检测信标,但我遇到了错误,无法解决。
我试图编写代码来检测信标,但我遇到了错误,无法解决。

it keeps showing me the same message until I stop the application
I have add the permission to AndroidManifest.xml file
How i can solve this and detect my beacons? I am fully new to this so I got stuck and cannot proceed further. I am using VS and an emulator from android studio

答案1

得分: 2

  1. 无法使用 Android 模拟器检测信标或执行任何其他蓝牙操作。尽管您的工作站可能支持蓝牙低功耗(Bluetooth LE),但 Android 模拟器不会将工作站上的蓝牙硬件与模拟的 Android 设备连接。模拟器的行为就像没有蓝牙低功耗功能的手机一样。

  2. 仅因为 LogCat 中显示了日志行并不意味着它是错误。许多消息是无害的,不表示问题。这在您提供的第一个日志摘录中是正确的。

  3. 对于您提供的第二个日志摘录,安全异常(Security Exception)确实表示存在问题 - 如果在 Android 12 或更高版本上运行,您的代码必须获得用户的 BLUETOOTH_SCAN 权限才能扫描信标。

最重要的是:您确实需要使用实际的 Android 手机来测试应用程序与信标的功能,而不要依赖模拟器。Flutter Beacon 库的底层使用了 Android Beacon Library,该库允许您在模拟器开发中模拟信标,具体操作说明请参阅 此处。然而,这仅适用于直接使用原生库的情况,我不认为 Flutter Beacon 提供了与此信标模拟器相关的 Flutter 绑定,因此只能在编写原生代码时使用它。

英文:

A few points:

  1. You cannot detect beacons or do any other bluetooth operations using an Android emulator. Even though the workstation you are using likely supports Bluetooth LE, Android emulators to not bridge the bluetooth hardware on the workstation to the emulated Android device. The emulator acts like a phone without Bluetooth LE.

  2. Just because a log line shows up in LogCat doesn't mean it is an error. Lots of messages are benign and do not indicate a problem. This is true in your first log excerpt.

  3. For your second log excerpt, the Security Exception does indicate a problem -- if you run on Android 12+ your code must get BLUETOOTH_SCAN permission from the user to be able to scan for beacons.

Bottom line: you really need to get a physical Android phone for testing beacons with your app and not rely on the emulator. The underlying Android Beacon Library that flutter beacon uses under the hood does allow you to simulate beacons for emulator development as described here. However, that is for direct use of the native library, and I do not believe flutter beacon provides any flutter bindings to this beacon simulator, so you can only use it if you are writing native code.

huangapple
  • 本文由 发表于 2023年3月15日 18:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定