SlideShare a Scribd company logo
1 of 27
Hack your Business with 
android and beacons 
Tushar Choudhary 
Mobile consultant, Xebia
Agenda 
Beacons- What, Why & How? 
Communication between Android & Beacons 
Android Libraries 
Android code- How to make an app with beacons 
Case Study- Employee Tracker 
Technical Pitfalls
Beacons : What are they? How they 
work? Why are they important ?
What are Beacons ? 
• Beacons are small piece of hardware 
devices that can emit and receive BLE 
signals.
Beacons
Why are they so important? 
• Broadcasting range 
• proximity
Communication b/w Android & 
Beacons
Communication between Android & 
Beacons 
• Bluetooth Low Energy (BLE)
Using BLE in Android 
•Permissions 
<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name=“android.permission.BLUETOOTH_ADMIN"/> 
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> 
// Use this check to determine whether BLE is supported on the device. Then 
// you can selectively disable BLE-related features. 
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); 
finish(); 
}
Using BLE in Android 
•Setting Up BLE 
// Initializes Bluetooth adapter. 
final BluetoothManager bluetoothManager = 
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
mBluetoothAdapter = bluetoothManager.getAdapter(); 
private BluetoothAdapter mBluetoothAdapter; 
... 
// Ensures Bluetooth is available on the device and it is enabled. If not, 
// displays a dialog requesting user permission to enable Bluetooth. 
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { 
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
}
Using BLE in Android 
• Finding BLE Devices 
/** 
* Activity for scanning and displaying available BLE devices. 
*/ 
public class DeviceScanActivity extends ListActivity { 
private BluetoothAdapter mBluetoothAdapter; 
private boolean mScanning; 
private Handler mHandler; 
// Stops scanning after 10 seconds. 
private static final long SCAN_PERIOD = 10000; 
... 
private void scanLeDevice(final boolean enable) { 
if (enable) { 
// Stops scanning after a pre-defined scan period. 
mHandler.postDelayed(new Runnable() { 
@Override 
public void run() { 
mScanning = false; 
mBluetoothAdapter.stopLeScan(mLeScanCallback); 
} 
}, SCAN_PERIOD); 
mScanning = true; 
mBluetoothAdapter.startLeScan(mLeScanCallback); 
} else { 
mScanning = false; 
mBluetoothAdapter.stopLeScan(mLeScanCallback); 
} 
...
Using BLE in Android 
• Connecting to a GATT Server 
/ A service that interacts with the BLE device via the Android BLE API. 
public class BluetoothLeService extends Service { 
// Various callback methods defined by the BLE API. 
private final BluetoothGattCallback mGattCallback = 
private final static String TAG = BluetoothLeService.class.getSimpleName(); 
private BluetoothManager mBluetoothManager; 
private BluetoothAdapter mBluetoothAdapter; 
private String mBluetoothDeviceAddress; 
private BluetoothGatt mBluetoothGatt; 
private int mConnectionState = STATE_DISCONNECTED; 
private static final int STATE_DISCONNECTED = 0; 
private static final int STATE_CONNECTING = 1; 
private static final int STATE_CONNECTED = 2; 
public final static String ACTION_GATT_CONNECTED = 
"com.example.bluetooth.le.ACTION_GATT_CONNECTED"; 
public final static String ACTION_GATT_DISCONNECTED = 
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; 
public final static String ACTION_GATT_SERVICES_DISCOVERED = 
"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"; 
public final static String ACTION_DATA_AVAILABLE = 
"com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; 
public final static String EXTRA_DATA = 
"com.example.bluetooth.le.EXTRA_DATA"; 
public final static UUID UUID_HEART_RATE_MEASUREMENT = 
UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT); 
new BluetoothGattCallback() { 
@Override 
public void onConnectionStateChange(BluetoothGatt gatt, int status, 
int newState) { 
String intentAction; 
if (newState == BluetoothProfile.STATE_CONNECTED) { 
intentAction = ACTION_GATT_CONNECTED; 
mConnectionState = STATE_CONNECTED; 
broadcastUpdate(intentAction); 
Log.i(TAG, "Connected to GATT server."); 
Log.i(TAG, "Attempting to start service discovery:" + 
mBluetoothGatt.discoverServices()); 
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
intentAction = ACTION_GATT_DISCONNECTED; 
mConnectionState = STATE_DISCONNECTED; 
Log.i(TAG, "Disconnected from GATT server."); 
broadcastUpdate(intentAction); 
} 
} 
@Override 
// New services discovered 
public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
if (status == BluetoothGatt.GATT_SUCCESS) { 
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 
} else { 
Log.w(TAG, "onServicesDiscovered received: " + status); 
} 
} 
@Override 
// Result of a characteristic read operation 
public void onCharacteristicRead(BluetoothGatt gatt, 
BluetoothGattCharacteristic characteristic, 
int status) { 
if (status == BluetoothGatt.GATT_SUCCESS) { 
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
} 
} 
... 
}; 
... 
}
Using BLE in Android 
• Reading BLE attributes 
public class DeviceControlActivity extends Activity { 
... 
// Demonstrates how to iterate through the supported GATT 
// Services/Characteristics. 
// In this sample, we populate the data structure that is bound to the 
// ExpandableListView on the UI. 
private void displayGattServices(List<BluetoothGattService> gattServices) { 
if (gattServices == null) return; 
String uuid = null; 
String unknownServiceString = getResources(). 
getString(R.string.unknown_service); 
String unknownCharaString = getResources(). 
getString(R.string.unknown_characteristic); 
ArrayList<HashMap<String, String>> gattServiceData = 
new ArrayList<HashMap<String, String>>(); 
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData 
= new ArrayList<ArrayList<HashMap<String, String>>>(); 
mGattCharacteristics = 
new ArrayList<ArrayList<BluetoothGattCharacteristic>>(); 
// Loops through available GATT Services. 
for (BluetoothGattService gattService : gattServices) { 
HashMap<String, String> currentServiceData = 
new HashMap<String, String>(); 
uuid = gattService.getUuid().toString(); 
currentServiceData.put( 
LIST_NAME, SampleGattAttributes. 
lookup(uuid, unknownServiceString)); 
currentServiceData.put(LIST_UUID, uuid); 
gattServiceData.add(currentServiceData); 
ArrayList<HashMap<String, String>> 
gattCharacteristicGroupData = 
new ArrayList<HashMap<String, String>>(); 
List<BluetoothGattCharacteristic> gattCharacteristics = 
gattService.getCharacteristics(); 
ArrayList<BluetoothGattCharacteristic> charas = 
new ArrayList<BluetoothGattCharacteristic>(); 
// Loops through available Characteristics. 
for (BluetoothGattCharacteristic gattCharacteristic : 
gattCharacteristics) { 
charas.add(gattCharacteristic); 
HashMap<String, String> currentCharaData = 
new HashMap<String, String>(); 
uuid = gattCharacteristic.getUuid().toString(); 
currentCharaData.put( 
LIST_NAME, SampleGattAttributes.lookup(uuid, 
unknownCharaString)); 
currentCharaData.put(LIST_UUID, uuid); 
gattCharacteristicGroupData.add(currentCharaData); 
} 
mGattCharacteristics.add(charas); 
gattCharacteristicData.add(gattCharacteristicGroupData); 
} 
... 
} 
... 
}
Beacons/BLE : How They Work ? 
• BLE enabled devices or Beacons can 
transmits small packets of data. 
• This wakes up a “listener/receiver” to let it 
know that it’s there. 
• Which then lets you calculate the proximity 
to the Beacon and show relevant 
information to visitors.
Android Libraries
Android Beacons & Support Libraries 
Android Beacon Library: 
https://github.com/AltBeacon/android-beacon-library-reference 
Smart Gatt Library: 
https://github.com/movisens/SmartGattLib 
Bluetooth LE Library Android: 
https://github.com/alt236/Bluetooth-LE-Library---Android
Android code- How to make an 
app with beacons
Building the Android App 
1. Implement a broadcast receiver to catch 
broadcasts for turning bluetooth on and off. 
2. Start a service when bluetooth turns on. 
3. Stop service when bluetooth turns off 
4. Register a listener to listen for nearby beacon 
devices
Implementing Broadcast Receiver 
@Override 
public void onReceive(Context context, Intent intent) { 
// TODO Auto-generated method stub 
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { 
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); 
Intent i; 
switch (state) { 
case BluetoothAdapter.STATE_OFF: 
if (MyBeaconService.isStarted) { 
i = new Intent(context, 
MyBeaconIntentService.class); 
i.putExtra("StartBeaconService", false); 
context.stopService(i); 
} 
Log.i(TAG, "Bluetooth State : OFF"); 
break; 
case BluetoothAdapter.STATE_ON: 
Log.i(TAG, "Bluetooth State : ON"); 
if (!MyBeaconService.isStarted) { 
i = new Intent(context, 
MyBeaconIntentService.class); 
i.putExtra("StartBeaconService", true); 
context.startService(i); 
} 
break; 
} 
} 
}
Registering Ranging Listener in 
Service’s onCreate() method 
@Override 
public void onCreate() { 
super.onCreate(); 
beaconManager.setMonitoringListener(new 
BeaconManager.MonitoringListener() { 
@Override 
public void onExitedRegion(Region arg0) { 
// TODO Auto-generated method stub 
Util.postNotification(getBaseContext(), 
"Alert Luggage Tracker", 
notificationManager, 1, 
AllDemosActivity.class); 
} 
@Override 
public void onEnteredRegion(Region arg0, List<Beacon> arg1) 
{ 
// TODO Auto-generated method stub 
}}); 
}
Connecting to BeaconManager’s Service 
in Service’s onStartCommand() 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
beaconManager.connect(new BeaconManager.ServiceReadyCallback() { 
@Override 
public void onServiceReady() { 
try { 
beaconManager.startMonitoring(BEACONS); 
} catch (RemoteException e) { 
Log.e(TAG, "Cannot start ranging", e); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
}); 
return START_NOT_STICKY; 
}
Case Study
Xebia Employee Tracker 
• Xebia Employee tracker maps real time positions of 
my company’s employees on a 2d map 
• Any employee at any time can see position of other 
colleagues on the map 
• Cool data analytics are applied to show interesting 
results such as- max crowd time in cafeteria in a day, 
An employee’s location heat map for a day :D
Xebia Employee Tracker 
{{ Show Case Xebia Employee Tracker }} 
{{ Show Source Code }}
Technical Pitfalls
Technical pitfalls of Android + Beacons ? 
• Bluetooth must be turned on. 
• Your app must be installed on users devices. 
• You can't trigger mobile beacon or iBeacon to trigger 
installation of an app. 
• Location services must be enabled for the specific 
application. 
• To download real data we must have an app 
connected via internet connection.
Thank You!

More Related Content

Similar to Android & Beacons

Assistive Technology_Research
Assistive Technology_ResearchAssistive Technology_Research
Assistive Technology_ResearchMeng Kry
 
A brief overview of BLE on Android
A brief overview of BLE on AndroidA brief overview of BLE on Android
A brief overview of BLE on AndroidLuka Bašek
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)Google
 
