有没有一种方法可以实现像信标一样的蓝牙广播? [英] Is there a method to implement in flutter a sort of bluetooth broadcast like beacons?

查看:231
本文介绍了有没有一种方法可以实现像信标一样的蓝牙广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为Flutter应用程序实现某种蓝牙广播"(使用低功耗蓝牙). 我特别希望达到以下几点:

I would like to know how to realize a sort of "bluetooth broadcast" (using Bluetooth Low Energy) for a Flutter application. In particular, i want to achieve those points:

  • 我想使用蓝牙在空中"发送随机字符串和整数.
  • 我还希望能够通过其他设备接收那些数据
  • 该应用程序必须能够同时发送和接收

现在,我检查了pub.dev中的一些信标库(例如),但事实证明,我可能不了解这些库是否以及如何帮助我.

Now, i checked some beacon library from pub.dev (like this or this) but it turns out that probably i did not understand if and how those libraries can help me.

例如,让我们考虑 beacon_broadcast 库:

建议的创建信标的代码如下:

The suggested code to make a beacon is the following:

beaconBroadcast
  .setUUID('39ED98FF-2900-441A-802F-9C398FC199D2')
  .setMajorId(1)
  .setMinorId(100)
  .setTransmissionPower(-59) //optional
  .setIdentifier('com.example.myDeviceRegion') //iOS-only, optional
  .setLayout('s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-21v') //Android-only, optional
  .setManufacturerId(0x001D) //Android-only, optional
  .start();

但是我有点困惑.我想了解如何发送自定义数据(一个简单的字符串和一个更简单的整数). 在接收方,我想获取具有特定布局的所有信标,而不仅仅是与特定UUID匹配的信标.

However i'm a little bit confused. I would like to understand how can i send custom data (a simple string and a simpler integer). On the receiver side, i would like to get all beacons with a specific layout, not only beacons matching a specific UUID.

可以找到与我想要实现的目标类似的事物

Something similar to what I want to achieve can be found here but it's made in kotlin. If you consider this as the nearest thing to what i need, please suggest me how can i use native code in my flutter project.

非常感谢您.

现在,我们需要找到两个Flutter库,以便同时发送和接收带有AltBeacon布局的所有信标.这并非微不足道,因为事实证明其他开发人员奇怪地实现了通信的一面,而不是两者都实现了.而且,很难找到带有包含电池"的库(即可以开箱即用地互相交谈).

Edit 1: we now need to find two Flutter libraries in order to simultaneously send and receive all kind of beacons with the AltBeacon layout. This is not trivial, because it turns out that other devs have strangely implemented one side of the communication and not both. Moreover, it's difficult to find libraries with "batteries included" (i.e able to talk each other out-of-the-box).

最新消息!我结合了两个flutter库,特别是 flutter_beacon beacon_broadcast .现在,请帮助我解决以下问题:我可以接收信标,开始做广告,但不能同时做这两个事情.

Edit 2: Breaking News ! I combined two flutter libraries, in particular flutter_beacon and beacon_broadcast. Now, please, help me with the following problem: i can receive beacon, start advertising but not both at the same time.

我不知道我的错误是什么,让我们在这里看看:

I don't know what my mistake is, anyway, let's have a look here:

我用来扫描信标的代码如下:

The code i use to scan beacons is the following:

  Future<void> beacon() async {

  await flutterBeacon.initializeScanning;

  final regions = <Region>[];

// Android platform, it can ranging out of beacon that filter all of Proximity UUID
  regions.add(Region(identifier: 'unito'));

      // to start monitoring beacons
  var _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
    // result contains a region, event type and event state
     print(result);
     print(result.toJson);
     print(result.beacons.toString());
  });
}

以下内容显示在调试控制台中:

The following content is displayed into the debug console:

但是,不幸的是,如果我同时激活BLE广告,也不会发生任何事情:

But, unfortunately, nothing happens if i also activate BLE Advertising:

那么,有什么建议吗?当我分别使用一种设备进行扫描而另一种用于广告时,它可以工作...

So, are there any advices ? When i use separately a device for scanning and another for advertising it works...

推荐答案

发送自定义数据只是编码问题.信标标识符只是对蓝牙数据包中一系列字节的解释.您可以根据需要安全地重新解释这些字节,从而重新使用所需的任何信标格式.

Sending custom data is a matter of encoding. Beacon identifiers are just an interpretation of a series of bytes in a Bluetooth packet. You can safely reinterpret these bytes however you want, reusing any beacon format you want.

例如,在您的问题中,您具有以下标识符:

For example in your question you have this identifier:

39ED98FF-2900-441A-802F-9C398FC199D2

为什么不将其重新解释为一系列16位整数?

Why not reinterpret this as a series of 16 bit integers?

39ED, 98FF, 2900, 441A, 802F, 9C39, 8FC1, 99D2

您还可以将字符串编码为字节流.例如,Eddystone-URL是一种将URL编码到Bluetooth字节数组中的格式.

You could also encode strings into the byte stream. Eddystone-URL, for example, is a format that encodes a URL into the Bluetooth byte array.

我写了您所引用的博客文章,通常都可以在Swift或Kotlin上完成所有工作,但是如果找到可以发送和接收BLE广告包的模块,就没有理由不能使用Flutter做到这一点.这些可以是原始广告,也可以是信标格式,例如您可以重新解释的AltBeacon或Eddystone.

I wrote the blog post you referenced, and generally do all work natively in Swift or Kotlin, but there is no reason you cannot do this with Flutter if you can find modules that let you transmit and receive BLE advertising packets. These can be raw advertisements or beacon formats like AltBeacon or Eddystone you can reinterpret.

问题中的示例显示了信标布局.这是一个简短的表达式,显示了如何将字节序列解释为特定长度的标识符数组.我创建了 Beacon布局表达式.这些简短的字符串使您可以简洁地定义如何对信标数据包中的字节进行编码和解码.

The example in the question shows a beacon layout. That is a short expression that shows how to interpret byte sequences as an array of identifiers of specific lengths. I created the concept of beacon layout expressions when I first wrote the Android Beacon Library. These are short strings that let you succinctly define how to encode and decode the bytes in a beacon packet.

如果您可以找到用于flutter的程序包,从而可以在发送和接收端基于这些表达式来设置布局,那么您的工作就快完成了.否则,您只需要手动重新编码即可.

If you can find packages for flutter that let you set layouts based on these expressions on both the transmit and receive side, your work is nearly done. Otherwise, you just have to re-encode manually.

这篇关于有没有一种方法可以实现像信标一样的蓝牙广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