Back to project page bluetooth.
The source code is released under:
MIT License
If you think the Android project bluetooth listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.mauriciotogneri.bluetooth.connection.server; /* w ww .jav a 2 s .c om*/ import java.util.UUID; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; class ServerThread extends Thread { private final ServerConnection serverConnection; private final BluetoothServerSocket serverSocket; public ServerThread(ServerConnection serverConnection, String uuid) { this.serverConnection = serverConnection; this.serverSocket = createSocket(uuid); } private BluetoothServerSocket createSocket(String uuid) { BluetoothServerSocket result = null; try { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } result = bluetoothAdapter.listenUsingRfcommWithServiceRecord(uuid, UUID.fromString(uuid)); } catch (Exception e) { this.serverConnection.errorOpeningConnection(this); } return result; } @Override public void run() { try { @SuppressWarnings("resource") BluetoothSocket socket = this.serverSocket.accept(); this.serverConnection.clientConnected(socket, this); } catch (Exception e) { this.serverConnection.errorOpeningConnection(this); } finally { close(); } } public void close() { try { this.serverSocket.close(); } catch (Exception e) { } } }