Bluetooth: Blackberry (JSR 82) And Android

Slides:



Advertisements
Similar presentations
Bluetooth. Wireless networking technology Limited broadcast range (< 10 meters) Example uses include Connecting headsets to phones, keyboard/mouse to.
Advertisements

1 Working with the Bluetooth radio Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
H. 323 Chapter 4.
CCNA – Network Fundamentals
Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
Design Description Java Bluetooth stack (JBS). What is a Bluetooth stack? In short, to get any functionality out of a Bluetooth device, one needs to implement.
Sattam Al-Sahli – Emad Al-Hemyari –
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
CPET 260 Bluetooth. What is Bluetooth? Not IEEE (Wi-Fi) or HomeRF Originally designed to replace wires Short-range, lower-power wireless technology.
Basic Input/Output Operations
Systems Architecture, Fourth Edition1 Internet and Distributed Application Services Chapter 13.
The OSI Model A layered framework for the design of network systems that allows communication across all types of computer systems regardless of their.
(part 3).  Switches, also known as switching hubs, have become an increasingly important part of our networking today, because when working with hubs,
Cosc 5/4730 Android Bluetooth. Bluetooth Basics First off Bluetooth is NOT – Infrared, which requires line-of-sight – a/b/g/n. It was designed as.
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
2011 Embedded Systems Software Training Center BluRapport SDK.
Hacking the Bluetooth Pairing Authentication Process Graduate Operating System Mini Project Siyuan Jiang and Haipeng Cai.
BLUETOOTH. Introduction Bluetooth technology discussed here aims at so-called ad- hoc piconets, which are local area networks with a very limited coverage.
Data Communications and Networks
Distributed systems – Part 2  Bluetooth – 2 nd set of slides Anila Mjeda.
Bluetooth Profile. Bluetooth profile A Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. A Bluetooth.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
INTRODUCTION Bluetooth technology is code name for Personal Area Network (PAN) technology that makes it extremely easy to connect a mobile, computing device.
ACM 511 Chapter 2. Communication Communicating the Messages The best approach is to divide the data into smaller, more manageable pieces to send over.
Bluetooth Kirthika Parmeswaran Telcordia Technologies OOPSLA’2000.
1 Chapter Overview Using the New Connection Wizard to configure network and Internet connections Using the New Connection Wizard to configure outbound.
Distributed systems – Part 2  Bluetooth 4 Anila Mjeda.
Lecture 16Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 16 Bluetooth for MIDlets Rob Pooley
Bluetooth Techniques ECE 591. Overview  Universal short-range wireless capability  Uses 2.4-GHz band  Available globally for unlicensed users  Devices.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
© 2009 Research In Motion Limited Advanced Java Application Development for the BlackBerry Smartphone Trainer name Date.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
10/15/ Short-Range Radio Frequency Networking B. Ramamuthy.
Doc.: IEEE /033r2 Submission July 1999 Simon Baatz, University of BonnSlide 1 Integration of Bluetooth into LAN Environments Simon Baatz, Matthias.
HandsFree Profile (HFP) 통신연구소 S/W Team 이상제.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
BLUETOOTH Created By: Ajay Bahoriya. Agenda Introduction to Bluetooth Bluetooth Basics Mode of operation Technology Security Advantages Integrating BT.
Computer Networking From LANs to WANs: Hardware, Software, and Security Chapter 13 FTP and Telnet.
Bluetooth Techniques Chapter 15. Overview of Bluetooth Initially developed by Swedish mobile phone maker in 1994 to let laptop computers make calls over.
발표자 : 현근수 Bluetooth. Overview wireless protocol short-range communications technology single digital wireless protocol connecting multiple devices mobile.
Jini Architecture Introduction System Overview An Example.
GPRS functionality overview in Horner OCS. GPRS functionality – Peer to Peer communication over GPRS – CSCAPE connectivity over GPRS – Data exchange using.
Bluetooth In 1994, the L. M. Ericsson company became interested in connecting its mobile phones to other devices without cables. A SIG (Special Interest.
Mapping Salutation to Bluetooth 4 Overview 4 Bluetooth Summary 4 Salutation Architecture Summary 4 Mapping Bluetooth SDP to Salutation APIs 4 Mapping Bluetooth.
JPMA PROJECT PRESENTATION (ITU) 1 INTRODUCTION BLUETOOTH CHAT DESIGN JAVA ON MOBILE DEVICE Presenters Benjamin Boateng Abass Omer Venkateshwar Rao Namilakonda.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Ch26 Ameera Almasoud 1 Based on Data Communications and Networking, 4th Edition. by Behrouz A. Forouzan, McGraw-Hill Companies, Inc., 2007.
TCS Internal Security. 2 TCS Internal Objective Objective :  Android Platform Security Architecture.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Technology By :-. What Is Bluetooth? Designed to be used to connect both mobile devices and peripherals that currently require a wire Short range wireless.
Development of a Bluetooth based web camera module.
 This work confers an application which makes possible to use a Bluetooth enabled mobile phone to remote control home appliances such electric fan, LEDs.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Object-Orientated Analysis, Design and Programming
Bluetooth.
MCA – 405 Elective –I (A) Java Programming & Technology
A Wireless LAN technologies IEEE
Wireless Communication between Android Application and Sensors
Bluetooth Profiles.
BLUETOOTH Personal Area Networking [ PAN ] over Bluetooth
Short-Range Radio Frequency Networking
Short-Range Radio Frequency Networking
Exceptions and networking
Presentation transcript:

Bluetooth: Blackberry (JSR 82) And Android Cosc 5/4730 Bluetooth: Blackberry (JSR 82) And Android

Bluetooth Basics First off Bluetooth is NOT Bluetooth is Infrared, which requires line-of-sight 802.11a/b/g/n . It was designed as a network (LAN) technology. About 300 feet or 100 meters Bluetooth is a cable replacement technology. PAN (Personal Area Network) of about 30 feet (10 meters) Transfer speed about 1 Mbit/sec and burst speed of about 20 Mbit/sec Signals are omni-directional and can pass through walls and briefcases.  

Bluetooth Network Topology Bluetooth-enabled devices are organized in groups called piconets. A piconet consists of a master and up to seven active slaves. A master and a single slave use point-to-point communication; if there are multiple slaves, point-to-multipoint communication is used. A master unit is the device that initiates the communication. A device in one piconet can communicate to another device in another piconet, forming a scatternet, as depicted in next slide.

Bluetooth Network Topology Notice that a master in one piconet may be a slave in another piconet.

Establishing a Connection the following procedures are carried out automatically when a application uses a bluetooth connection Inquire: In a new environment, the device automatically initiates an inquiry to find an access point. All nearby access points respond with their addresses, and the device picks one. Page: The paging procedure synchronizes the device with the access point. Establish a link: The Link Manager Protocol establishes a link with the access point. Discover services: The LMP uses the Service Discovery Protocol (SDP) to find out what services are available from the access point. Here we assume that the email service is available. Create an L2CAP Channel: The LMP uses information obtained from the Service Discovery Protocol (SDP) to create an L2CAP channel to the access point. The application may use this channel directly or use a protocol like RFCOMM (Radio Frequency Communications Protocol) that might be running over L2CAP. RFCOMM emulates a serial line. Create an RFCOMM channel: Depending on the needs of the application, an RFCOMM channel (or another channel) is created over the L2CAP channel. Creating an RFCOMM channel allows an existing application that works with serial ports to work with Bluetooth as well, without any modifications. Authenticate: This is the only step that requires input from the user. If the access point requires authentication, it will send an authentication request, and the user will be prompted to enter a PIN to access the service. For security reasons, the PIN code itself is not sent over the wireless link, but rather a key generated from it. Log in: If the devices use the Point-to-Point Protocol (PPP) over RFCOMM, a serial port is emulated, and user may login to the service, such as email (if that's the application). Send and receive data: The email client and the access point now use standard network protocols like TCP/IP to send and receive data.

Bluetooth Profiles Note that Bluetooth devices cannot interact unless they conform to a particular profile having the bare minimum Bluetooth stack isn't enough. The Generic Access Profile defines connection procedures, device discovery, and link management. It also defines procedures related to use of different security models and common format requirements for parameters accessible on the user interface level. At a minimum all Bluetooth devices must support this profile. The Service Discovery Application and Profile defines the features and procedures for an application in a Bluetooth device to discover services registered in other Bluetooth devices, and retrieves information related to the services. The Serial Port Profile defines the requirements for Bluetooth devices that need to set up connections that emulate serial cables and use the RFCOMM protocol. The LAN Access Profile defines how Bluetooth devices can access the services of a LAN using PPP, and shows how PPP mechanisms can be used to form a network consisting of Bluetooth devices. The Synchronization Profile defines the application requirements for Bluetooth devices that need to synchronize data on two or more devices.

Bluetooth Security All Bluetooth-enabled devices must implement the Generic Access Profile, which contains all the Bluetooth protocols and possible devices. This profile defines a security model that includes three security modes: Mode 1 is an insecure mode of operation. No security procedures are initiated. Mode 2 is known as service-level enforced security. When devices operate in this mode, no security procedures are initiated before the channel is established. This mode enables applications to have different access policies and run them in parallel. Mode 3 is known as link-level enforced security. In this mode, security procedures are initiated before link setup is complete.

Standard Bluetooth Applications File transfer. Ad-hoc networking Communicating devices can spontaneously form a community of networks that persists only as long as it's needed Device synchronization Seamless connectivity among PDAs, computers, and mobile phones allows applications to update information on multiple devices automatically when data on any one device changes. Peripheral connectivity Car kits Hands-free packages enable users to access phones and other devices without taking their hands off the steering wheel Mobile payments Your Bluetooth-enabled phone can communicate with a Bluetooth-enabled vending machine to buy a can of Diet Pepsi, and put the charge on your phone bill.

Blackberry JSR 82

JSR 82 The API is intended to provide the following capabilities Manage the Local Bluetooth Device settings. Discover other bluetooth devices and services in the neighborhood. Search for bluetooth devices on the discovered bluetooth devices. Connect to any of those bluetooth service and communicate with it. Establish RFCOMM, L2CAP, and OBEX connections between devices Using those connections, send and receive data (voice communication are likely not supported) Register a bluetooth service on the Local Bluetooth Device, so that other bluetooth devices can connect to it. Manage and control the communication connections. Provide the security to all of the above options.

Packages javax.bluetooth: core Bluetooth API javax.obex: APIs for the Object Exchange (OBEX) protocol the OBEX APIs are defined independently of the Bluetooth transport layer and packaged separately. Each of the above packages represents a separate optional package, which means that a CLDC implementation can include either package or both. MIDP-enabled devices are expected to be the kind of devices to incorporate this specification.

Developing a Bluetooth app To develop a Bluetooth application in Java using the JSR-82, you need to have the following JSR-82 Compliant Bluetooth Stack. You need a JSR-82 Implementation to successfully develop and test a Java Bluetooth application. You either need to have a simulated enviorment like the Sun Java Wireless Toolkit or Java Platform Micro Edition Software Development Kit 3.0, or a real Java Bluetooth Stack like ElectricBlue or Avelink. The simulator allows you to test the Bluetooth application in a simulated environment, with no access to real bluetooth device. Local Bluetooth Device. If you are using a simulator, this is not needed. But to test your application in real world over a java bluetooth stack, you need to have a bluetooth enabled system. This can either be a Bluetooth USB Dongle plugged in to your system, or the internal built in bluetooth device. The java application that you writes, runs over this bluetooth device, and this bluetooth device is called as the ‘LocalDevice’ in JSR-82.

Device management LocalDevice and RemoteDevice Provide management capabilities as defined in the Generic Access Profile LocalDevice needs javax.bluetooth.DeviceClass to retrieve the device's type and the kinds of services it offers The RemoteDevice class represents a remote device (a device within a range of reach) and provides methods to retrieve information about the device, including its Bluetooth address and name

LocalDevice example // retrieve the local Bluetooth device object LocalDevice local = LocalDevice.getLocalDevice(); // retrieve the Bluetooth address of the local device String address = local.getBluetoothAddress(); // retrieve the name of the local Bluetooth device String name = local.getFriendlyName(); Another useful method boolean isPowerOn()

RemoveDevice Example // retrieve the device that is at the other end of the Bluetooth Serial Port Profile connection, L2CAP connection, or OBEX over RFCOMM connection RemoteDevice remote = RemoteDevice.getRemoteDevice( javax.microedition.io.Connection c); // retrieve the Bluetooth address of the remote device String remoteAddress = remote.getBluetoothAddress(); // retrieve the name of the remote Bluetooth device String remoteName = local.getFriendlyName(true); The RemoteDevice class also provides methods to authenticate, authorize, or encrypt data transferred between local and remote devices.

Device Discovery Because wireless devices are mobile they need a mechanism that allows them to find other devices and gain access to their capabilities. The core Bluetooth API's DiscoveryAgent class and DiscoveryListener interface provide the necessary discovery services. A Bluetooth device can use a DiscoveryAgent object to obtain a list of accessible devices There are a couple of ways to do this

Implement the DiscoveryListener The DiscoveryAgent.startInquiry method places the device into an inquiry mode. To take advantage of this mode, the application must specify an event listener that will respond to inquiry-related events. DiscoveryListener.deviceDiscovered is called each time an inquiry finds a device. When the inquiry is completed or canceled, DiscoveryListener.inquiryCompleted is invoked. Two more that needed be implemented, serviceSearchCompleted and servicesDiscovered

DiscoveryListener example SampleSPPClient client=new SampleSPPClient(); SampleSPPClient declared on the next slide. DiscoveryAgent agent = localDevice.getDiscoveryAgent(); agent.startInquiry(DiscoveryAgent.GIAC, client); //client is the class that has the listener, use this if implemented in the same class. //The inquiry access code for General/Unlimited Inquiry Access Code (GIAC) //to cancel an inquiry agent.cancelInquire(client)

DiscoveryListener example (2) class SampleSPPClient implements DiscoveryListener{ //required methods of DiscoveryListener public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { //Found a Device, btDevice is RemoteDevice needed to talk to the device } public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { //needed once we have found a device, and want to search for the services that device provides //this is called when services are found and SerivceRecord contains the information. //See the Field Summary for DisType and respCode constants. public void serviceSearchCompleted(int transID, int respCode) { //search for services has completed public void inquiryCompleted(int discType) { //search for devices has completed.

Other ways If the device doesn't wish to wait for devices to be discovered it can use the DiscoveryAgent.retrieveDevices method to retrieve an existing list. Based on the parameter either: return either a list of devices that were found in a previous inquiry (cached) return a list of pre-known devices that the local device has told the Bluetooth Control Center it will contact often. No Listener is needed.

Other ways Example // retrieve the discovery agent DiscoveryAgent agent = local.getDiscoveryAgent(); // return an array of pre-known devices RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN); OR // return an array of devices found in a previous inquiry RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.CACHED);

Service Discovery Once the local device has discovered at least one remote device, it can begin to search for available services - Bluetooth applications it can use to accomplish useful tasks. Because service discovery is much like device discovery, DiscoveryAgent also provides methods to discover services on a Bluetooth server device, and to initiate service-discovery transactions. As seen in listener example. Note that the API provides mechanisms to search for services on remote devices, but not for services on the local device.

ServicesDiscovered //From the implemented Listener class public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { if(servRecord!=null && servRecord.length>0){ connectionURL=servRecord[0].getConnectionURL(0,false); } //Then in the "main" code StreamConnection streamConnection=(StreamConnection)Connector.open(connectionURL); //output strean OutputStream outStream=streamConnection.openOutputStream(); OutputStreamWriter out= new OutputStreamWriter(outStream); //input stream InputStream inStream=streamConnection.openInputStream(); InputStreamReader in =new InputStreamReader(inStream);

Server side Bluetooth Before a service can be discovered, it must first be registered - advertised on a Bluetooth server device. The server is responsible for: Creating a service record that describes the service offered Adding the service record to the server's Service Discovery DataBase (SDDB), so it's visible and available to potential clients Registering the Bluetooth security measures associated with the service (enforced for connections with clients) Accepting connections from clients Updating the service record in the SDDB whenever the service's attributes change Removing or disabling the service record in the SDDB when the service is no longer available

Service Registration To create a new service record that represents the service, invoke Connector.open with a server connection URL argument, and cast the result to a StreamConnectionNotifier that represents the service StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open("someURL"); Obtain the service record created by the server device: ServiceRecord sr = local.getRecord(service); Not necessary if you don't plan to change the ServiceRecord. Indicate that the service is ready to accept a client connection: StreamConnection connection = service.acceptAndOpen(); Note that acceptAndOpen blocks until a client connects. When the server is ready to exit, close the connection and remove the service record: service.close();

Connector.open("someURL"); Serial Port Profile The RFCOMM protocol, which is layered over the L2CAP protocol, emulates an RS-232 serial connection. The Serial Port Profile (SPP) eases communication between Bluetooth devices by providing a stream-based interface to the RFCOMM protocol. Some capabilities and limitations to note: Two devices can share only one RFCOMM session at a time. Up to 60 logical serial connections can be multiplexed over this session. implementation may vary and maybe less. A single Bluetooth device can have at most 30 active RFCOMM services. A device can support only one client connection to any given service at a time.

SPP URL // assuming the service UID has been retrieved String serviceURL = "btspp://localhost:" + serviceUID.toString()); // more explicitly: String ServiceURL = "btspp://localhost:10203040607040A1B1C1DE100;name=SPP Server1";

SPP with UUID Create a UUID object from the string provided, using the ShortUUID method. UUID uuid = new UUID("1101", true); //Create the service url String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server"; What is UUID?

What is UUID? The UUID class defines universally unique identifiers. These 128-bit unsigned integers are guaranteed to be unique across all time and space. Accordingly, an instance of this class is immutable. The Bluetooth specification provides an algorithm describing how a 16-bit or 32-bit UUID could be promoted to a 128-bit UUID. Accordingly, this class provides an interface that assists applications in creating 16-bit, 32-bit, and 128-bit long UUIDs. The methods supported by this class allow equality testing of two UUID objects. The Bluetooth Assigned Numbers document ( http://www.bluetooth.org/assigned-numbers/sdp.htm) defines a large number of UUIDs for protocols and service classes.

Most common UUIDs Name Value Size Base UUID Value (Used in promoting 16-bit and 32-bit UUIDs to 128-bit UUIDs) 0x0000000000001000800000805F9B34FB 128-bit SDP 0x0001 16-bit RFCOMM 0x0003 OBEX 0x0008 HTTP 0x000C L2CAP 0x0100 BNEP 0x000F Serial Port 0x1101 ServiceDiscoveryServerServiceClassID 0x1000 BrowseGroupDescriptorServiceClassID 0x1001 PublicBrowseGroup 0x1002 OBEX Object Push Profile 0x1105 OBEX File Transfer Profile 0x1106 Personal Area Networking User 0x1115 Network Access Point 0x1116 Group Network 0x1117

Pulling it all together The server registers a service, then blocks while waiting for a connection (just like network code). From the server connection: StreamConnection connection=streamConnNotifier.acceptAndOpen(); //input : InputStream inStream=connection.openInputStream(); InputStreamReader in =new InputStreamReader(inStream); //output: OutputStream outStream=connection.openOutputStream(); OutputStreamWriter out= new OutputStreamWriter( outStream); We have our standard Streams for both client and server side, so just like network code at this point.

Simulator note No Bluetooth in the Blackberry (or android) simulators

BlackBerry specific Implements the full JSR 82 standard. Also adds net.rim.device.api.bluetooth Provides serial communication support for Bluetooth connections. This api needs a signed key. See the BluetoothDemo in sample code for use. While the Bluetooth code is much simpler then JSR 82, the example of how to use it, IS NOT! This simulator requires a specific bluetooth board connected to the PC, in order for bluetooth to usable in the simulator, otherwise there is NO bluetooth in the simulator. the Casira tool is this: http://www.btdesigner.com/devcasira.htm may cost about $3,000 … In the JDE, simlulator preferences, select Ports and enter the com port number the Casira board is using. (ie com1, then enter 1).

Blackberry Phones. To get the example code to work Turn on Bluetooth App will attempt to turn it on, but sometimes locks up. In Bluetooth options Discovery: YES It’s a security problem, so if you are using your own phone, change the setting back to NO once you are done.

JSR 82 References Java Docs for JSR 82 http://java.sun.com/javame/reference/apis/jsr082/ http://developers.sun.com/mobility/midp/articles/bluetooth2/ Wireless Application Programming with J2ME and Bluetooth http://developers.sun.com/mobility/midp/articles/bluetooth1/ Part II: The Java APIs for Bluetooth Wireless Technology JSR-82 : Java Bluetooth http://www.jsr82.com/jsr-82-basics/ More example code http://www.codeguru.com/java/article.php/c13147

Android Package: android.bluetooth

Android Does not implement JSR-82 (nor OBEX). Uses it own functions. Found in the android.bluetooth package.

android.bluetooth package BluetoothAdapter Represents the local Bluetooth adapter (Bluetooth radio). The BluetoothAdapter is the entry-point for all Bluetooth interaction. Using this, you can discover other Bluetooth devices, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for communications from other devices. BluetoothDevice Represents a remote Bluetooth device. Use this to request a connection with a remote device through a BluetoothSocket or query information about the device such as its name, address, class, and bonding state. BluetoothSocket Represents the interface for a Bluetooth socket (similar to a TCP Socket). This is the connection point that allows an application to exchange data with another Bluetooth device via InputStream and OutputStream. BluetoothServerSocket Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket). In order to connect two Android devices, one device must open a server socket with this class. When a remote Bluetooth device makes a connection request to the this device, the BluetoothServerSocket will return a connected BluetoothSocket when the connection is accepted. BluetoothClass Describes the general characteristics and capabilities of a Bluetooth device. This is a read-only set of properties that define the device's major and minor device classes and its services. However, this does not reliably describe all Bluetooth profiles and services supported by the device, but is useful as a hint to the device type.

Bluetooth Permissions Like sms, you need to add a permission line in the androidManifest.xml file <manifest ... >   ...   <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> Needed if you turn on/off bluetooth or change how the device functions. </manifest>

Setting Up Bluetooth The BluetoothAdapter is required for any and all Bluetooth activity BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth } To check and see if Bluetooth is enabled and enable it private static final int REQUEST_ENABLE_BT = 2 … if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

Paired devices If the device you need has already been paired, then you can query to find out if it is connected or not. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); // If there are paired devices if (pairedDevices.size() > 0) { // Loop through paired devices for (BluetoothDevice device : pairedDevices) { // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); }

Enabling Discoverability Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); By default, the device will become discoverable for 120 seconds, but this code changes it to 300 seconds. Note the user may get this screen

