With the boom of IOT, BLE (Bluetooth low energy) and other connected devices and protocols, android app development is no longer limited to basic client server interaction. Android app development now includes interaction with other devices (not necessarily android) in the vicinity, at its very core.
Transferring files with friends without internet, Bluetooth and WiFi; streaming media from your phone or tablet to dumb plain TV (without HDMI cables) and switching off bedroom light with phone have become part of our lives. Let's explore how it's done and where do we need to start to kick start such projects.
In this session we will explore:
• Communication between connected and non-connected android devices.
• BLE overview (Bluetooth Low Energy).
• BLE APIs you should know about.
• WiFi-Direct and P2P.
• WiFi-Direct service discovery.
• Network service discovery (NSD) and relevant demos
Why it is important to know about network interfaces?
Show ifconfig adb command
And java program
Show ifconfig adb command
And java program
Question about port sharing between android devices.
Why port number should not be prefixed?
Define service
serviceInfo.getPort() will not give the port info unless resolved
Service type = “_<protocol>._<transportlayer>”
International Assigned Numbers Authority (IANA) http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
Explain error code = 2 (Wi-Fi is not on or not connected)
Wi-Fi is either off or not connected
Single service discovery method with discovery listener call back
Now after the services are discovered, either show it in UI or resolve it to get entire service information
We will discuss important methods of WifiP2pManager in coming slides
Take a look at WifiP2pDeviceList, WifiP2pDevice & WifiP2pManager
There you go, a list of peer’s is available
This sends a connection request to the peer device. Invitation request to peer device. User has to manually accept the request.
From here onwards we can continue our socket communication.
This is important because it can connect to both Wi-Fi direct and non-Wi-Fi direct legacy devices (those connect to Group owner as regular access point)
The network name starts with “Direct-xy”
This is used in nearby device discovery such as printers and other smart devices.
This has a wide variety of applications. Right from xbox controllers to wireless printers to IOT systems
WifiP2pServiceInfo is a class for storing service information that is advertised over a Wi-Fi peer-to-peer setup.
Universal Plug and Play (UPnP) is a set of networking protocols that permits networked devices, such as personal computers, printers, Internet gateways, Wi-Fi access points and mobile devices to seamlessly discover each other's presence on the network and establish functional network services for data sharing, communications, and entertainment.
UPnP is intended primarily for residential networks without enterprise-class devices
Before we dig deeper into Android. Few things we need to understand about BLE protocol stack
Android introduced BLE stack in Jelly Bean 4.3 <pause> API level 18 <pause><enter>
Though became stable and reliable from Android Lollipop 5.0 <pause> API level 21 <enter>Next we will look into<pause> certain API calls and steps <pause>that android framework specifies<pause> to work with BLE devices.<enter>
http://www.electronicsweekly.com/blogs/eyes-on-android/what-is/what-is-bluedroid-2014-01/
BlueDroid was introduced in 4.2
http://www.argenox.com/blog/android-5-0-lollipop-brings-ble-improvements/
The story of Android’s Bluetooth support is long. Android originally used the open source BlueZ stack that is common in Linux. But Google removed the bluez stack that was originally in Android because of its GPL license. Instead it decided to develop its own BlueDroid Bluetooth stack with Broadcom.
BlueZ is GPL
BlueDroid Apache License
Improved Scanning
Scanning, and in particular background scanning, was a big problem in Android for two reasons. The first is that scanning drains a battery tremendously. BLE scanning in Android was stopping the device from going into standby mode and would quickly drain batteries. The normal reaction of users was to disable Bluetooth to save on battery life, but this caused the phone to lose connection with BLE devices. The end result was that communicating with BLE enabled devices and getting data would take much longer, frustrating users.
With Android L, the scanning process has been offloaded to lower layers which allows the smartphone to go to sleep. Users don’t have to disable Bluetooth when not using it actively.
Support for BLE Peripheral and Broadcaster Modes
Till 4.4 android framework supported only central mode.
Device will scan for near by peripheral to connect
Device will scan for near by peripheral to connect
Central will initiate connection
Peripheral will respond to accept
A device could be a broadcaster or an observer.
A device could be a broadcaster or an observer.
Server is hosting services .
Each service can have one or more characteristics
And Each characteristic could read and written.Analogy is similar to a Class its attributes which can be get or set
get BluetoothManager<enter>
from application context using getSystemService method.
To ensure your application has proper device compatibility <enter>.
Declare ble hardware feature requirement in manifest file<pause><enter>
Grab BluetoothAdapter from the manager object.<enter>
The purpose of a <uses-feature> declaration is to inform any external entity of the set of hardware and software features on which your application depends. The element offers a requiredattribute that lets you specify whether your application requires and cannot function without the declared feature, or whether it prefers to have the feature but can function without it.
BluetoothManager
High level manager used to obtain an instance of an BluetoothAdapter and to conduct overall Bluetooth Management.
The BluetoothAdapter is required for any and all Bluetooth activity.
luetoothManager what does it manages??
BluetoothAdapter purpose??
get BluetoothLeScanner<enter> from BluetoothAdapter<pause>BluetoothLeScanner<pause> provides methods<pause> to perform scan related operations <pause>for Bluetooth LE devicesNow execute startScan<enter> method It will run asynchronously
Provide a callback object to receive scan results..
To perform device discovery get hold of bluetooth admin permission
Note: <pause>To enable device discovery <enter> get hold of Bluetooth_Admin permission<enter>
If you want your app to initiate device discovery or manipulate Bluetooth settings, you must also declare theBLUETOOTH_ADMIN permission. Note: If you use the BLUETOOTH_ADMIN permission, then you must also have theBLUETOOTH permission.
Allows applications to discover and pair bluetooth devices.
callbackType
Determines how this callback was triggered. Could be one of
* {@link ScanSettings#CALLBACK_TYPE_ALL_MATCHES},
* {@link ScanSettings#CALLBACK_TYPE_FIRST_MATCH} or
* {@link ScanSettings#CALLBACK_TYPE_MATCH_LOST}
Bluetooth_admin what doors does this permission opens?? What do we need this particular permission
After a successful device discovery You will receive ScanResult <enter> in your ScanCallback implementation.<pause>
ScanResult <enter> contains ble device information
ScanResult
Device BluetoothDevice: Remote bluetooth device that is found.
scanRecord ScanRecord: Scan record including both advertising data and scan response data.
Rssi int: Received signal strength.
timestampNanos long: Device timestamp when the scan result was observed.
http://www.metageek.com/training/resources/understanding-rssi.html
callbackType
Determines how this callback was triggered. Could be one of
* {@link ScanSettings#CALLBACK_TYPE_ALL_MATCHES},
* {@link ScanSettings#CALLBACK_TYPE_FIRST_MATCH} o
* {@link ScanSettings#CALLBACK_TYPE_MATCH_LOST}???
How rssi calculates??
ScanResult contains what??
And what errorCode could be produced??
When you have multiple ble devices you will feed your list adapter with scan result and add on the basis of uuid
get bluetooth device <enter> from scan result.
We will connect to the ble device, which is at this moment is acting as a GATT server. <enter>
connectGatt method returns BluetoothGatt object.<pause>
mBluetoothGatt is used to conduct GATT <loud>client operations.
To automatically connect as soon as the remote device becomes available pass autoConnect true<pause>
GattCallback will receive connection related responses.<pause>
optional
while waiting for callback Show intermediate progress bar.
Optional
It could be understood as mysql driver connection object is used to communicate mysql database server.
BluetoothGatt functionality to enable communication with Bluetooth Smart or Smart Ready devices.
/**
* Connect to GATT Server hosted by this device. Caller acts as GATT client.
* The callback is used to deliver results to Caller, such as connection status as well
* as any further GATT client operations.
* The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
* GATT client operations.
* @param callback GATT callback handler that will receive asynchronous callbacks.
* @param autoConnect Whether to directly connect to the remote device (false)
* or to automatically connect as soon as the remote
* device becomes available (true).
* @throws IllegalArgumentException if callback is null
*/
Whatever happens to connection is communicated <enter> through onConnectionStateChange callback
Check if it was connected <enter> successfully
Gatt is the connection object
Status is like 200ok where operation was successfull
newState tells our current connection state
/**
* Callback indicating when GATT client has connected/disconnected to/from a remote
* GATT server.
*
* @param gatt GATT client
* @param status Status of the connect or disconnect operation.
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
* @param newState Returns the new connection state. Can be one of
* {@link BluetoothProfile#STATE_DISCONNECTED} or
* {@link BluetoothProfile#STATE_CONNECTED}
*/
Note this would be called in separate thread
Once we are connected successfully to GATT server , we will <enter> discover services.
When service discovery is completed <enter> onServicesDiscovered is called in GattCallback implementation
Retrieve services <enter> and obtain desired service using particular UUID<enter>
Once service discovery is completed,
* the {@link BluetoothGattCallback#onServicesDiscovered} callback is
* triggered.
/**
* Callback indicating when GATT client has connected/disconnected to/from a remote
* GATT server.
*
* @param gatt GATT client
* @param status Status of the connect or disconnect operation.
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
* @param newState Returns the new connection state. Can be one of
* {@link BluetoothProfile#STATE_DISCONNECTED} or
* {@link BluetoothProfile#STATE_CONNECTED}
*/
Note this would be called in separate thread
What is ble service?? Example??
A particular service may hold multiple characteristics<enter><pause>
Retrieve desired characteristic using UUID.
Now set color value<enter> by moving seekbar on application.
And write <enter> this characteristic onto server.
/**
* Callback indicating when GATT client has connected/disconnected to/from a remote
* GATT server.
*
* @param gatt GATT client
* @param status Status of the connect or disconnect operation.
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
* @param newState Returns the new connection state. Can be one of
* {@link BluetoothProfile#STATE_DISCONNECTED} or
* {@link BluetoothProfile#STATE_CONNECTED}
*/
Note this would be called in separate thread
We will receive A callback<enter> onCharacteristicWrite
And <enter> if write is Successful <enter> we will see Led strip changed its color
/**
* Callback indicating the result of a characteristic write operation.
*
* <p>If this callback is invoked while a reliable write transaction is
* in progress, the value of the characteristic represents the value
* reported by the remote device. An application should compare this
* value to the desired value to be written. If the values don't match,
* the application must abort the reliable write transaction.
*
* @param gatt GATT client invoked {@link BluetoothGatt#writeCharacteristic}
* @param characteristic Characteristic that was written to the associated
* remote device.
* @param status The result of the write operation
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
*/
Now we will make few <loud>connections step by step<pause><enter>
Take arduino uno<enter>
Take breadboard<enter>
Take Led strip<enter>
Now connect 5Volt output from arduino to led strip power supply pin<enter>
Connect ground pin from arduino to breadboard<enter>
Connect Pin 6 from arduino to led strip Data input pin<enter>
Take HM-10 Module , connect the ground wire with breadboad <enter>
Now connect 3.3Volt output from arduino to power pin supply of HM -10<enter>
Connect Pin 10 the Receiving end of arduino <enter> to Transmitting end of HM-10 <enter>
Connect Pin 11 the Transmitting end of arduino to Receiving pin of HM -10