JSR 82 (bluetooth obex)
JSR 82 (bluetooth obex)JSR 82 (bluetooth obex)
JSR 82 (bluetooth obex)SMIJava
 
MCE^3 - Dariusz Seweryn, Paweł Urban - Demystifying Android's Bluetooth Low ...
MCE^3 - Dariusz Seweryn, Paweł Urban -  Demystifying Android's Bluetooth Low ...MCE^3 - Dariusz Seweryn, Paweł Urban -  Demystifying Android's Bluetooth Low ...
MCE^3 - Dariusz Seweryn, Paweł Urban - Demystifying Android's Bluetooth Low ...PROIDEA
 
Demystifying Android's Bluetooth Low Energy at MCE^3 Conf
Demystifying Android's Bluetooth Low Energy at MCE^3 ConfDemystifying Android's Bluetooth Low Energy at MCE^3 Conf
Demystifying Android's Bluetooth Low Energy at MCE^3 ConfPawel Urban
 
Android Wi-Fi Manager and Bluetooth Connection
Android Wi-Fi Manager and Bluetooth ConnectionAndroid Wi-Fi Manager and Bluetooth Connection
Android Wi-Fi Manager and Bluetooth ConnectionJussi Pohjolainen
 
Fiware IoT Proposal & Community
Fiware IoT Proposal & Community Fiware IoT Proposal & Community
Fiware IoT Proposal & Community TIDChile
 
