Bluetooth Communications

The Bluetooth communications APIs are wrappers around RFCOMM, the Bluetooth radio frequency communications protocol. RFCOMM supports RS232 serial communication over the Logical Link Control and Adaptation Protocol (L2CAP) layer.

In practice, this alphabet soup provides a mechanism for opening communication sockets between two paired Bluetooth devices.

Before your application can communicate between devices they must be paired (bonded). At the time of writing (Android API level 7) there is no way to manually initiate pairing between the local Bluetooth Adapter and a remote Bluetooth Device.

If two devices are to be paired the user will need to explicitly allow this, either through the Bluetooth Settings screen or when prompted by your application when you attempt to connect a Bluetooth Socket between two unpaired devices.

You can establish an RFCOMM communication channel for bidirectional communications using the following classes.

> BluetoothServerSocket Used to establish a listening socket for initiating a link between devices. To establish a handshake, one device acts as a server to listen for, and accept, incoming connection requests.

> BluetoothSocket Used in creating a new client socket to connect to a listening Bluetooth Server Socket, and returned by the Server Socket once a connection is established. Once the connection is made, Bluetooth Sockets are used on both the server and client sides to transfer data streams.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

When creating an application that uses Bluetooth as a peer-to-peer transport layer, you'll need to implement both a Bluetooth Server Socket to listen for connections and a Bluetooth Socket to initiate a new channel and handle communications.

Once connected, the Socket Server returns a Bluetooth Socket that's subsequently used by the server device to send and receive data. This server-side Bluetooth Socket is used in exactly the same way as the client socket. The designations of server and client are relevant only to how the connection is established. They don't affect how data flows once that connection is made.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

Post a comment