+ All Categories
Home > Software > Connected World in android - Local data sharing and service discovery

Connected World in android - Local data sharing and service discovery

Date post: 15-Apr-2017
Category:
Upload: talentica-software
View: 191 times
Download: 1 times
Share this document with a friend
71
Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. Connected world in android Local data sharing and service discovery Presented by: Kaushal Dhruw Naval Barthwal
Transcript
Page 1: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved.

Connected world in android

Local data sharing and service discovery

Presented by:Kaushal DhruwNaval Barthwal

Page 2: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 2

About PMD• Started in August 2013

• “Increase awareness of fast growing mobile technology among Developers”

• Currently has 3000+ members

• 21 meetups so far

Page 3: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 3

Team• Ashutosh • Tarun• Arvind • Kaushal • Suyash • Vijendra

• Talentica Mobile Community

Page 4: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 4

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 5: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 5

Introduction

Network interfaces

Page 6: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 6

Network interface

A Network interface is a point of interconnection between a device and a public or private network. It can be a hardware (Network interface card). Or can be implemented in software.

Some network interfaces: wlan: wireless networking interface. lo: loopback interface (localhost, usually 127.0.0.1). rmnet: usually associated with mobile data and USB tethering. p2p: a peer-to-peer interface. eth, en: Ethernet card interface. dummy: alternative for loopback interfaces.

NetworkInterface.getNetworkInterfaces() //gets list of all available network interfaces

Page 7: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 7

Network interface methods isUp(): Whether the network interface is up and running. isVirtual(): Whether the network interface is virtual or sub-

interface. getSubInterfaces(): gets all the sub interfaces bound to this

network interface (like eth0:1 for eth0) supportsMulticast(): checks multicast support. getInetAddresses(): get all the addresses bound to this network

interface. getHardwareAddress(): usually MAC. getByName(): getByInetAddress(): getByIndex():

Page 8: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 8

Network interface selection When a socket is created it automatically determines which

network (that is currently up and running) to use. Most of the times you will not have to worry about the

network interface. When you want to use the address of a particular network

interface, use the get methods discussed in previous slide. To listen to all the network interfaces, bind your socket to

“0.0.0.0” Loopback and dummy interfaces are used for localhost

connection testing. use adb shell ifconfig to list all available network interfaces.

Page 9: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 9

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 10: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 10

Generic java not specific to android

Basic socket communication

Page 11: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 11

Basic socket communication

A socket is basically an end point for two-way communication link between two devices.

In a typical client-server architecture. Server runs on a specific computer and has a socket that is bound to a specific

port, the server listens for a client to make connection request in that socket. Client knows the host name of server and port it is listening on. The client

system usually assigns a port to rendezvous with server’s machine and port. Client receives confirmation of connection acceptance and can use the socket

for communicating with server. Listening on a port for client requests is done via ServerSocket. A ServerSocket

class provides a system independent implementation of the server side of client/server socket connection.

ServerSocket serverSocket = new ServerSocket(port);Socket s = serverSocket.accept();

Page 12: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 12

Basic socket communication contd…

Page 13: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 13

Steps involved in socket communication

Create a ServerSocket. This socket waits for a connection from a client on a specified port and blocks until it happens, so do this in a background thread.

Create a client Socket. The client uses the IP address and port of the server socket to connect to the server device.

Send data from the client to the server. When the client socket successfully connects to the server socket, you can send data from the client to the server with byte streams.

The server socket waits for a client connection (with the accept() method). This call blocks until a client connects, so call this in another thread. When a connection happens, the server device can receive the data from the client.

And carry out any actions with this data, such as saving it to a file or presenting it to the user.

Page 14: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 14

Client/server sockets

