เอกสารนี้อธิบายวิธีเริ่มพัฒนาด้วย Nearby Messages API ใน Android Nearby Messages API เป็นส่วนหนึ่งของบริการ Google Play
ขั้นตอนที่ 1: ติดตั้งบริการ Google Play
Nearby Messages API พร้อมให้บริการในอุปกรณ์ Android ที่มีบริการ Google Play 7.8.0 ขึ้นไป อุปกรณ์ที่ใช้ Android 2.3 ขึ้นไปซึ่งมีแอป Google Play Store จะได้รับการอัปเดตบริการ Google Play โดยอัตโนมัติ หากต้องการตรวจสอบเวอร์ชันบริการ Google Play ที่ติดตั้งในอุปกรณ์ ให้ไปที่การตั้งค่า > แอป > บริการ Google Play
ตรวจสอบว่าคุณมีไลบรารีไคลเอ็นต์ล่าสุดสำหรับบริการ Google Play ในโฮสต์การพัฒนาแล้ว โดยทำดังนี้
หากต้องการใช้ Nearby Messages API คุณต้องมีบัญชี Google
เพื่อให้คุณในฐานะนักพัฒนาแอปเปิดใช้ Nearby API ในขั้นตอนถัดไปได้ (ผู้ใช้ไม่จำเป็นต้องมีบัญชี Google)
หากมีบัญชีอยู่แล้ว ทุกอย่างก็พร้อมใช้งานแล้ว คุณอาจต้องใช้บัญชี Google แยกต่างหากเพื่อวัตถุประสงค์ในการทดสอบด้วย
ขั้นตอนที่ 3: รับคีย์ API
ทําตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ Google Nearby Messages API สําหรับ Android และรับคีย์ API
หมายเหตุ: รหัสผ่านสำหรับคีย์สโตร์สำหรับการแก้ไขข้อบกพร่องคือ android ใน Mac OS และ Linux โดยทั่วไปแล้วคีย์สโตร์สำหรับการแก้ไขข้อบกพร่องจะอยู่ที่ ~/.android/debug.keystore ใน Windows โดยทั่วไปจะอยู่ที่ %USERPROFILE%\.android\debug.keystore
Android Studio ช่วยให้คุณสร้างโปรเจ็กต์สำหรับ Nearby Messages API ได้อย่างง่ายดาย ทําตามขั้นตอนที่อธิบายไว้ในส่วนการสร้างโปรเจ็กต์เพื่อสร้างโปรเจ็กต์ใหม่ ใน Android Studio ให้เปิดไฟล์ build.gradle ของโมดูล แล้วเพิ่มไลบรารีไคลเอ็นต์บริการ Google Play เป็น Dependency ดังนี้
[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."]]