Is there a method to implement in flutter a sort of bluetooth broadcast like beacons?

2,276

Solution 1

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

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

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

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.

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.

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.

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.

Solution 2

actually you should get it work with the following package: https://pub.dev/packages/flutter_blue

As from another app project I know that this is highly customizable.

Set notifications and listen to changes #

await characteristic.setNotifyValue(true);
characteristic.value.listen((value) {
    // do something with new value
});

Full documentation in my provided link. You can read and write, connect with services and listen to every kind of notification you want.

Share:
2,276
docdev
Author by

docdev

Updated on December 20, 2022

Comments

  • docdev
    docdev over 1 year

    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:

    • I want to send random string and integers "in the air" using bluetooth.
    • I want also to be able to receive those data by other devices
    • The app must be able to send and receive simultaneously

    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.

    For example, let's consider beacon_broadcast library:

    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();
    

    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.

    Thank you very much.

    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).

    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:

    When i turn on scanning

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

    When i turn on advertising

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