[null,null,["最后更新时间 (UTC):2025-08-13。"],[[["\u003cp\u003eYour app can advertise and discover nearby devices to establish connections after necessary permissions are granted.\u003c/p\u003e\n"],["\u003cp\u003eChoose a \u003ccode\u003eStrategy\u003c/code\u003e to define the connection topology (one-to-many or many-to-many) for your app's discovery and advertising.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003estartAdvertising()\u003c/code\u003e and \u003ccode\u003estartDiscovery()\u003c/code\u003e with a unique \u003ccode\u003eserviceId\u003c/code\u003e (preferably your app's package name) for advertising and discovering, respectively.\u003c/p\u003e\n"],["\u003cp\u003eImplement \u003ccode\u003eConnectionLifecycleCallback\u003c/code\u003e and \u003ccode\u003eEndpointDiscoveryCallback\u003c/code\u003e to manage connection requests and discovered devices.\u003c/p\u003e\n"],["\u003cp\u003eCall \u003ccode\u003estopAdvertising()\u003c/code\u003e and \u003ccode\u003estopDiscovery()\u003c/code\u003e when advertising and discovering are no longer needed, respectively, but be aware of their impact on ongoing connections and future discoveries.\u003c/p\u003e\n"]]],[],null,["# Advertise and discover\n\nOnce the user has granted all required permissions, your app can begin to\nadvertise and discover in order to find nearby devices.\n\nFirst, [choose a `Strategy`](/nearby/connections/strategies) for your use case.\nThe `Strategy` you select determines the connection topology for your app (one\nadvertiser to N discoverers, or M advertisers to N discoverers).\n\nOn devices that will advertise, call `startAdvertising()` with the desired\n`Strategy` and a `serviceId` parameter that identifies your app.\n\nOn devices that will discover nearby advertisers, call `startDiscovery()` with\nthe same `Strategy` and `serviceId`.\n\nThe `serviceId` value must uniquely identify your app. As a best practice, use\nthe package name of your app (for example, `com.google.example.myapp`).\n\nThe following example shows how to advertise: \n\n```scdoc\nprivate void startAdvertising() {\n AdvertisingOptions advertisingOptions =\n new AdvertisingOptions.Builder().setStrategy(STRATEGY).build();\n Nearby.getConnectionsClient(context)\n .startAdvertising(\n getLocalUserName(), SERVICE_ID, connectionLifecycleCallback, advertisingOptions)\n .addOnSuccessListener(\n (Void unused) -\u003e {\n // We're advertising!\n })\n .addOnFailureListener(\n (Exception e) -\u003e {\n // We were unable to start advertising.\n });\n}\n```\n\nThe `ConnectionLifecycleCallback` parameter is the callback that will be invoked\nwhen discoverers request to connect to the advertiser. See [Manage\nConnections](/nearby/connections/android/manage-connections) for details about\ndefining this callback.\n\nThe following example shows how to discover: \n\n```scdoc\nprivate void startDiscovery() {\n DiscoveryOptions discoveryOptions =\n new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();\n Nearby.getConnectionsClient(context)\n .startDiscovery(SERVICE_ID, endpointDiscoveryCallback, discoveryOptions)\n .addOnSuccessListener(\n (Void unused) -\u003e {\n // We're discovering!\n })\n .addOnFailureListener(\n (Exception e) -\u003e {\n // We're unable to start discovering.\n });\n}\n```\n\nThe `EndpointDiscoveryCallback` parameter is the callback that will be invoked\nwhen nearby advertisers are discovered or lost. See [Manage\nConnections](/nearby/connections/android/manage-connections) for details about\ndefining this callback.\n\nCall `stopAdvertising()` when you no longer need to advertise, and\n`stopDiscovery()` when you no longer need to discover.\n| **Note:** After calling `stopDiscovery()`, the discoverer can still request connections to advertisers that were discovered; however, the discoverer will not discover any new advertisers until it starts discovery again.\n| **Caution:** Since most forms of discovery entail heavy radio operations that increase the odds of established connections getting broken, a very common pattern is to invoke `stopDiscovery()` once you find all the peers you want to connect to and exchange data with."]]