Mini project final presentation
Mini project final presentationMini project final presentation
Mini project final presentationGianlucaCapozzi1
 
What's new in Android P @ I/O Extended Bangkok 2018
What's new in Android P @ I/O Extended Bangkok 2018What's new in Android P @ I/O Extended Bangkok 2018
What's new in Android P @ I/O Extended Bangkok 2018Somkiat Khitwongwattana
 
Mobile Development for Devices and IoT
Mobile Development for Devices and IoTMobile Development for Devices and IoT
Mobile Development for Devices and IoTWes Bailey
 
Google Wave API: Now and Beyond
Google Wave API: Now and BeyondGoogle Wave API: Now and Beyond
Google Wave API: Now and BeyondMarakana Inc.
 
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇Johnny Sung
 
Android Workshop
Android WorkshopAndroid Workshop
Android WorkshopJunda Ong
 
Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)Luc Bors
 
Easy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lotEasy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lotDaniele Davoli
 
HyWAI Web Bluetooth API
HyWAI Web Bluetooth APIHyWAI Web Bluetooth API
HyWAI Web Bluetooth APIJonathan Jeon
 

Similar to Android & Beacons (20)

Eddystone beacons demo
Eddystone beacons demoEddystone beacons demo
Eddystone beacons demo
 
Assistive Technology_Research
Assistive Technology_ResearchAssistive Technology_Research
Assistive Technology_Research
 
