1

Edit: So after researching for days i decided to stop using nearby messages api because it doesn't work.. The Google also wants it Deprecated. So i used flutter_beacon to listen to messages and broadcast_beacon to send messages using the BLE technology

i have been trying to use the nearby messages api plugin of flutter to broadcast and receive messages. the code seems perfectly okay but the message published is not received in the subscribing device.

here is my code

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_nearby_messages_api/flutter_nearby_messages_api.dart';

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

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

class _MyAppState extends State<MyApp> {
  FlutterNearbyMessagesApi nearbyMessagesApi = FlutterNearbyMessagesApi();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // For iOS
    await nearbyMessagesApi.setAPIKey('AIzaSyBUdrSDYbhLtOxzSBDbwu_NtUgR9IOq5zQ');

    nearbyMessagesApi.onFound = (message) {
      print('~~~onFound : $message');
    };

    nearbyMessagesApi.onLost = (message) {
      print('~~~onLost : $message');
    };

    nearbyMessagesApi.statusHandler = (status) {
      print('~~~statusHandler : $status');
    };

    nearbyMessagesApi.setPermissionAlert(
        'Your title', 'Your message', 'Deny', 'Grant');

    nearbyMessagesApi.permissionHandler = (status) {
      print(status);
    };

    nearbyMessagesApi.bluetoothPowerErrorHandler = (args) {
      print('~~~ bluetoothPowerErrorHandler');
    };

    nearbyMessagesApi.bluetoothPermissionErrorHandler = (args) {
      print('~~~ bluetoothPermissionErrorHandler');
    };

    nearbyMessagesApi.microphonePermissionErrorHandler = (args) {
      print('~~~ microphonePermissionErrorHandler');
    };

    // Do not use it if you have not learned it carefully
    // nearbyMessagesApi.setNearbyAccessPermission(true);
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Flutter Nearby Messages Example'),
        ),
        body: new Container(
          color: Colors.white70,
          child: new Column(children: [
            new TextButton(
                onPressed: () async {
                  await nearbyMessagesApi.publish('Hello world!');
                },
                child: new Text("publish")),
            new TextButton(
                onPressed: () async {
                  await nearbyMessagesApi.unPublish();
                },
                child: new Text("unPublish")),
            new TextButton(
                onPressed: () async {
                  await nearbyMessagesApi.backgroundSubscribe();
                },
                child: new Text("backgroundSubscribe")),
            new TextButton(
                onPressed: () async {
                  await nearbyMessagesApi.backgroundUnsubscribe();
                },
                child: new Text("unSubscribe"))
          ]),
        ),
      ),
    );
  }
}

i have also included the API key in the manifest file of android folder

<meta-data
           android:name="com.google.android.nearby.messages.API_KEY"
           android:value="AIzaSyBUdrSDYbhLtOxzSBDbwu_NtUgR9IOq5zQ" />

0 Answers0