ServerSocket mServer = new ServerSocket(mPort);While(true){ Socket s = mServer.accept(); //process s and streams and return response if required}

socket = new Socket(host, port);OutputStream outputStream = socket.getOutputStream();ObjectOutputStream oos = new ObjectOutputStream(outputStream);oos.writeObject(transferObject);oos.close();

Server Client

mPort = (new ServerSocket(0)).getLocalPort()

Page 15: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 15

Use case we are solving (Local chat)

Share files& Send chat&Receive chat and file from other devices

Share files& Send chat&Receive chat and file from other devices

Device 1 Device 2

Page 16: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 16

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 17: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 17

Discover services and devices in the same network

Network service discovery (NSD)

Page 18: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 18

Network service discover (NSD)

android.net.nsd.* package is used. It helps with many use cases like file sharing, multi-player games

etc. Android network service discovery allows your app to identify other

devices in the same network that support the services your app requests.

NSD allows us to register our service(s) on the network, discover it, and connect with it.

Important classes: NsdManager NsdServiceInfo

Although internet is not used, INTERNET permission is required for socket communication.

Page 19: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 19

Basic steps in NSD

Page 20: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 20

NSD Service registration

Creating NsdServiceInfo Object

Page 21: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 21

NSD Service registration Listener

Page 22: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 22

NSD Service discovery

Page 23: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 23

NSD Service discovery Listener

Page 24: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 24

Resolving discovered NSD service

• NsdManager.ResolveListener

Note**: Do not forget to stop discovery and unregister service when app closes

Page 25: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 25

NSD Demoplus code walkthrough

Page 26: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 26

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 27: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 27

Connect with devices in vicinity (Wi-Fi p2p in android)

Wi-Fi direct

Page 28: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 28

Wi-Fi direct

Wi-Fi direct is a Wi-Fi certified standard enabling devices to connect with each other without requiring a wireless access point.

Using this devices can communicate with each other at typical Wi-Fi speeds, unlike ad-hoc wireless connections (which allows two or more devices to connect and communicate, and has a limit of 11Mbps). And Wi-Fi direct has much simpler setup that ad-hoc network.

Wi-Fi direct assigns each member a limited access point (if it is Wi-Fi direct enabled).

Wi-Fi direct uses WPS and WPA2 to prevent unauthorized access and keep communications private.

Page 29: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 29

Wi-Fi direct contd… Wi-Fi direct allows devices to connect to each other and form

groups. Not only one-to-one, but also one-to-many. Compatible devices negotiate their roles in the connection:

One of these assumes role of a traditional access point. Called Group Owner (GO).

Other devices including non-Wi-Fi direct devices connect to the Group owner or Access point as P2P clients.

Any P2P device can assume the role of a Group owner and P2P client. The purpose of role negotiation is to determine which of the peer devices will exchange group characteristics (like operating channel, WPS configuration, is the group persistent etc.).

Devices can communicate their willingness to become group owner with the GO intent attribute (0-15).

After the role negotiation a confirmation is sent and devices move to their specified roles. GO starts operating in Access point mode.

Page 30: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 30

Wi-Fi direct and android Android’s Wi-Fi P2P framework complies with the Wi-Fi direct

certification program. It allows devices running android 4.0+ (API 14+) with appropriate hardware to connect easily without an intermediate access point.

Android Wi-Fi P2P API consists of: Methods that allows us to discover, request and connect to peers

(android.net.wifi.p2p.WifiP2pManager). Listeners that notifies us of success and failure of WifiP2pmanager’s

method calls. Intents to notify specific events, such as new peer joined, connection

dropped etc. (like WIFI_P2P_PEERS_CHANGED_ACTION and WIFI_P2P_CONNECTION_CHANGED_ACTION)

A single call to WifiP2pManager’s discover peers is enough for Wi-Fi direct peer discovery. Connection however with Wi-Fi direct group requires user’s consent. This is unlike NSD.

Page 31: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 31

Wi-Fi direct – Permissions

Page 32: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 32

Wi-Fi direct – starting peer discovery

Page 33: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 33

Wi-Fi direct – getting peer list

This broadcast receiver needs to be registered dynamically. It notifies when peer list has changed so peer list can be requested from WifiP2pManager

Page 34: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 34

Wi-Fi direct – getting peer list contd…

When WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION is received in the broadcast receiver, peer list can be requested. With WifiP2pManager.requestPeers() method.

Gives a list of all available peers,

not just new ones. So clear old list

and repopulate with new list

Page 35: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 35

Wi-Fi direct – WifiP2pDeviceImportant method in WifiP2pDeviceList is getDeviceList() which gives a collection of WifiP2pDevice, it has the following important fields

Page 36: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 36

Wi-Fi direct – Connecting to a peer

Once a peer is discovered, connection request can be sent

Page 37: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 37

Connecting to a peer – WifiP2pInfo

Once connection is successful a connection info request can be issued via WifiP2pManager. Which send callback to WifiP2pManager.ConnectionInfoListener. It has only one method described below:

WifiP2pInfo

Page 38: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 38

WifiP2pManager - createGroup

This method is to support older devices that do not support Wi-Fi direct. It creates an Access point which non-Wi-Fi direct can connect to (like a regular Wi-Fi), and continue communication.

The device that calls this method must support Wi-Fi direct. The method creates a p2p group with the current device as its group owner. In addition to hosting an access point, it also allows other Wi-Fi direct

enabled devices to connect with it in regular fashion. Connecting with a Wi-Fi programmatically (if SSID and password are

known):

Page 39: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 39

Wi-Fi direct demoPlus code walkthrough for our use case

Page 40: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 40

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 41: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 41

Discover devices and services in vicinity (Irrespective of device connection status)

Wi-Fi direct service discovery

Page 42: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 42

Wi-Fi direct service discovery

Similar function as NSD. Similar code like Wi-Fi direct. Kind of like a combination of both. And also more complex than

BOTH. Same classes and listeners are used as in Wi-Fi direct. Here we find services in nearby devices. (Can work for devices in the

same network too). In this WifiP2pManager’s addLocalService() method is used. There is an additional option for adding key value pairs in a map (100-

200 bytes). This map can be received with service discovery. Any small additional data can be shared with service (like device’s

port it is listening to, name, device info etc.)

Page 43: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 43

Wi-Fi direct adding local service

WifiP2pManager’s addLocalService():

WifiP2pServiceInfo is a class for storing service information that is advertised over a Wi-Fi peer-to-peer setup. It has two direct subclasses. Both are bonjour service info: WifiP2pDnsSdServiceInfo – domain name system service discovery (service

info) WifiP2pUpnpServiceInfo – Universal plug and play (service info)

For our use case we will be using the former in place of WifiP2pServiceInfo. With DNS-SD service info we have an option to advertise additional data (relatively small 100-200 bytes). For this use case we will advertise port information.Once the port and IP information is determined, rest of the work is similar (socket programming).

Page 44: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 44

Setting WifiP2pDnsSdServiceInfo

Page 45: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 45

Wi-Fi direct service discovery request

WifiP2pManager’s addServiceRequest():

Since we are searching for DNS-SD service the WifiP2pServiceRequest will be of NDS-SD type.

Setting service discovery response listeners to WifiP2pManager

Page 46: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 46

Wi-Fi direct service response listeners

WifiP2pManager.DnsSdServiceResponseListener interface has only one method:

WifiP2pManager.DnsSdTxtRecordListener interface:

Other callbacks and broadcasts are same as Wi-Fi direct

Page 47: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 47

Wi-Fi direct… rest of it and more

Rest of the flow in Wi-Fi Direct service discovery is same as Wi-Fi direct. A device will call connect. Other device will receive an invitation to connect. There will be role negotiation based on GO intent parameter. A group will be formed with one group owner and others as clients. After that information can be exchanged.

If we look at our use case the only difference is instead of pre-fixing a port, we are adding the port information when adding service request. So no listeners on pre-fixed port.

All the services should be unregistered and discovery request must be stopped when exiting an application. We don’t want to drain the battery for no reason.

Use createGroup() method discussed in Wi-Fi direct section only when supporting legacy devices.

Page 48: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 48

Wi-Fi direct service discovery demoAnd a glance at some of the important methods and classes in demo code

Page 49: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 49

Agenda Network interfaces. Basic socket communication. Network service discovery. Wi-Fi direct. Wi-Fi direct service discovery. Bluetooth low energy (BLE). Demos (wherever needed)

Page 50: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 50

In AndroidBluetooth Low Energy

Page 51: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 51

In Android, BLE stack was introduced in API 18 Jelly Bean 4.3, in July 2013.

Page 52: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 52

Scene

Peripheral

Page 53: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 53

Scene

Peripheral

CONN_REQ

Connection Object

Central

Page 54: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. 54Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 54

Generic Access Profile Roles

Broadcaster Observer

Page 55: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. 55Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 55

Generic Access Profile Roles

Peripheralaka Slave

Centralaka Master

connect

Page 56: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. 56Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 56

GATT Server-Client

Server(holding data)

Client(asking data)

transact

Page 57: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 57

ServicesCharacteristics

Class {

}

attributes;get();set();

• Read• Write

Characteristics

Page 58: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 58

BluetoothManager manager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = manager.getAdapter();

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

Page 59: Connected World in android - Local data sharing and service discovery

mBluetoothLeScanner.startScan(callback);

mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

<uses-permissionandroid:name= "android.permission.BLUETOOTH_ADMIN" />

Page 60: Connected World in android - Local data sharing and service discovery

ScanCallback{

public void onScanResult(int callbackType, ScanResult result) {}

public void onScanFailed(int errorCode) {}

}

Page 61: Connected World in android - Local data sharing and service discovery

mBluetoothGatt = mBluetoothDevice.connectGatt(

context,autoConnect,

gattCallback) ;

mBluetoothDevice = bleScanResult.getBluetoothDevice();

Page 62: Connected World in android - Local data sharing and service discovery

BluetoothGattCallback {

public void onConnectionStateChange(

BluetoothGatt gatt,int status,int newState) ;

}

if( newState == BluetoothProfile.STATE_CONNECTED)

Page 63: Connected World in android - Local data sharing and service discovery

bluetoothGatt.discoverServices();BluetoothGattCallback {

public void onServicesDiscovered(BluetoothGatt gatt, int status);

}

List<BluetoothGattService> serviceList = gatt.getServices();

Page 64: Connected World in android - Local data sharing and service discovery

bluetoothGattCharacteristic.setValue( intToByteArray(color));

List<BluetoothGattCharacteristic> list =bluetoothGattService.getCharacteristics();

bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic)

