bruce scharlau, university of aberdeen, 2011 bluetooth considerations mobile computing some slides...

35
Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Upload: christian-coffey

Post on 28-Mar-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth Considerations

Mobile Computing

Some slides from MobEduNet

Page 2: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Electronic devices connect to one another in a variety of ways:

– Data cable and a docking cradle connect a personal digital assistant (PDA) or a cellular phone to a computer

– Radio waves connect a cordless phone to its base unit

– Infrared beam connects a remote control to a television

• Better solution: Bluetooth

Page 3: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth was supposed to replace cables

• Originally positioned as a replacement for cable, infrared, and other connection media

• But it offers a variety of other services, like synchronizing devices

• Works quietly, unconsciously, and automatically

Page 4: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth is better than Infrared

• Infrared is reliable and cheep, but it has drawbacks:– Line-of-sight: sender must align with its

receiver– One-to-one: a device can't send to multiple

receivers at the same time

• Advantages:– Interference is uncommon– Message delivery is reliable

Page 5: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth has different goals from wifi

• Both operate in the 2.4 GHz band

• 802.11b is designed to connect large devices with lots of power and speed

• Bluetooth is designed to connect small devices like PDAs, mobile phones, and peripherals

Page 6: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth differs from wifi

• 802.11b:– Communicate at up to 11 Mbit/sec– Distances up to 100 meters

• Bluetooth:– 1 Mbit/sec– Shorter range: 10 meters– Reduced power requirements

Page 7: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth features

• Wireless and automatic

• Bluetooth is inexpensive (< $5 per unit)

• Handles both data and voice

• Signals are omni-directional and can pass through walls and briefcases

• Bluetooth uses frequency hopping

Page 8: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Can do lots with Bluetooth on a mobile

Mobile

AnotherMobile

LaptopHeadset

GPS

Page 9: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bluetooth connects to sensors

• BT enabled sensors can be embedded anywhere for training, monitoring, etc

• Place sensor in shoes, strap to leg, etc for running

• Place sensor in car components for monitoring performance

• Place sensor in health equipment for ease of data transfer

Bruce Scharlau, University of Aberdeen, 2011http://www.bluetooth.com

Page 10: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bluetooth provides apps for cars

• More than music and hands free driving in cars

• Use bigger screen of car for more detail to find accommodation, food, etc

• Continuation of using apps in home for tasks

• Collect sensor data from car for use in other apps

Bruce Scharlau, University of Aberdeen, 2011http://www.bluetooth.com

Page 11: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth uses one of five profile types

GAP = generic access profile, which enables other profiles and defines how to do other services

GAP = generic access profile, which enables other profiles and defines how to do other services

PAN = personal area network, such as headset and phone, or laptop and phone

PAN = personal area network, such as headset and phone, or laptop and phone

SPP = serial port profile (over RFCOMM), such as printers use

SPP = serial port profile (over RFCOMM), such as printers use

SP = synchronisation profile, such as synching contacts from phone to laptop

SP = synchronisation profile, such as synching contacts from phone to laptop

SDAP = service discovery application profile, such as when you look for BT enabled devices (inquiry) and their offered services (discovery)

SDAP = service discovery application profile, such as when you look for BT enabled devices (inquiry) and their offered services (discovery)

Page 12: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Security is provided in three ways:

Pseudo-random frequency hopping

Authentication

Encryption

Generic Access Profile defines a security model

Page 13: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Can also create a scatternet with Bluetooth

One master and up to seven slaves = piconet

A ‘scatternet’ can have up to ten piconets

http://developers.sun.com/mobility/midp/articles/bluetooth1/

Page 14: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth Protocol Stack• Radio layer is the physical wireless connection• Baseband layer is responsible for controlling

and sending data packets over the radio link• Link Manager Protocol (LMP) uses the links

set up by the baseband to establish connections and manage piconets

• Host Controller Interface (HCI) is the dividing line between software and hardware

• Logical Link Control and Adaptation Protocol (L2CAP) receives application data and adapts it to the Bluetooth format

