Bluetooth. Mobila applikationer och trådlösa nät HI /3/2013. Lecturer: Anders Lindström,

Similar documents
Android Networking and Connec1vity

App Development for Smart Devices. Lec #16: Networking

Object-Oriented Databases Object-Relational Mappings and Frameworks. Alexandre de Spindler Department of Computer Science

CS434/534: Topics in Networked (Networking) Systems

32. And this is an example on how to retrieve the messages received through NFC.

private static String TAG = BluetoothUtils.class.getSimpleName();

ENRNG3076 : Oral presentation BEng Computer and Communications Engineering

Bluetooth: Short-range Wireless Communication

Android Connectivity & Google APIs

Mobile Application (Design and) Development

Socket 101 Excerpt from Network Programming

Distributed Programming - Sockets

Bluetooth. Bluetooth Radio

NFC is the double click in the internet of the things

Near Field Comunications

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Linux NFC Subsystem. Lauro Ramos Venancio Samuel Ortiz 2011, September 9th

Bluetooth low energy technology Bluegiga Technologies

Covers Android 2 IN ACTION SECOND EDITION. W. Frank Ableson Robi Sen Chris King MANNING

NEAR FIELD COMMUNICATION - THE FUTURE TECHNOLOGY FOR AN INTERACTIVE WORLD

When is Bluetooth not Bluetooth?

protocols September 15,

[A SHORT REPORT ON BLUETOOTH TECHNOLOGY]

Bluetooth Demystified

Distributed Systems Recitation 2. Tamim Jabban

Computer Networks II Advanced Features (T )

MMI 2: Mobile Human- Computer Interaction Mobile Communication

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Wireless Sensor Networks BLUETOOTH LOW ENERGY. Flavia Martelli

BITalino Java Application Programming Interface. Documentation Android API

Java Support for developing TCP Network Based Programs

Distributed Systems Recitation 2. Tamim Jabban

Mobile Security Fall 2014

Bluetooth LE 4.0 and 4.1 (BLE)

INTERFACING A SENSORY MICROCONTROLLER WITH AN ANDROID SMARTPHONE

CS263: Wireless Communications and Sensor Networks

ACR1255U-J1. Secure Bluetooth NFC Reader. User Manual V1.02. Subject to change without prior notice.

Multi Users Text Communication (with new ideas for more utilization) using Android Smartphone Bluetooth Connection

Prepaid Energy System

COMP327 Mobile Computing Session: Lecture Set 6 - The Internet of Things

PM0257. BlueNRG-1, BlueNRG-2 BLE stack v2.x programming guidelines. Programming manual. Introduction

Lecture 20: Future trends in mobile computing. Mythili Vutukuru CS 653 Spring 2014 April 7, Monday

Bluetooth. The Bluetooth Vision. Universal Wireless Connectivity. Universal Wireless Connectivity

ACR1252U. NFC Forum Certified Reader. Technical Specifications V1.03. Subject to change without prior notice.

Bluetooth. Quote of the Day. "I don't have to be careful, I've got a gun. -Homer Simpson. Stephen Carter March 19, 2002

Current Benefits and Future Directions of NFC Services

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

CS 351 Design of Large Programs Sockets Example

Arduino meets Android Creating Applications with Bluetooth, Orientation Sensor, Servo, and LCD

Leveraging the full potential of NFC to reinvent physical access control. Friday seminar,

Secure Elements 101. Sree Swaminathan Director Product Development, First Data

EMBEDDED SYSTEMS PROGRAMMING Accessing Hardware

EE579: Annavaram & Krishnamachari. Bluetooth Communication. Basics. Network Stack. Network Topology

Bluetooth mobile solutions APPLICATION NOTE / FAQ. Page 1 on 24

Current Benefits and Future Directions of NFC Services

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

12/2/09. Mobile and Ubiquitous Computing. Bluetooth Networking" George Roussos! Bluetooth Overview"

Software Engineering Large Practical: Accessing remote data and XML parsing. Stephen Gilmore School of Informatics October 8, 2017

NEAR FIELD COMMUNICATION

D220 - User Manual mypos Europe Ltd. mypos Mini Ice En

Inside Bluetooth Low Energy

HOW TO INTEGRATE NFC CONTROLLERS IN LINUX

Alpha Scanner Pro User manual

Android OA Android Application Engineer(R) Certifications Basic.

Guide to Wireless Communications, 3 rd Edition. Objectives

SunmiPrinter Developer documentation

Attacks on NFC enabled phones and their countermeasures

