bluetooth

advertisement
Cosc 5/4730
Bluetooth:
Blackberry (JSR 82) And Android
Bluetooth Basics
• First off Bluetooth is NOT
– 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
•
1.
2.
3.
4.
5.
6.
7.
8.
9.
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
1.
2.
3.
4.
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)
5.
6.
7.
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
1. 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.
2.
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
1.
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
–
2.
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.
3.
Indicate that the service is ready to accept a client connection:
– StreamConnection connection = service.acceptAndOpen();
Note that acceptAndOpen blocks until a client connects.
4.
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.
– implementation may vary and maybe less.
• 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:10203040607040A1B1C1D
E100;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
16-bit
OBEX
0x0008
16-bit
HTTP
0x000C
16-bit
L2CAP
0x0100
16-bit
BNEP
0x000F
16-bit
Serial Port
0x1101
16-bit
ServiceDiscoveryServerServiceClassID
0x1000
16-bit
BrowseGroupDescriptorServiceClassID
0x1001
16-bit
PublicBrowseGroup
0x1002
16-bit
OBEX Object Push Profile
0x1105
16-bit
OBEX File Transfer Profile
0x1106
16-bit
Personal Area Networking User
0x1115
16-bit
Network Access Point
0x1116
16-bit
Group Network
0x1117
16-bit
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
– http://developers.sun.com/mobility/midp/articles/bluetooth2/
• 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-8a390800200c9a66");
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/zhCN/guide/topics/wireless/bluetooth.html
• Android BluetoothChat example found in SDK
and/or the development website.
Q&A
Download