A brief overview of BLE on Android
A brief overview of BLE on AndroidA brief overview of BLE on Android
A brief overview of BLE on Android
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)
 
JSR 82 (bluetooth obex)
JSR 82 (bluetooth obex)JSR 82 (bluetooth obex)
JSR 82 (bluetooth obex)
 
MCE^3 - Dariusz Seweryn, Paweł Urban - Demystifying Android's Bluetooth Low ...
MCE^3 - Dariusz Seweryn, Paweł Urban -  Demystifying Android's Bluetooth Low ...MCE^3 - Dariusz Seweryn, Paweł Urban -  Demystifying Android's Bluetooth Low ...
MCE^3 - Dariusz Seweryn, Paweł Urban - Demystifying Android's Bluetooth Low ...
 
Demystifying Android's Bluetooth Low Energy at MCE^3 Conf
Demystifying Android's Bluetooth Low Energy at MCE^3 ConfDemystifying Android's Bluetooth Low Energy at MCE^3 Conf
Demystifying Android's Bluetooth Low Energy at MCE^3 Conf
 
Android Wi-Fi Manager and Bluetooth Connection
Android Wi-Fi Manager and Bluetooth ConnectionAndroid Wi-Fi Manager and Bluetooth Connection
Android Wi-Fi Manager and Bluetooth Connection
 
Fiware IoT Proposal & Community
Fiware IoT Proposal & Community Fiware IoT Proposal & Community
Fiware IoT Proposal & Community
 
Mini project final presentation
Mini project final presentationMini project final presentation
Mini project final presentation
 
What's new in Android P @ I/O Extended Bangkok 2018
What's new in Android P @ I/O Extended Bangkok 2018What's new in Android P @ I/O Extended Bangkok 2018
What's new in Android P @ I/O Extended Bangkok 2018
 
