ภาพรวม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การเชื่อมต่อใกล้เคียงเป็น API การเชื่อมต่อเครือข่ายแบบเพียร์ทูเพียร์ที่ช่วยให้แอปค้นหา เชื่อมต่อ และแลกเปลี่ยนข้อมูลกับอุปกรณ์ที่อยู่ใกล้เคียงได้อย่างง่ายดายในแบบเรียลไทม์ ไม่ว่าจะมีหรือไม่มีการเชื่อมต่อเครือข่ายก็ตาม
ตัวอย่างกรณีการใช้งานมีดังนี้
- ไวท์บอร์ดเพื่อการทำงานร่วมกัน: จดไอเดียกับผู้เข้าร่วมที่อยู่ใกล้เคียงบนไวท์บอร์ดเสมือนจริงที่แชร์
- การเล่นเกมแบบผู้เล่นหลายคนในพื้นที่: ตั้งค่าเกมแบบผู้เล่นหลายคนและเชิญผู้ใช้คนอื่นๆ
ที่อยู่ใกล้เคียงให้เข้าร่วม
- การเล่นเกมแบบหลายหน้าจอ: ใช้โทรศัพท์หรือแท็บเล็ตเป็นเกมคอนโทรลเลอร์เพื่อเล่นเกมที่แสดงบนอุปกรณ์ Android หน้าจอขนาดใหญ่ที่อยู่ใกล้เคียง เช่น Android TV
- การโอนไฟล์แบบออฟไลน์: แชร์รูปภาพ วิดีโอ หรือข้อมูลประเภทอื่นๆ
ได้อย่างรวดเร็วและไม่ต้องเชื่อมต่อเครือข่าย
บทนำ
การเชื่อมต่อใกล้เคียงช่วยให้การโฆษณา การค้นหา และการเชื่อมต่อระหว่างอุปกรณ์ที่อยู่ใกล้เคียงเป็นแบบเพียร์ทูเพียร์ที่ออฟไลน์โดยสมบูรณ์ การเชื่อมต่อระหว่าง
อุปกรณ์มีแบนด์วิดท์สูง เวลาในการตอบสนองต่ำ และมีการเข้ารหัสอย่างเต็มรูปแบบเพื่อช่วยให้การโอนข้อมูลเป็นไปอย่างรวดเร็ว
และปลอดภัย
เป้าหมายหลักของ API นี้คือการจัดหาแพลตฟอร์มที่เรียบง่าย เชื่อถือได้
และมีประสิทธิภาพ API นี้ใช้เทคโนโลยีบลูทูธ, BLE และ Wi-Fi ร่วมกันเพื่อใช้ประโยชน์จากจุดแข็งของแต่ละเทคโนโลยี
ในขณะที่หลีกเลี่ยงจุดอ่อนของแต่ละเทคโนโลยี ซึ่งจะช่วยลดความซับซ้อนของ
บลูทูธและ Wi-Fi ในแพลตฟอร์มและระบบปฏิบัติการเวอร์ชันต่างๆ ได้อย่างมีประสิทธิภาพ
ช่วยให้นักพัฒนาแอปมุ่งเน้นไปที่ฟีเจอร์ที่สำคัญต่อผู้ใช้ได้
ภาพรวมของ API
การใช้งาน API แบ่งออกเป็น 2 ระยะ ได้แก่ ก่อนการเชื่อมต่อและหลังการเชื่อมต่อ
ในระยะก่อนการเชื่อมต่อ ผู้ลงโฆษณาจะโฆษณาตัวเอง ขณะที่ผู้ค้นหาจะค้นหาผู้ลงโฆษณาที่อยู่ใกล้เคียงและส่งคำขอเชื่อมต่อ คำขอเชื่อมต่อ
จากผู้ค้นหาไปยังผู้ลงโฆษณาจะเริ่มขั้นตอนการตรวจสอบสิทธิ์แบบสมมาตร
ซึ่งส่งผลให้ทั้ง 2 ฝ่ายยอมรับ (หรือปฏิเสธ) คำขอเชื่อมต่อ
โดยอิสระ
หลังจากทั้ง 2 ฝ่ายยอมรับคำขอเชื่อมต่อแล้ว ระบบจะถือว่าการเชื่อมต่อ
เสร็จสมบูรณ์และอุปกรณ์จะเข้าสู่ระยะหลังการเชื่อมต่อ
ซึ่งทั้ง 2 ฝ่ายจะแลกเปลี่ยนข้อมูลกันได้
การโฆษณาและการค้นพบ
ผู้ลงโฆษณาจะปรากฏต่อผู้ค้นพบรายอื่นๆ และผู้ค้นพบจะได้รับแจ้งเมื่อพบหรือสูญเสียผู้ลงโฆษณารายใหม่
สร้างการเชื่อมต่อ
เมื่อผู้ค้นพบต้องการเชื่อมต่อกับผู้ลงโฆษณาที่อยู่ใกล้เคียง ผู้ค้นพบสามารถ
ขอเชื่อมต่อได้
ทั้ง 2 ฝ่ายจะได้รับการแจ้งเตือนเกี่ยวกับกระบวนการเริ่มต้นการเชื่อมต่อ และทั้ง 2 ฝ่ายจะมีโอกาสยอมรับหรือปฏิเสธการเชื่อมต่อ
เมื่อทั้ง 2 ฝ่ายตอบกลับแล้ว แต่ละฝ่ายจะได้รับการแจ้งเตือนเกี่ยวกับผลลัพธ์ หากทั้ง 2 ฝ่ายยอมรับการเชื่อมต่อ ระบบจะถือว่าการเชื่อมต่อเสร็จสมบูรณ์และเริ่มโอนข้อมูลได้
การแลกเปลี่ยนข้อมูล
หลังจากสร้างการเชื่อมต่อแล้ว การใช้ API ต่อไปจะสมมาตรกัน ดังนั้นจึงไม่มีความแตกต่างระหว่างผู้ลงโฆษณาและผู้ค้นพบอีกต่อไป
ตอนนี้ทั้ง 2 ฝ่ายสามารถแลกเปลี่ยนข้อมูลได้แล้ว โดยมีเพย์โหลดที่รองรับ 3 ประเภทดังนี้
- ไบต์: อาร์เรย์ไบต์จำกัดไว้ที่ 32k ซึ่งเหมาะสำหรับการส่งข้อมูล เช่น ข้อมูลเมตาหรือข้อความควบคุม
- ไฟล์: ไฟล์ขนาดใดก็ได้ โดยจะมีการโอนไฟล์เหล่านี้จากแอปไปยัง
อินเทอร์เฟซเครือข่ายโดยมีการคัดลอกข้ามขอบเขตของกระบวนการน้อยที่สุด
- สตรีม: สตรีมข้อมูลที่สร้างขึ้นทันที เช่น ในกรณีของ
เสียง/วิดีโอที่บันทึกไว้ โดยไม่ทราบขนาดสุดท้ายล่วงหน้า
ผู้รับจะได้รับการแจ้งเตือนเกี่ยวกับข้อมูลใหม่ที่เข้ามา รวมถึงการอัปเดตความคืบหน้า
การยกเลิก หรือความล้มเหลว
การเชื่อมต่อที่สร้างขึ้นเป็นแบบฟูลดูเพล็กซ์ ซึ่งหมายความว่าผู้ลงโฆษณาและผู้ค้นพบสามารถส่งและรับข้อมูลได้พร้อมกัน
การเก็บรวบรวมข้อมูล
เมื่อใช้ Nearby SDK ของบริการ Google Play ระบบจะรวบรวมข้อมูลวิเคราะห์การใช้งานเพื่อ
ปรับปรุงประสบการณ์การใช้งาน
- เมตริกประสิทธิภาพ: ระบบจะรวบรวมเวลาในการตอบสนอง ความน่าเชื่อถือ และ
ปริมาณงานของการค้นหาและการเชื่อมต่อเมื่อสแกนหาและเชื่อมต่อกับอุปกรณ์โดยใช้
Nearby SDK
- ข้อมูลอุปกรณ์: ระบบจะรวบรวมรุ่นอุปกรณ์ ประเทศ เวอร์ชันบิลด์ และ
ชื่อแพ็กเกจแอปพลิเคชัน แล้วเชื่อมโยงกับเมตริกประสิทธิภาพ
เพื่อระบุการแบ่งตามภูมิภาคและอุปกรณ์
ผู้ใช้ปลายทางสามารถอนุญาตหรือปฏิเสธการเก็บรวบรวมนี้ได้โดยไปที่การตั้งค่า > Google >
การใช้งานและการวินิจฉัย
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-13 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-13 UTC"],[[["\u003cp\u003eNearby Connections is a peer-to-peer networking API enabling apps to discover, connect, and exchange data with nearby devices in real-time, even without internet access.\u003c/p\u003e\n"],["\u003cp\u003eIt leverages Bluetooth, BLE, and Wi-Fi for high-bandwidth, low-latency, and encrypted communication, abstracting away the complexities of these technologies.\u003c/p\u003e\n"],["\u003cp\u003eThe API facilitates advertising, discovery, and connection establishment between devices, followed by data exchange using bytes, files, or streams.\u003c/p\u003e\n"],["\u003cp\u003eUsage analytics, including performance metrics and device information, are collected to enhance the API's performance but can be controlled by users in their device settings.\u003c/p\u003e\n"]]],[],null,["# Overview\n\nNearby Connections is a peer-to-peer networking API that allows apps to easily\ndiscover, connect to, and exchange data with nearby devices in real-time,\nregardless of network connectivity.\n\nSome example use cases:\n\n- **Collaborative whiteboard:** Jot ideas down with nearby participants on a shared virtual whiteboard.\n- **Local multiplayer gaming:** Set up a multiplayer game and invite other users nearby to join.\n- **Multi-screen gaming:** Use a phone or tablet as a game controller to play games displayed on a nearby large-screen Android device, such as Android TV.\n- **Offline file transfers:** Share photos, videos, or any other type of data quickly and without requiring a network connection.\n\nIntroduction\n------------\n\nNearby Connections enables advertising, discovery, and connections between\nnearby devices in a fully-offline peer-to-peer manner. Connections between\ndevices are high-bandwidth, low-latency, and fully encrypted to enable fast,\nsecure data transfers.\n\nA primary goal of this API is to provide a platform that is simple, reliable,\nand performant. Under the hood, the API uses a combination of Bluetooth, BLE,\nand Wi-Fi technologies, leveraging the strengths of each while circumventing\ntheir respective weaknesses. This effectively abstracts the vagaries of\nBluetooth and Wi-Fi across a range of platforms and OS versions, allowing\ndevelopers to focus on the features that matter to their users.\n\nAPI overview\n------------\n\nUsage of the API falls into two phases: pre-connection, and post-connection.\n\nIn the pre-connection phase, Advertisers advertise themselves, while Discoverers\ndiscover nearby Advertisers and send connection requests. A connection request\nfrom a Discoverer to an Advertiser initiates a symmetric authentication flow\nthat results in both sides independently accepting (or rejecting) the connection\nrequest.\n\nAfter a connection request is accepted by both sides, the connection is\nconsidered to be established and the devices enter the post-connection phase,\nduring which both sides can exchange data.\n\n### Advertising and discovery\n\nAdvertisers will become visible to other discovers and discoverers will be\nnotified when a new advertiser is found or has been lost.\n\n### Establishing connections\n\nWhen a discoverer wishes to connect to a nearby advertiser, the discoverer can\nrequest a connection.\n\nBoth sides will be notified of the connection initiation process and both will\nhave the opportunity to accept or reject the connection.\n\nOnce both sides have responded, each will be notified of the result. If both\nsides accepted the connection, the connection is considered established, and\ndata transference can then begin.\n\n### Exchanging data\n\nAfter a connection is established, further API usage is symmetrical, so there's\nno longer a distinction between Advertiser and Discoverer.\n\nBoth sides can now exchange data. There are 3 types of supported Payloads:\n\n- **Bytes:** Byte arrays limited to 32k; these are good for sending things such as metadata or control messages.\n- **File:** Files of any size; these are transferred from the app to the network interface with minimal copying across process boundaries.\n- **Stream:** A stream of data that is generated on the fly, as in the case of recorded audio/video, with no final size known beforehand.\n\nReceivers will be notified of new incoming data and any progress updates,\ncancellations or failures.\n\nThe connections established are full-duplex, which means that advertisers and\ndiscoverers can simultaneously send and receive data.\n\n### Data Collection\n\nWhen using the Google Play services Nearby SDK, usage analytics are collected to\nimprove the experience.\n\n- **Performance metrics:** Discovery and connection latency, reliability and throughput are collected when scanning for and connecting to devices using the Nearby SDK.\n- **Device information:** Device model, country, build version and the application package name are collected and associated with the performance metrics to identify regional and device-specific slices.\n\nEnd users can allow or deny this collection by navigating to Settings \\\u003e Google \\\u003e\nUsage \\& diagnostics."]]