Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
Accepting Connections from a Bluetooth DeviceBack to Android Cookbook Home | Up to Chapter: Bluetooth

Author: Rachee Singh ('racheesingh')
In Published Edition? Yes
FormatLanguage: WikiFormat

Accepting Connections from a Bluetooth Device


Creating a listening server for Bluetooth Connections.


For Bluetooth devices to interact, prior to the establishment of a connection, one of the communicating devices acts like a server. It obtains an instance BluetoothServerSocket and listens for incoming requests. This instance is obtained by calling the listenUsingRfcommWithServiceRecord method on the Bluetooth adapter


With the instance of BluetoothServerSocket, we can start listening for incoming requests from remote devices through the start() method. Listening is a blocking process so we have make a new thread and call it within the thread otherwise the UI of the application becomes unresponsive.

//Making the host device discoverable
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			boolean isDiscoverable = resultCode > 0;
			if (isDiscoverable) {
				UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666");
				String serverName = "BTserver";
				final BluetoothServerSocket bluetoothServer = bluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, uuid);
				Thread listenThread = new Thread(new Runnable() {
					public void run() {
						try {
							BluetoothSocket serverSocket = bluetoothServer.accept();

						} catch (IOException e) {
							Log.d("BLUETOOTH", e.getMessage());

See Also