val client = Nearby.getMessagesClient(this) val payload = byteArrayOf(...) val message = Message(payload, "my_message_type") client.publish(message).addOnSuccessListener { // yay }.addOnFailureListener({ // exception is ApiException })
This requires android.permission.BLUETOOTH and ACCESS_COARSE_LOCATION // Android 6.0 and up requires location to be enabled too! client.subscribe(messageListener, SubscribeOptions.Builder(). setStrategy(Strategy.BLE_ONLY).build()). addOnFailureListener { }
beacons iBeacon proximity id w/ optional major and minor ids Eddystone namespace with optional instance Obtain beacon id from a Message using IBeaconId and EddystoneUid
android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- Request at runtime on M and up --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> </manifest>
ConnectionResolution) { } override fun onDisconnected(endpoint: String) { } override fun onConnectionInitiated(endpoint: String, info: ConnectionInfo) { // Make sure to failures from the resulting task! // Instead of accepting, you can also reject or accept later // ConnectionInfo contains an auth token for OOB use client.acceptConnection(endpoint, MyPayloadCallback()) } }
First argument is the name for this device // Empty string generates a name client.startAdvertising("", BuildConfig.APPLICATION_ID, MyConnectionCallback(), AdvertisingOptions(Strategy.P2P_CLUSTER)). addOnFailureListener { }
if empty client.requestConnection("", endpoint, MyConnectionCallback()). addOnFailureListener { // Use status code from ApiException // to check what went wrong }
String, payload: Payload) { // Check payload type and handle the (partial) payload } override fun onPayloadTransferUpdate(endpoint: String, update: PayloadTransferUpdate) { // for Payload.Type.Bytes this is not needed, for other types // reports progress (both incoming and outgoing) } }