FLUTTER NEARBY MESSAGES API

Google Nearby Messages API for Flutter

Installation

First, add flutter_nearby_messages_api as a dependency in your pubspec.yaml file.

iOS

Get and add a google cloud nearby messages API key to your project https://developers.google.com/nearby/messages/ios/get-started

nearbyMessagesApi.setAPIKey('API_KEY');

Add two rows to the ios/Runner/Info.plist:

  • Privacy - Microphone Usage Description and a usage description. (For example description, "The microphone listens for anonymous tokens from nearby devices.")

For iOS 13 and newer versions

  • Privacy - Bluetooth Always Usage Description and a usage description.

For earlier than iOS 13

  • Privacy - Bluetooth Peripheral Usage Description and a usage description. (For example description, "An anonymous token is advertised via Bluetooth to discover nearby devices.")

Or in text format add the key:

<key>NSMicrophoneUsageDescription</key>
<string>The microphone listens for anonymous tokens from nearby devices</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>A message that tells the user why the app needs access to Bluetooth.</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>An anonymous token is advertised via Bluetooth to discover nearby devices</string>

Android

Get and add a google cloud nearby messages API key to your project https://developers.google.com/nearby/messages/android/get-started

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.sample.app" >
    <application ...>
        <meta-data
            android:name="com.google.android.nearby.messages.API_KEY"
            android:value="API_KEY" />
        <activity>
        ...
        </activity>
    </application>
</manifest>

Example

import 'package:flutter_nearby_messages_api/flutter_nearby_messages_api.dart';

class main {
    // init NearbyMessagesApi
    FlutterNearbyMessagesApi nearbyMessagesApi = FlutterNearbyMessagesApi();
 
    Future<void> main() async {
      // config for iOS
      await nearbyMessagesApi.setAPIKey('API_KEY');

      // This callback gets the message when an a nearby device sends one
      nearbyMessagesApi.onFound = (message) {
        print('~~~onFound : $message');
      };

      // This callback gets the message when a nearby device leaves
      nearbyMessagesApi.onLost = (message) {
        print('~~~onLost : $message');
      };
      
      // Listen status when publish and subscribe
      // enum GNSOperationStatus { inactive, starting, active }
      nearbyMessagesApi.statusHandler = (status) {
        print('~~~statusHandler : $status');
      };

      // Use your messages to nearby permission alert
      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');
      };

      /**
      * Publishing a message
      * @require message
      */
      await nearbyMessagesApi.publish('Nice to meet you #NTMY');
      /**
      * Subscribing
      */
      await nearbyMessagesApi.backgroundSubscribe();

      // Enable debug mode
      await nearbyMessageAPi.enableDebugMode();
      // Disable debug mode
      await nearbyMessageAPI.disableDebugMode();

      // Do not use it if you have not learned it carefully
      // nearbyMessagesApi.setNearbyAccessPermission(true);
    
    void dispose() {
      // unPublish
      await nearbyMessagesApi.unPublish();
      // backgroundUnsubscribe
      await nearbyMessagesApi.backgroundUnsubscribe();
    }
}

Libraries

flutter_nearby_messages_api