Opening a Bluetooth Server Socket Listener

A Bluetooth Server Socket is used to listen for incoming Bluetooth Socket connection requests from remote Bluetooth Devices. In order for two Bluetooth devices to be connected, one must act as a server (listening for and accepting incoming requests) and the other as a client (initiating the request to connect to the server).

Once the two are connected, the communications between the server and host device are handled through a Bluetooth Socket at both ends.

To listen for incoming connection requests call the listenUsingRfcommWithServiceRecord method on your Bluetooth Adapter, passing in both a string "name" to identify your server and a UUID (universally unique identifier). This will return a BluetoothServerSocket object. Note that the client Bluetooth Socket that connects to this listener will need to know the UUID in order to connect.

To start listening for connections call accept on this Server Socket, optionally passing in a timeout duration. The Server Socket will now block until a remote Bluetooth Socket client with a matching UUID attempts to connect. If a connection request is made from a remote device that is not yet paired with the local adapter, the user will be prompted to accept a pairing request before the accept call returns. This prompt is made via a notification, as shown in Figure 13-3.

FIGURE 13-3

If an incoming connection request is successful, accept will return a Bluetooth Socket connected to the client device. You can use this socket to transfer data, as shown later in this section.

Note that accept is a blocking operation, so it's best practice to listen for incoming connection requests on a background thread rather than block the UI thread until a connection has been made.

It's also important to note that your Bluetooth Adapter must be discoverable for remote Bluetooth Devices to connect to it. Listing 13-8 shows some typical skeleton code that uses the action_request_discoverable broadcast to request that the device be made discoverable, before listening for incoming connection requests for the returned discoverability duration.

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.

LISTING 13-8: Listening for Bluetooth Socket connection requests startActivityForResult(new

Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),

DISCOVERY_REQUEST);

©Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == DISCOVERY_REQUEST) { boolean isDiscoverable = resultCode > 0; int discoverableDuration = resultCode; if (isDiscoverable) {

UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"); String name = "bluetoothserver";

final BluetoothServerSocket btserver =

bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);

Thread acceptThread = new Thread(new Runnable() { public void run() { try {

// Block until client connection established. BluetoothSocket serverSocket = btserver.accept();

// TODO Transfer data using the server socket } catch (IOException e) {

Log.d("BLUETOOTH", e.getMessage());

acceptThread.start();

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

  • s
    How to accept the bluetooth listener?
    4 years ago
  • dafne conti
    How to create a bluetooth server?
    8 months ago

Post a comment