+ All Categories
Home > Documents > Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration...

Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration...

Date post: 19-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
39
Lecture #8 Nearby & Communication Services Android Things 2020
Transcript
Page 1: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Lecture #8 Nearby & Communication

ServicesAndroid Things 2020

Page 2: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby

Page 3: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby• Connections API

Page 4: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby• Connections API

• Messages API

Page 5: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby• Connections API

• Messages API

• Fast Pair

Page 6: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API• A Publish-Subscribe API.

Page 7: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

Page 8: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

• Bluetooth.

Page 9: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

• Bluetooth.

• Bluetooth Low Energy.

Page 10: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

• Bluetooth.

• Bluetooth Low Energy.

• Wi-Fi.

Page 11: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

• Bluetooth.

• Bluetooth Low Energy.

• Wi-Fi.

• Near-ultrasonic audio.

Page 12: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

Page 13: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Nearby Messages API

Page 14: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Configuration

apply plugin: 'android'...

dependencies { compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Page 15: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Configurationapply plugin: 'android'...

dependencies { compile 'com.google.android.gms:play-services-nearby:16.0.0'}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.google.sample.app" >    <application ...>        <meta-data            android:name="com.google.android.nearby.messages.API_KEY"            android:value="API_KEY" /> <uses-library android:name="com.google.android.things" />        <activity>        ...        </activity>    </application></manifest>

Page 16: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Publish and Subscribe@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... mMessageListener = new MessageListener() { @Override public void onFound(Message message) { Log.d(TAG, "Found message: " + new String(message.getContent())); }

@Override public void onLost(Message message) { Log.d(TAG, "Lost sight of message: " + new String(message.getContent())); } }

mMessage = new Message("Hello World".getBytes());}

@Overridepublic void onStart() { super.onStart(); ... Nearby.getMessagesClient(this).publish(mMessage); Nearby.getMessagesClient(this).subscribe(mMessageListener);}

Page 17: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

super.onCreate(savedInstanceState); ... mMessageListener = new MessageListener() { @Override public void onFound(Message message) { Log.d(TAG, "Found message: " + new String(message.getContent())); }

@Override public void onLost(Message message) { Log.d(TAG, "Lost sight of message: " + new String(message.getContent())); } }

mMessage = new Message("Hello World".getBytes());}

@Overridepublic void onStart() { super.onStart(); ... Nearby.getMessagesClient(this).publish(mMessage); Nearby.getMessagesClient(this).subscribe(mMessageListener);}

@Overridepublic void onStop() { Nearby.getMessagesClient(this).unpublish(mMessage); Nearby.getMessagesClient(this).unsubscribe(mMessageListener); ... super.onStop();}

Page 18: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Handling User Consent

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)        == PackageManager.PERMISSION_GRANTED) {    mMessagesClient = Nearby.getMessagesClient(this, new MessagesOptions.Builder() .setPermissions(NearbyPermissions.BLE) .build());}

Page 19: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Get Beacon Messagespublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... mMessageListener = new MessageListener() { @Override public void onFound(Message message) { Log.d(TAG, "Found message: " + new String(message.getContent())); }

@Override public void onLost(Message message) { Log.d(TAG, "Lost sight of message: " + new String(message.getContent())); } }}

// Subscribe to receive messages.private void subscribe() { Log.i(TAG, "Subscribing."); SubscribeOptions options = new SubscribeOptions.Builder() .setStrategy(Strategy.BLE_ONLY) .build(); Nearby.getMessagesClient(this).subscribe(mMessageListener, options);}

Page 20: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Get Beacon Messages

// Subscribe to messages in the background.private void backgroundSubscribe() {    Log.i(TAG, "Subscribing for background updates.");    SubscribeOptions options = new SubscribeOptions.Builder()            .setStrategy(Strategy.BLE_ONLY)            .build();    Nearby.getMessagesClient(this).subscribe(getPendingIntent(), options);}