Energy Efficient Mobile Compu4ng Building low power sensing devices with Bluetooth low energy. Simo Veikkolainen Nokia May 2014

Bluetooth PCI Adapter

CQ Beacon Android SDK V2.0.1

BlueSerial. Bluetooth Serial RS232 Port Adapters. User Manual HANTZ + PARTNER. The Upgrade Company!

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

Bluetooth modules. Modules and turnkey solutions with embedded Bluetooth firmware and AT command

A Proposed e-payment Service for Visually Disabled

Lab 10: Sockets 12:00 PM, Apr 4, 2018

SMART Technologies. Introducing bluetooth low energy and ibeacon

By Ambuj Varshney & Akshat Logar

ART Demo Application for Mobile Phones

ECE 435 Network Engineering Lecture 8

Index. Cambridge University Press Bluetooth Essentials for Programmers Albert S. Huang and Larry Rudolph. Index.

mypos Mini - User Manual mypos Europe Ltd. mypos Mini En

DEFCON 26 - Playing with RFID. by Vanhoecke Vinnie

MOBILE COMPUTING. Jan-May,2012. ALAK ROY. Assistant Professor Dept. of CSE NIT Agartala.

DASH7 ALLIANCE PROTOCOL - WHERE RFID MEETS WSN. public

Design Document. Computer Science Department. Texas Christian University. Date: May 5, 2014

Join the forward thinkers who rely on Toshiba for wireless connectivity ICs.

WPAN/WBANs: ZigBee. Dmitri A. Moltchanov kurssit/elt-53306/

NFC DESIGN FOR ATTENDANCE SYSTEM IN THE UNIVERSITY

Wireless Personal Area Networks

Internet Technology 2/7/2013

Dotstack Porting Guide.

HAKI-NFC BASED ANDROID APPLICATION

Tracing Bluetooth Headsets with the CATC Bluetooth Analysers

Network Communications Standards. Applied Information Technology

Chapter 2 Applications and

ENVIRONMENTAL SENSING PROFILE

Product Specification

Smart Card meets Connectivity New Opportunities in Mobile Business with NFC Technology. Smart Card Alliance2005 Fall Annual Conference Martin Bührlen

CS4/MSc Computer Networking. Lecture 13: Personal Area Networks Bluetooth

Introduction to Wireless Networking ECE 401WN Spring 2009

Transcription:

Mobila applikationer och trådlösa nät HI1033 Lecturer: Anders Lindström, anders.lindstrom@sth.kth.se Lecture 7 Today s topics Bluetooth NFC Bluetooth 1

Bluetooth Wireless technology standard for exchanging data over short distances, peer to peer Ericsson, IBM, Toshiba, Nokia, Intel,... Proprietary open Bluetooth Each device has a Bluetooth chip and antenna Radio frequency 2.45 GHz Relativly slow; 721 kbit/sec 3 Mbit/sec (ver. 2.0) Frequency jumps, 1600/sec, takes care of interference problems Up to 7 connections simultaneously Can run in parallell with WiFi (802.11b) 2

Bluetooth Class Min. power, sender Max. power, sender Min. range Typical use Class 1 0 dbm (1 mw) 20 dbm (100 mw) <100 m Devices with no limit on current Class 2 7 dbm (0,25 mw) 4 dbm (2,5 mw) <10 m Battery powered devices Class 3 0 dbm (1 mw) 0 dbm (1 mw) <1 m Battery powered devices Bluetooth Consider BT as power consuming disconnet when not needed BT v. 4.0 protocols High speed (based on WiFi) BT low energy, designed for small devices running on a button cell for months Classic API for BT v. 4.0 available in Android 4.3 and higher 3

Bluetooth Bluetooth protocols simplify the discovery and setup of services between devices Bluetooth devices can advertise all of the services they provide A device in discoverable mode on demand transmits Device name Device class List of services Technical information Two devices need to be paired to communicate with each other Bluetooth v2.1 Encryption is required for all non Service Discovery Protocol connections Bluetooth profiles Generic Access Profile, Service Discovery Application Profile supported by all devices Other, optional, profiles defining e.g. audio/video/image distribution Object exchange (push) Remote control Headsets, hands free SIM access Health Device Profile 4

Serial Port Profile (SPP) Based on the RFCOMM protocol which provides a simple reliable data stream to the user, similar to TCP Emulates a serial cable to provide a simple substitute for existing RS 232 The basis for other profiles, such as DUN, FAX, HSP and AVRCP Android Bluetooth stack 5