Page 15: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

The Bluetooth stack reaches down into the internals from your application

http://developers.sun.com

/mobility/apis/article

s/bluetoo

thintro/index.h

tml

You’ll find API docs under docs/JSR082 in WTK

You’ll find API docs under docs/JSR082 in WTK

Page 16: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

The client and server roles are clearly defined with Bluetoothhttp://develop

ers.sun.com/m

obility/apis/articles/blue

toothintro/in

dex.htm

l

Device must initialised

Need to find what is nearby

Need to advertise, and then supply

Page 17: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

These are the common parts of Bluetooth enabled MIDlets

http://developers.sun.com

/mobility/apis/article

s/bluetoo

thintro/index.h

tml

Yourdevice

Otherdevices

Page 18: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Duke’s Auction and Bluetooth

AuctionMIDletAuctionMIDlet

AuctionMIDletServerAuctionMIDletServer

AuctionMIDletServiceAuctionMIDletService

AuctionFinderAuctionFinder

LogLog

DiscoveryListenerDiscoveryListener

Encapsulates URLEncapsulates URL

Server functionalityServer functionality

Display errorsDisplay errors

AuctionMIDlet.jadAuctionMIDlet.jad

Permissions, PushRegistryPermissions, PushRegistryList details in JAD file

Used by all classes

Page 19: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Set up the Push Registry to enable connections from other devices

MIDlet-Version: 1.0.0MIDlet-Vendor: Midlet Suite VendorMIDlet-Jar-URL: AuctionMidlet.jarMicroEdition-Configuration: CLDC-1.1MIDlet-Permissions: javax.microedition.io.PushRegistry, javax.microedition.io.Connector.bluetooth.client,javax.microedition.io.Connector.bluetooth.serverMicroEdition-Profile: MIDP-1.0MIDlet-1: AuctionMIDlet, ,com.auction.j2me.AuctionMIDletMIDlet-Name: AuctionMidlet Midlet SuiteMIDlet-Push-1: btspp://localhost:F0E0D0C0B0A000908070605040302010;name=com.auction.j2me.AuctionMIDlet, *

Same UUID detailed in AuctionMIDletService class

UUID = 128 bit hexadecimal as a String

Page 20: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

The push registry needs specific details

MIDlet-Push-1: btspp://localhost:F0E0D0C0B0A000908070605040302010;name=com.auction.j2me.AuctionMIDlet, *

MIDlet-Push-<n>: <ConnectionURL>, <MIDletClassName>, <AllowedSender>

Page 21: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Set up BT to enable specific types of connections based on Generic

connection framework• A server RFCOMM URL:

btspp://localhost:2D26618601FB47C28D9F10B8EC891363;authenticate=false;    encrypt=false;name=MyBtService

• A client RFCOMM URL:

btspp://0123456789AF:1;master=false;encrypt=false;authenticate=false

Page 22: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Initialise the device first

LocalDevice bt = LocalDevice.getLocalDevice();

DiscoveryAgent da = bt.getDiscoveryAgent();

boolean success = da.startInquiry(DiscoveryAgent.GIAC, this);

mSearching = true;

Make us discoverable, and look for others

Code from AuctionFinderCode from AuctionFinder

Page 23: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

There are two kinds of ‘discoverable’ settings that you can use

GIAC = indefinately discoverable

LIAC = limited (usually a minute) discoverability

Page 24: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Second, discover and then consume services

• Look for nearby devices

• Discover what services they offer

Page 25: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Add new devices to the Service Discovery Database (SDDB)

RemoteDevice rd = servRecord[i].getHostDevice();

String name = rd.getFriendlyName(false);

String address = rd.getBluetoothAddress();

String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

AuctionMIDletService ams = new AuctionMIDletService(name, address, url);

mNewServices.addElement(ams);

Code from AuctionFinderCode from AuctionFinder

Page 26: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Check for specific services on the device