private PendingIntent getPendingIntent() {    return PendingIntent.getBroadcast(this, 0, new Intent(this, BeaconMessageReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);}

Subscribe in the background

Page 21: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Get Beacon MessagesSubscribe in the background

@Overridepublic void onReceive(Context context, Intent intent) { Nearby.getMessagesClient(context).handleIntent(intent, new MessageListener() { @Override public void onFound(Message message) { Log.i(TAG, "Found message via PendingIntent: " + message); }

@Override public void onLost(Message message) { Log.i(TAG, "Lost message via PendingIntent: " + message); } });}

https://developers.google.com/nearby/messages/android/get-beacon-messages

Page 22: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 23: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 24: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 25: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 26: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 27: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Android Things Nearby Connection

Page 28: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Schema

Page 29: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Advertise

val advertisingOptions = AdvertisingOptions.Builder().setStrategy(Strategy.P2P_STAR).build()client = Nearby.getConnectionsClient(ctx)client.startAdvertising("AndroidThings", SERVICE_ID, connectionLifeCycleCB, advertisingOptions) .addOnSuccessListener { logi("OnSuccess...") } .addOnFailureListener { e -> loge("OnFailure: ", e) }

Page 30: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

DiscoverNearby.getConnectionsClient(ctx) .startDiscovery(SERVICE_ID, endpointDiscoveryCB, discoveryOptions) .addOnSuccessListener { logi("OnSuccess...") listener.startDiscovering() } .addOnFailureListener { e -> loge("OnFailure", e) }

Page 31: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

LCD Screenimplementation 'com.nilhcem.androidthings:driver-lcd1602:0.0.3'

val GPIO_LCD_RS = "BCM26"val GPIO_LCD_EN = "BCM19"val GPIO_LCD_D4 = "BCM21"val GPIO_LCD_D5 = "BCM20"val GPIO_LCD_D6 = "BCM16"val GPIO_LCD_D7 = "BCM12"

mLcd = Lcd1602(GPIO_LCD_RS, GPIO_LCD_EN, GPIO_LCD_D4, GPIO_LCD_D5, GPIO_LCD_D6, GPIO_LCD_D7)mLcd.clear()mLcd.print(“Hello World!”)

https://github.com/Nilhcem/1602A-androidthings

Page 32: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Panic Button

Page 33: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Panic Button

• When pressing the button more than 600ms send an SMS to a predefined number.

• To send the sms we can use, either:

• https://www.twilio.com

• GSM Sim900A module.

Page 34: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

www.twilio.com

• Create a trial account.

• You will get:

• An API key.

• A client id code.

Page 35: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Send a SMS Messagefun sms(body: String) { val formBody = FormBody.Builder() .add("From", ORIGIN_PHONE_NUMBER) .add("To", DEST_PHONE_NUMBER) .add("Body", body) .build() val builder = Request.Builder() .url(TWILLIO_MESSAGE_URL) .post(formBody) execute(builder) }

Page 36: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Authenticate the Callprivate fun execute(builder: Request.Builder) { val credential = Credentials.basic(TWILLIO_ACCOUNT_SID, TWILLIO_AUTH_TOKEN) val request = builder .addHeader("Authorization", credential) .build() httpClient.newCall(request)?.enqueue(object : Callback { @Throws(IOException::class) override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { logd("Twilio request succeed.") } else { logd("Twilio request failed. Make sure you have the correct configuration") } } override fun onFailure(call: Call, e: IOException) { logd("Twilio request failed.", e) } })}

Page 37: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Make a Phone Callfun call() { val url = "https://someurl.com"

val formBody = FormBody.Builder() .add("From", ORIGIN_PHONE_NUMBER) .add("To", DEST_PHONE_NUMBER) .add("Url", url) .build()

val builder = Request.Builder() .url(TWILLIO_CALL_URL) .post(formBody)

execute(builder)}

Page 38: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Lecture outcomes

• Nearby API.

• Send sms messages.

• Make phone calls.

Page 39: Lecture #8 Nearby & Communication Servicesdan/at/Lecture8.pdf · 2020. 4. 15. · Configuration apply plugin: 'android'... dependencies {compile 'com.google.android.gms:play-services-nearby:16.0.0'}

Recommended