Page 65: Connected World in android - Local data sharing and service discovery

BluetoothGattCallback {

public void onCharacteristicWrite(

BluetoothGatt gatt, BluetoothGattCharacteristic

characteristic, int status) ;

}

if( status == BluetoothGatt.GATT_SUCCESS)

Page 66: Connected World in android - Local data sharing and service discovery

Video removed…

Page 67: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. 67Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 67

Demo code available at…

• https://github.com/drulabs/LocalDash

• https://github.com/navalkishoreb/IoT/tree/zone_discovery/Android

Page 68: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved.Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 68

Page 69: Connected World in android - Local data sharing and service discovery

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. 69Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 69

References• https://docs.oracle.com/javase/tutorial/networking/nifs/index.html

• https://docs.oracle.com/javase/7/docs/api/java/net/NetworkInterface.html

• http://www.tldp.org/LDP/nag2/x-087-2-hwconfig.tour.html

• http://adbshell.com/commands/adb-shell-netstat

• https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/

• https://docs.oracle.com/javase/tutorial/networking/sockets/definition.html

• https://developer.android.com/guide/topics/connectivity/wifip2p.html

Page 70: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 70

BLE has 40 channels of 2Mhz bandwidth

Out of which 37 are Data channels

Only 3 are for Advertising data

Page 71: Connected World in android - Local data sharing and service discovery

Copyright ©2016 Talentica Software (I) Pvt Ltd. All rights reserved. 71

GND

+5Volt+3.3 Volt

_Tx _

_Rx_

_Tx_

_Rx_

PIN 6


Recommended