このドキュメントでは、Android で Nearby Messages API を使用して開発を開始する方法について説明します。Nearby Messages API は Google Play 開発者サービスの一部です。
ステップ 1: Google Play 開発者サービスを入手する
Nearby Messages API は、Google Play 開発者サービス 7.8.0 以降を搭載した Android デバイスで利用できます。Android 2.3 以降を搭載し、Google Play ストア アプリがインストールされているデバイスには、Google Play 開発者サービスのアップデートが自動的に配信されます。デバイスにインストールされている Google Play 開発者サービスのバージョンを確認するには、[設定] > [アプリ] > [Google Play 開発者サービス] に移動します。
開発ホストに Google Play 開発者サービスの最新のクライアント ライブラリがあることを確認します。
Nearby Messages API を使用するには、Google アカウントが必要です。これは、デベロッパーが次のステップで Nearby API を有効にできるようにするためです(ユーザーは Google アカウントを必要としません)。すでにアカウントをお持ちの場合は、準備が整っています。テスト用に別の Google アカウントを取得することもできます。
ステップ 3: API キーを取得する
Android 向け Google Nearby Messages API を有効にして API キーを取得する手順は次のとおりです。
注: デバッグ キーストアのパスワードは android です。Mac OS と Linux の場合、デバッグ キーストアは通常 ~/.android/debug.keystore にあります。Windows では、通常 %USERPROFILE%\.android\debug.keystore にあります。
keytool コマンドの出力には、証明書の SHA1 フィンガープリントが含まれます。
ステップ 4: プロジェクトを構成する
Android Studio を使用すると、Nearby Messages API のプロジェクトを簡単に作成できます。プロジェクトの作成の手順に沿って、新しいプロジェクトを作成します。Android Studio で、モジュールの build.gradle ファイルを開き、Google Play 開発者サービス クライアント ライブラリを依存関係として追加します。
[null,null,["最終更新日 2025-08-29 UTC。"],[[["\u003cp\u003eThe Nearby Messages API is deprecated and will stop functioning in December 2023, with users directed to migrate to Nearby Connections.\u003c/p\u003e\n"],["\u003cp\u003eAs of August 1, 2021, the Nearby Messages API on Android no longer supports ultrasonic advertising and discovery, and now utilizes Bluetooth.\u003c/p\u003e\n"],["\u003cp\u003eDeveloping with the Nearby Messages API on Android requires Google Play services version 7.8.0 or higher, along with a Google account and an API key from the Google Developers Console.\u003c/p\u003e\n"],["\u003cp\u003eTo use the Nearby Messages API, you need to generate an Android API key using your app's SHA-1 fingerprint, ensuring all API keys for different platforms are generated within the same Google Developers Console project.\u003c/p\u003e\n"],["\u003cp\u003eIntegrating the Nearby Messages API into your Android project involves adding the Google Play services client library to your \u003ccode\u003ebuild.gradle\u003c/code\u003e file and configuring the API key in your application's manifest.\u003c/p\u003e\n"]]],[],null,["# Get Started\n\n| **Warning:** Nearby Messages is deprecated and will stop working as of December 2023. Please navigate to [Nearby Connections](//developers.google.com/nearby/connections/overview) for further support or [Migration Guideline](//developers.google.com/nearby/messages/android/migrate-to-nc) on how to migrate existing Nearby Messages usages to Nearby Connections.\n| **Warning:** Nearby Messages on Android will cease supporting ultrasonic advertising and discovery beginning August 1, 2021. Advertising and discovery will still occur over Bluetooth.\n\nThis document explains how to start developing with the Nearby Messages API on\nAndroid. The Nearby Messages API is part of\n[Google Play services](//developer.android.com/google/play-services/index.html).\n\nStep 1: Get Google Play services\n--------------------------------\n\nThe Nearby Messages API is available on Android devices with\n[Google Play services](//developer.android.com/google/play-services/index.html)\n7.8.0 or higher. Devices running Android 2.3 or higher that have the Google\nPlay Store app automatically receive updates to Google Play services. To check\nwhich version of Google Play services is installed on your device, go to\n**Settings \\\u003e Apps \\\u003e Google Play services**.\n\nEnsure that you have the latest client library for Google Play services on your\ndevelopment host:\n\n1. Open the [Android SDK Manager](//developer.android.com/sdk/installing/adding-packages.html).\n2. Under **Appearance \\& Behavior \\\u003e System Settings \\\u003e Android SDK \\\u003e SDK Tools**,\n ensure that the following packages are installed:\n\n - Google Play services\n - Google Repository\n\nStep 2: Get a Google account\n----------------------------\n\nTo use the Nearby Messages APIs, you need a [Google Account](//www.google.com/accounts/NewAccount).\nThis is so that you, the developer, can enable the Nearby API in the next\nstep (your users will not need to have a Google account).\nIf you already have an account, then you're all set. You may also want a\nseparate Google Account for testing purposes.\n\nStep 3: Get an API key\n----------------------\n\nTake these steps to enable the Google Nearby Messages API for Android and get\nan API key:\n\n1. Go to the [Google Developers Console](https://console.developers.google.com/flows/enableapi?apiid=copresence&keyType=CLIENT_SIDE_ANDROID&reusekey=true).\n2. Create or select a project to register your application with.\n3. Click **Continue** to Enable the API.\n4. On the **Credentials** page, create a new **Android key** (and set the API Credentials). \n Note: If you have an existing **Android key**, you may use that key.\n5. In the resulting dialog, enter your app's [SHA-1 fingerprint](#obtain_the_sha1_fingerprint_of_your_certificate) and package name. For example: \n\n ```\n BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75\n ``` \n\n ```\n com.example.android.nearbyexample\n ```\n6. Your new Android API key appears in the list of API keys for your project. An API key is a string of characters, something like this: \n\n ```\n AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0\n ```\n\n### Obtain the SHA1 fingerprint of your certificate\n\nTo create a new API key for your Android app, you need the SHA1 fingerprint\nof the certificate you use to sign your APK. Messages can only be exchanged\nbetween apps that are signed with API keys from the same project.\n\nTo obtain this fingerprint:\n\n1. Find the location of your keystore.\n2. In a terminal, run the `keytool` utility from the JDK. For example, if you are using the ```debug``` keystore: \n\n```\n$ keytool -alias \\\nandroiddebugkey -keystore \\\n~/.android/debug.keystore -list -v\n```\n3. **Note:** For the debug keystore, the password is `android`. On Mac OS and Linux, the debug keystore is typically located at `~/.android/``debug.keystore`. On Windows, it is typically located at `%USERPROFILE%\\``.android\\``debug.keystore`.\n4. The output of the `keytool` command contains the SHA1 fingerprint for the certicate.\n\n| **Note:** If you are also developing an iOS app, or using the Proximity Beacon REST API, make sure you generate all respective API keys within the same Google Developers Console project. For more information, see the [Getting Started guide for iOS](/nearby/messages/ios/get-started), and [Get Started with the REST API](/beacons/proximity/get-started).\n\nStep 4: Configure your project\n------------------------------\n\n[Android Studio](//developer.android.com/sdk/installing/studio.html) makes it\neasy to create a project for the Nearby Messages API. Follow the steps described\nin [Creating a Project](//developer.android.com/sdk/installing/create-project.html)\nto create a new project. In Android Studio, open the `build.gradle` file for\nyour module and add the Google Play services client library as a dependency: \n\n```\napply plugin: 'android'\n...\n\ndependencies {\n compile 'com.google.android.gms:play-services-nearby:19.3.0'\n}\n```\n\nThen, configure your manifest with the API Key generated in the previous step: \n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n package=\"com.google.sample.app\" \u003e\n \u003capplication ...\u003e\n \u003cmeta-data\n android:name=\"com.google.android.nearby.messages.API_KEY\"\n android:value=\"API_KEY\" /\u003e\n \u003cactivity\u003e\n ...\n \u003c/activity\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\nStep 5: Publish and subscribe\n-----------------------------\n\nIn your app, start using the Nearby Messages API. \n\n @Override\n public void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n ...\n mMessageListener = new MessageListener() {\n @Override\n public void onFound(Message message) {\n Log.d(TAG, \"Found message: \" + new String(message.getContent()));\n }\n\n @Override\n public void onLost(Message message) {\n Log.d(TAG, \"Lost sight of message: \" + new String(message.getContent()));\n }\n }\n\n mMessage = new Message(\"Hello World\".getBytes());\n }\n\n @Override\n public void onStart() {\n super.onStart();\n ...\n Nearby.getMessagesClient(this).publish(mMessage);\n Nearby.getMessagesClient(this).subscribe(mMessageListener);\n }\n\n @Override\n public void onStop() {\n Nearby.getMessagesClient(this).unpublish(mMessage);\n Nearby.getMessagesClient(this).unsubscribe(mMessageListener);\n ...\n super.onStop();\n }\n\nThe Nearby Messages API requires user consent. When either publish or subscribe\nis first invoked, Nearby will show an opt in dialog."]]