public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {

try {LocalDevice bt = LocalDevice.getLocalDevice();DiscoveryAgent da = bt.getDiscoveryAgent();UUID[] uuidSet =

{AuctionMIDletService.AUCTIONMIDLET_UUID}; mPendingServiceSearches++; da.searchServices(null, uuidSet, rd, this);} catch …

Code from AuctionFinderCode from AuctionFinder

Page 27: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Consume desired service (send a message)

private void runURL(String url) throws IOException {StreamConnection sc = null;DataOutputStream dataOut = null;try {sc = (StreamConnection) Connector.open(url);dataOut = new

DataOutputStream(sc.openDataOutputStream());LocalDevice bt = LocalDevice.getLocalDevice();String from = bt.getFriendlyName() + ":" +

bt.getBluetoothAddress();dataOut.writeUTF(from);dataOut.writeUTF(mMessageField.getString());} finally { … // close connections

Code from AuctionMIDletCode from AuctionMIDlet

Page 28: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Find a device, and send a message

Sees B

A

A

B

B

Sees A

Sends message to B

Receives message from A

Page 29: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Receive and show the messagepublic void messageReceived(String from, String message) {int layout = Item.LAYOUT_NEWLINE_AFTER;StringItem si;Form messageForm = new Form("Message");si = new StringItem("From:", from);si.setLayout(layout);messageForm.append(si);si = new StringItem("Message:", message);si.setLayout(layout);messageForm.append(si);messageForm.addCommand(mBackCommand);messageForm.setCommandListener(this);display.setCurrent(messageForm);}

Code from AuctionMIDletCode from AuctionMIDlet

Page 30: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Third, server advertises, waits for (and serves) clients, stops service

UUID uuid = AuctionMIDletService.AUCTIONMIDLET_UUID; String cs = "btspp://localhost:" + uuid.toString() + ";name=AuctionMidlet"; mNotifier = (StreamConnectionNotifier)Connector.open(cs); while (mTrucking) { // Get the next incoming connection. StreamConnection sc = mNotifier.acceptAndOpen();

DataInputStream dataIn = new DataInputStream( sc.openInputStream());

String from = dataIn.readUTF(); String message = dataIn.readUTF(); mAuctionMIDlet.messageReceived(from, message); dataIn.close(); sc.close(); } } catch ….

Display the messageDisplay the message

Code from AuctionMIDletServerCode from AuctionMIDletServer

Page 31: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bruce Scharlau, University of Aberdeen, 2011

Bluetooth should be done with separate classes so that you can

reuse components in your application

If everything is in one class, then you need to make the methods reusableIf everything is in one class, then you need to make the methods reusable

Page 32: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Bluetooth on android focuses on four tasks:

1. Set up Bluetooth with adapter class

2. Find available, or paired devices

3. Connect to specified device

4. Transfer data between the connected devices

Bruce Scharlau, University of Aberdeen, 2011

Page 33: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Android uses five classes for BT• BluetoothAdapter - Represents the local Bluetooth adapter

and is the entry-point for all Bluetooth interaction. • BluetoothDevice - Represents a remote Bluetooth device. • BluetoothSocket - Represents the interface for a Bluetooth

socket as a connection point to exchange data with another Bluetooth device via InputStream and OutputStream.

• BluetoothServerSocket - Represents an open server socket that listens for incoming requests andwill return a connected BluetoothSocket when the connection is accepted.

• BluetoothClass - Describes the general characteristics and capabilities of a Bluetooth device as a read-only set of properties.

Bruce Scharlau, University of Aberdeen, 2011

Page 34: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

Also set permissions in manifest

• Need to set BLUETOOTH permission for any BT activity

• Need to set BLUETOOTH_ADMIN for discovery and manipulate BT settings

• Need to also set BLUETOOTH when using admin.

Bruce Scharlau, University of Aberdeen, 2011

Page 35: Bruce Scharlau, University of Aberdeen, 2011 Bluetooth Considerations Mobile Computing Some slides from MobEduNet

See dev guide and example

http://developer.android.com/guide/topics/wireless/bluetooth.html has all details and link to chat example

Bruce Scharlau, University of Aberdeen, 2011