Mobile Development for Devices and IoT
Mobile Development for Devices and IoTMobile Development for Devices and IoT
Mobile Development for Devices and IoT
 
Google Wave API: Now and Beyond
Google Wave API: Now and BeyondGoogle Wave API: Now and Beyond
Google Wave API: Now and Beyond
 
Integrando sua app Android com Chromecast
Integrando sua app Android com ChromecastIntegrando sua app Android com Chromecast
Integrando sua app Android com Chromecast
 
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
 
Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)
 
Easy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lotEasy2park - A smarter way to find a parking lot
Easy2park - A smarter way to find a parking lot
 
HyWAI Web Bluetooth API
HyWAI Web Bluetooth APIHyWAI Web Bluetooth API
HyWAI Web Bluetooth API
 
Developing in android
Developing in androidDeveloping in android
Developing in android
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Recently uploaded (20)

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

Android & Beacons

  • 1. Hack your Business with android and beacons Tushar Choudhary Mobile consultant, Xebia
  • 2. Agenda Beacons- What, Why & How? Communication between Android & Beacons Android Libraries Android code- How to make an app with beacons Case Study- Employee Tracker Technical Pitfalls
  • 3. Beacons : What are they? How they work? Why are they important ?
  • 4. What are Beacons ? • Beacons are small piece of hardware devices that can emit and receive BLE signals.
  • 6. Why are they so important? • Broadcasting range • proximity
  • 8. Communication between Android & Beacons • Bluetooth Low Energy (BLE)
  • 9. Using BLE in Android •Permissions <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name=“android.permission.BLUETOOTH_ADMIN"/> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> // Use this check to determine whether BLE is supported on the device. Then // you can selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); }
  • 10. Using BLE in Android •Setting Up BLE // Initializes Bluetooth adapter. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); private BluetoothAdapter mBluetoothAdapter; ... // Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
  • 11. Using BLE in Android • Finding BLE Devices /** * Activity for scanning and displaying available BLE devices. */ public class DeviceScanActivity extends ListActivity { private BluetoothAdapter mBluetoothAdapter; private boolean mScanning; private Handler mHandler; // Stops scanning after 10 seconds. private static final long SCAN_PERIOD = 10000; ... private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; mBluetoothAdapter.startLeScan(mLeScanCallback); } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } ...
  • 12. Using BLE in Android • Connecting to a GATT Server / A service that interacts with the BLE device via the Android BLE API. public class BluetoothLeService extends Service { // Various callback methods defined by the BLE API. private final BluetoothGattCallback mGattCallback = private final static String TAG = BluetoothLeService.class.getSimpleName(); private BluetoothManager mBluetoothManager; private BluetoothAdapter mBluetoothAdapter; private String mBluetoothDeviceAddress; private BluetoothGatt mBluetoothGatt; private int mConnectionState = STATE_DISCONNECTED; private static final int STATE_DISCONNECTED = 0; private static final int STATE_CONNECTING = 1; private static final int STATE_CONNECTED = 2; public final static String ACTION_GATT_CONNECTED = "com.example.bluetooth.le.ACTION_GATT_CONNECTED"; public final static String ACTION_GATT_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"; public final static String ACTION_GATT_SERVICES_DISCOVERED = "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"; public final static String ACTION_DATA_AVAILABLE = "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; public final static String EXTRA_DATA = "com.example.bluetooth.le.EXTRA_DATA"; public final static UUID UUID_HEART_RATE_MEASUREMENT = UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT); new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction); } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } } ... }; ... }
  • 13. Using BLE in Android • Reading BLE attributes public class DeviceControlActivity extends Activity { ... // Demonstrates how to iterate through the supported GATT // Services/Characteristics. // In this sample, we populate the data structure that is bound to the // ExpandableListView on the UI. private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; String uuid = null; String unknownServiceString = getResources(). getString(R.string.unknown_service); String unknownCharaString = getResources(). getString(R.string.unknown_characteristic); ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>(); ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<ArrayList<HashMap<String, String>>>(); mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>(); // Loops through available GATT Services. for (BluetoothGattService gattService : gattServices) { HashMap<String, String> currentServiceData = new HashMap<String, String>(); uuid = gattService.getUuid().toString(); currentServiceData.put( LIST_NAME, SampleGattAttributes. lookup(uuid, unknownServiceString)); currentServiceData.put(LIST_UUID, uuid); gattServiceData.add(currentServiceData); ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<HashMap<String, String>>(); List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>(); // Loops through available Characteristics. for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { charas.add(gattCharacteristic); HashMap<String, String> currentCharaData = new HashMap<String, String>(); uuid = gattCharacteristic.getUuid().toString(); currentCharaData.put( LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString)); currentCharaData.put(LIST_UUID, uuid); gattCharacteristicGroupData.add(currentCharaData); } mGattCharacteristics.add(charas); gattCharacteristicData.add(gattCharacteristicGroupData); } ... } ... }
  • 14. Beacons/BLE : How They Work ? • BLE enabled devices or Beacons can transmits small packets of data. • This wakes up a “listener/receiver” to let it know that it’s there. • Which then lets you calculate the proximity to the Beacon and show relevant information to visitors.
  • 16. Android Beacons & Support Libraries Android Beacon Library: https://github.com/AltBeacon/android-beacon-library-reference Smart Gatt Library: https://github.com/movisens/SmartGattLib Bluetooth LE Library Android: https://github.com/alt236/Bluetooth-LE-Library---Android
  • 17. Android code- How to make an app with beacons
  • 18. Building the Android App 1. Implement a broadcast receiver to catch broadcasts for turning bluetooth on and off. 2. Start a service when bluetooth turns on. 3. Stop service when bluetooth turns off 4. Register a listener to listen for nearby beacon devices
  • 19. Implementing Broadcast Receiver @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); Intent i; switch (state) { case BluetoothAdapter.STATE_OFF: if (MyBeaconService.isStarted) { i = new Intent(context, MyBeaconIntentService.class); i.putExtra("StartBeaconService", false); context.stopService(i); } Log.i(TAG, "Bluetooth State : OFF"); break; case BluetoothAdapter.STATE_ON: Log.i(TAG, "Bluetooth State : ON"); if (!MyBeaconService.isStarted) { i = new Intent(context, MyBeaconIntentService.class); i.putExtra("StartBeaconService", true); context.startService(i); } break; } } }
  • 20. Registering Ranging Listener in Service’s onCreate() method @Override public void onCreate() { super.onCreate(); beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() { @Override public void onExitedRegion(Region arg0) { // TODO Auto-generated method stub Util.postNotification(getBaseContext(), "Alert Luggage Tracker", notificationManager, 1, AllDemosActivity.class); } @Override public void onEnteredRegion(Region arg0, List<Beacon> arg1) { // TODO Auto-generated method stub }}); }
  • 21. Connecting to BeaconManager’s Service in Service’s onStartCommand() @Override public int onStartCommand(Intent intent, int flags, int startId) { beaconManager.connect(new BeaconManager.ServiceReadyCallback() { @Override public void onServiceReady() { try { beaconManager.startMonitoring(BEACONS); } catch (RemoteException e) { Log.e(TAG, "Cannot start ranging", e); } catch (Exception e) { e.printStackTrace(); } } }); return START_NOT_STICKY; }
  • 23. Xebia Employee Tracker • Xebia Employee tracker maps real time positions of my company’s employees on a 2d map • Any employee at any time can see position of other colleagues on the map • Cool data analytics are applied to show interesting results such as- max crowd time in cafeteria in a day, An employee’s location heat map for a day :D
  • 24. Xebia Employee Tracker {{ Show Case Xebia Employee Tracker }} {{ Show Source Code }}
  • 26. Technical pitfalls of Android + Beacons ? • Bluetooth must be turned on. • Your app must be installed on users devices. • You can't trigger mobile beacon or iBeacon to trigger installation of an app. • Location services must be enabled for the specific application. • To download real data we must have an app connected via internet connection.