Discovering Devices To find a device, you use startDiscovery(), via a BroadcastReceiver. // Create a BroadcastReceiver for ACTION_FOUND private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } }; // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

Managing the devices Before you can connection to each, unpaired devices will be paired. This is done automatically in the framework, but the user will get a pairing dialog It will fail if the user says no or the pairing times out. Once the two devices are paired, then a connection can be made.

Server Side bluetooth Like networking, you setup a socket (BluetoothServerSocket) and accept a connection. Use the listenUsingRfcommWithServiceRecord( String, UUID). String is the Name UUID is similar to UUID before (see the note on android website)

Server Side bluetooth example // Name for the SDP record when creating server socket private static final String NAME = "BluetoothChat"; // Unique UUID for this application private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); BluetoothServerSocket mmServerSocket = null; BluetoothSocket socket = null; try { // MY_UUID is the app's UUID string, also used by the client code mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); socket = mmServerSocket.accept(); } catch (IOException e) { }

Client side bluetooth To initiate a connection with a remote device, first you discover the BluetoothDevice object that represents the remote device. Shown in Discovering Devices and in paired devices. Using the BluetoothDevice, get a BluetoothSocket by calling createRfcommSocketToServiceRecord(UUID). Initiate the connection by calling connect(). Note connect is blocking and should be not called when in discovery mode.

Client side bluetooth example // Unique UUID for this application private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); //find remote device ... //from discovery example previously. // Get a BluetoothSocket to connect with the given BluetoothDevice; BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); ... BluetoothSocket mmSocket=null; try { // MY_UUID is the app's UUID string, also used by the server code mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID); mmSocket.connect(); } catch (IOException e) { }

Connected via Android bluetooth Once both the client and server are setup with sockets Now it's just like standard network code. PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); But you will likely need to deal with disconnections, if either of the "phones" is moved out of connection range, then the connection will fail and have to be restarted once they are in range again.

Android References http://developer.android.com/intl/zh-CN/guide/topics/wireless/bluetooth.html Android BluetoothChat example found in SDK and/or the development website.

Q A &