350 likes | 469 Views
Bluetooth Considerations. Mobile Computing. Some slides from MobEduNet. 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
E N D
Bluetooth Considerations Mobile Computing Bruce Scharlau, University of Aberdeen, 2011 Some slides from MobEduNet
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 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 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 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 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 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 Bruce Scharlau, University of Aberdeen, 2011
Can do lots with Bluetooth on a mobile Another Mobile GPS Mobile Headset Laptop Bruce Scharlau, University of Aberdeen, 2011
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, 2011 http://www.bluetooth.com
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, 2011 http://www.bluetooth.com
Bluetooth uses one of five profile types GAP = generic access profile, which enables other profiles and defines how to do other services SPP = serial port profile (over RFCOMM), such as printers use PAN = personal area network, such as headset and phone, or laptop and phone 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) 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 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 Bruce Scharlau, University of Aberdeen, 2011 http://developers.sun.com/mobility/midp/articles/bluetooth1/
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 Bruce Scharlau, University of Aberdeen, 2011
The Bluetooth stack reaches down into the internals from your application You’ll find API docs under docs/JSR082 in WTK http://developers.sun.com/mobility/apis/articles/bluetoothintro/index.html Bruce Scharlau, University of Aberdeen, 2011
The client and server roles are clearly defined with Bluetooth Device must initialised Need to find what is nearby http://developers.sun.com/mobility/apis/articles/bluetoothintro/index.html Need to advertise, and then supply Bruce Scharlau, University of Aberdeen, 2011
These are the common parts of Bluetooth enabled MIDlets Your device http://developers.sun.com/mobility/apis/articles/bluetoothintro/index.html Other devices Bruce Scharlau, University of Aberdeen, 2011
Duke’s Auction and Bluetooth Log AuctionMIDlet Display errors Used by all classes AuctionMIDletServer Server functionality AuctionFinder DiscoveryListener AuctionMIDletService Encapsulates URL AuctionMIDlet.jad List details in JAD file Permissions, PushRegistry Bruce Scharlau, University of Aberdeen, 2011
Set up the Push Registry to enable connections from other devices MIDlet-Version: 1.0.0 MIDlet-Vendor: Midlet Suite Vendor MIDlet-Jar-URL: AuctionMidlet.jar MicroEdition-Configuration: CLDC-1.1 MIDlet-Permissions: javax.microedition.io.PushRegistry, javax.microedition.io.Connector.bluetooth.client,javax.microedition.io.Connector.bluetooth.server MicroEdition-Profile: MIDP-1.0 MIDlet-1: AuctionMIDlet, ,com.auction.j2me.AuctionMIDlet MIDlet-Name: AuctionMidlet Midlet Suite MIDlet-Push-1: btspp://localhost:F0E0D0C0B0A000908070605040302010;name=com.auction.j2me.AuctionMIDlet, * UUID = 128 bit hexadecimal as a String Same UUID detailed in AuctionMIDletService class 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> 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 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 AuctionFinder 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 Bruce Scharlau, University of Aberdeen, 2011
Second, discover and then consume services • Look for nearby devices • Discover what services they offer 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 AuctionFinder Bruce Scharlau, University of Aberdeen, 2011
Check for specific services on the device publicvoid 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 AuctionFinder Bruce Scharlau, University of Aberdeen, 2011
Consume desired service (send a message) privatevoid 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 AuctionMIDlet Bruce Scharlau, University of Aberdeen, 2011
Find a device, and send a message A B Sees A Sees B A B Sends message to B Receives message from A Bruce Scharlau, University of Aberdeen, 2011
Receive and show the message publicvoid 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 AuctionMIDlet 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 message Code from AuctionMIDletServer 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 reusable Bruce Scharlau, University of Aberdeen, 2011
Bluetooth on android focuses on four tasks: • Set up Bluetooth with adapter class • Find available, or paired devices • Connect to specified device • Transfer data between the connected devices Bruce Scharlau, University of Aberdeen, 2011
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
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
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