Communicate using Bluetooth Steps: 1. Setting up Bluetooth 2. Finding, and pairing with, devices that are either available in the local area (discovery) or already paired 3. Connecting devices (e.g. by using RFCOMM and BT sockets) 4. Transferring data between devices Android Bluetooth API Supports Bluetooth 2.1 Using Bluetooth APIs, an Android application can perform the following: Scan for other Bluetooth devices Query the local Bluetooth adapter for paired Bluetooth devices Connect to other devices through service discovery Establish RFCOMM channels Transfer data to and from other devices Manage multiple connections 6

Android Bluetooth API BluetoothAdapter the local adapter (Bluetooth radio) the entry point for all interaction BluetoothDevice represents a remote Bluetooth device BluetoothSocket represents the interface for a Bluetooth socket allows an application to exchange data with another device BluetoothServerSocket represents an open server socket listening for incoming requests connecting two Android devices requires one device exposing a server socket BluetoothClass describes the general characteristics and capabilities of a Bluetooth device Uses permissions BLUETOOTH required for requesting/accepting a connection and data transfer BLUETOOTH_ADMIN required to initiate device discovery and manage bluetooth settings <manifest...> <uses permission android:name= android.permission.bluetooth /> </manifest> 7

Set up the local adapter BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter!= null) { // Device does support Bluetooth if(adapter.isenabled() == false) { Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startactivityforresult( intent, REQUEST_ENABLE_BT); The user is prompted to enable the device Finding devices Android devices are not discoverable by default! An application can request that the user enable discoverability for limited time Discover remote devices by Querying for paired devices first Starting a device discovery Device discovery inquiry scan + page scan > 10 sec, consumes bandwith! 8

Discovering devices startdiscovery() Discovering consumes bandwith, canceldiscovery() before connecting! Asynchronous Register a BroadcastReceiver to receive information on individual devices being discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerreceiver(discoveryreceiver, filter); Don't forget to unregister, e.g. during ondestroy Discovering devices private class DiscoveryReceiver extends BroadcastReceiver { public void onreceive(context context, Intent intent) { String action = intent.getaction(); // Device discovered? if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the discovered device BluetoothDevice device = intent.getparcelableextra( BluetoothDevice.EXTRA_DEVICE); ; // Do something... arrayadapter.add(device.getname() + "\n" + device.getaddress()); 9

Enabling discoverability Prompts the user Makes the local device discoverable to others for 120 (max 300) secs Bluetooth is automatically enabled Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intent.putextra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startactivityforresult(intent, REQ_DISCOVERABLE); Pairing (bonding) When a connection is made with a remote device for the first time, a pairing request is automatically presented to the user Information about the remote device is stored; device name, class, MAC address,... A connection can be initiated with a paired device without performing discovery Paired vs. Connected: Paired devices are aware of each other's existence, having a shared link key that can be used for authentication Connected devices currently share an RFCOMM channel 10

Querying paired devices Set<BluetoothDevice> paireddevices = adapter.getbondeddevices(); if (paireddevices.size() > 0) { for (BluetoothDevice device : paireddevices) { // Show name and MAC address in a ListView arrayadapter.add(device.getname() + "\n" + device.getaddress()); Normally: First search paired devices, then (if necessary) make a discovery Connecting devices Server listens for incoming connections using BluetoothServerSocket Client initiate the connection using a BluetoothSocket + the servers MAC address Communication via BluetoothSockets and streams If not yet paired, user will be prompted for this P2P? Prepare each device as a client and as a server, listening for incoming connections? 11

Server side Get a BluetoothServerSocket via listenusingrfcommwithservicerecord( String name, UUID id) The UUID identifies your application must match the client UUID Set a time out! Call accept() to start listen blocks until connection or time out Returns a BluetoothSocket for the data transfer Close the server socket Sever side private class AcceptThread extends Thread { private BluetoothServerSocket serversocket; public AcceptThread() { serversocket= null; try { serversocket = adapter.listenusingrfcommwithservicerecord(name, MY_UUID); catch (IOException e) { public void run() { BluetoothSocket socket = null; try { socket = serversocket.accept(); // Manage the connection, in a separate threa/asynctask manageconnectedsocket(socket); catch (IOException e) {... finally { serversocket.close(); 12

Server side private class AcceptThread extends Thread { private final BluetoothServerSocket serversocket;... /** Will cancel the listening socket, and cause the thread to finish */ public void cancel() { try { serversocket.close(); catch (IOException e) { Client side Use the BluetoothDevice object representing the remote device to get a BluetoothSocket createrfcommsockettoservicerecord(uuid id) Initiate the connection by calling socket.connect() blocking, call in a separate thread. Times out after 12 seconds, throwing an exception close the socket If the UUID matches and the remote device accepts the connection, the socket is ready to transfer data 13

Client side private class ConnectThread extends Thread { private BluetoothSocket socket = null; private BluetoothDevice device; public ConnectThread(BluetoothDevice device) { this.device = device; try { socket = device.createrfcommsockettoservicerecord(my_uuid); catch (IOException e) { public void run() { bluetoothadapter.canceldiscovery(); try { socket.connect(); // Manage the connection in a separate thread/asynctask manageconnectedsocket(socket); catch (IOException connectexception) { socket.close(); // Unable to connect; close the socket Client side private class ConnectThread extends Thread { private final BluetoothSocket socket = null; private final BluetoothDevice device;... // Will cancel an in progress connection, // and close the socket public void cancel() { try { mmsocket.close(); catch (IOException e) { 14

RFCOMM socket API BT v 2.1, encrypted and authenticated communication channel bluetoothsocket = bluetoothdevice. createrfcommsockettoservicerecord( STANDARD_SPP_UUID); bluetoothsocket.connect(); Communicating with devices prior to BT v 2.1 bluetoothsocket = bluetoothdevice. createinsecurerfcommsockettoservicerecord( STANDARD_SPP_UUID); bluetoothsocket.connect(); UUID STANDARD_SPP_UUID = UUID.fromString("00001101 0000 1000 8000 00805F9B34FB"); Transfering data getinputstream() / getoutputstream() Read and write data to the streams with read(byte[]) and write(byte[]), or Add wrapper/filter streams like BufferedReader, PrintWriter, Use a separate thread for all stream reading and writing (read/write calls are blocking) Provide a method to shut down the connection, by raising a flag and closing the socket 15

Transferring data private class DataTransferThread extends Thread {... public DataTransferThread(socket) { this.socket = socket; try { sin = socket.getinputstream(); sout = socket.getoutputstream(); catch (IOException e) { public void run() { byte[] buffer = new byte[1024]; int bytes; // Keep listening to the InputStream until an exception occurs while (true) { try { bytes = sin.read(buffer); // Send the obtained bytes to the UI Activity handler.obtainmessage( MESSAGE_READ, bytes, 1, buffer).sendtotarget(); catch (IOException e) { break; Transferring data private class DataTransferThread extends Thread { private final BluetoothSocket socket; private final InputStream sin; private final OutputStream sout;... // Call this from the main Activity to send data // to the remote device public void write(byte[] bytes) { try { sout.write(bytes); catch (IOException e) { // Call this from the main Activity to shutdown the connection public void cancel() { try { socket.close(); catch (IOException e) { 16

Bluetooth Health Device Profile (HDP) BT profile designed to facilitate transmission and reception of Medical Device data API available on Android 4.0 (API level 14) Testing, resources Currently, the AVD doesn t support Bluetooth Test the BT part of your application on 2 devices, or AVD + Bluetooth? http://technoga.wordpress.com/2013/05/06/debug abluetooth app with android emulator on pc/ Readings Meier chapter 16 http://developer.android.com/guide/topics/wireless/bluet ooth.html Bluetooth chat example: http://developer.android.com/tools/samples/index.html 17

Near Field Communication NFC is a set of short range wireless technologies, typically < 5 cm Radio frequency 13.56 MHz Rates ranging from 106 kbit/s to 848 kbit/s Very low power consumption Initiator and Target the initiator actively generates an RF field that can power a passive target Near Field Communication Mobile ticketing in public transport, such as Mobile Phone Boarding Pass Mobile payment: the device acts as a debit/credit payment card Smart poster: the mobile phone is used to read RFID tags Bluetooth pairing Applications in the future, e.g. Electronic payment Identity documents Mobile commerce Electronic keys car keys, house/office keys, hotel room keys, etc. 18

Mobile payment Primary models for mobile payments: SMS based transactional payments Direct Mobile Billing Mobile web payments (WAP) Contactless Near Field Communication NFC: A Mobile phone equipped with a smartcard is brought near a reader module No authentication, or authentication using PIN Europe, e.g. parking payment NFC Mobile payment Standard by NFC Forum 2004, supported by Nokia and others Banks, Payment technology companies and Telecommunications companies has to cooperate... Breakthrough 2012? 2013? 2014? 19

NFC and (Smart)phones at present Spring 2012 10% of devices has NFC hardware < 1% are used Nokia Money: NFC supported by all new devices, 2011 Apple: NFC chip in Iphone 5? Nope. Android API from version (>) 2.3, android.nfc package hardware supported on some devices 20