Opening a Client Bluetooth Socket Connection

To initiate a communications channel to a remote device, create a Bluetooth Socket from the BluetoothDevice object that represents it.

To create a new connection call createRfcommSocket ToServiceRecord on the Bluetooth Device to connect to, passing in the UUID of the Bluetooth Server Socket accepting requests.

If you attempt to connect to a Bluetooth Device that has not yet been paired (bonded) with the host device, you will be prompted to accept the pairing before the connect call completes, as shown in Figure 13-4.

The user must accept the pairing request on both the host and remote devices for the connection to be established.

The returned Bluetooth Socket can then be used to initiate the connection with a call to connect, as shown in Listing 13-10.

E9Jk □ gCDiffiQ^PM


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

LISTING 13-10: Connecting to a remote Bluetooth server

Available for download on Try{ BluetoothDevice device = bluetooth.getRemoteDevice("00:23:76:35:2F:AA");

BluetoothSocket clientSocket =

device.createRfcommSocketToServiceRecord(uuid); clientSocket.connect();

// TODO Transfer data using the Bluetooth Socket } catch (IOException e) {

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

Was this article helpful?

0 0
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

Post a comment