Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothServerSocket;
- import android.bluetooth.BluetoothSocket;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- public class BluetoothService extends Thread {
- private static final String TAG = BluetoothService.class.getSimpleName();
- private static final String NAME = "IntelligentLine";
- private static final UUID MY_UUID = UUID.fromString("d0c722b0-7e15-11e1-b0c4-0800200c9a66");
- public static final int STATE_NONE = 0;
- public static final int STATE_LISTEN = 1;
- public static final int STATE_CONNECTING = 2;
- public static final int STATE_CONNECTED = 3;
- private BluetoothAdapter bluetoothAdapter = null;
- private Handler handler = null;
- private AcceptThread acceptThread = null;
- private ConnectThread connectThread = null;
- private ConnectedThread connectedThread = null;
- private int bluetoothState = STATE_NONE;
- public BluetoothService(Handler handler) {
- this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- this.bluetoothState = STATE_NONE;
- this.handler = handler;
- }
- public synchronized void startConnection() {
- if (this.connectThread != null) {
- this.connectThread.cancel();
- this.connectThread = null;
- }
- if (this.connectedThread != null) {
- this.connectedThread.cancel();
- this.connectedThread = null;
- }
- this.setBluetoothState(STATE_LISTEN);
- if (this.acceptThread == null) {
- this.acceptThread = new AcceptThread();
- this.acceptThread.start();
- }
- }
- public synchronized void connect(BluetoothDevice device) {
- if (this.bluetoothState == STATE_CONNECTING) {
- if (this.connectThread != null) {
- this.connectThread.cancel();
- this.connectThread = null;
- }
- }
- if (this.connectedThread != null) {
- this.connectedThread.cancel();
- this.connectedThread = null;
- }
- this.connectThread = new ConnectThread(device);
- this.connectThread.start();
- this.setBluetoothState(STATE_CONNECTING);
- }
- public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
- if (this.connectThread != null) {
- this.connectThread.cancel();
- this.connectThread = null;
- }
- if (this.connectedThread != null) {
- this.connectedThread.cancel();
- this.connectedThread = null;
- }
- if (this.acceptThread != null) {
- this.acceptThread.cancel();
- this.acceptThread = null;
- }
- this.connectedThread = new ConnectedThread(socket);
- this.connectedThread.start();
- this.setBluetoothState(STATE_CONNECTED);
- Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME);
- Bundle bundle = new Bundle();
- bundle.putString(Globals.EXTRA_DEVICE_NAME, device.getName());
- msg.setData(bundle);
- this.handler.sendMessage(msg);
- }
- public synchronized void stopConnection() {
- if (this.connectThread != null) {
- this.connectThread.cancel();
- this.connectThread = null;
- }
- if (this.connectedThread != null) {
- this.connectedThread.cancel();
- this.connectedThread = null;
- }
- if (this.acceptThread != null) {
- this.acceptThread.cancel();
- this.acceptThread = null;
- }
- this.setBluetoothState(STATE_NONE);
- }
- public void write(byte[] out) {
- ConnectedThread connectedThread = null;
- synchronized (this) {
- if (this.bluetoothState != STATE_CONNECTED) {
- return;
- }
- connectedThread = this.connectedThread;
- }
- connectedThread.write(out);
- }
- private void connectionFailed() {
- this.setBluetoothState(STATE_LISTEN);
- Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
- Bundle bundle = new Bundle();
- bundle.putString(Globals.TOAST, "Unable to connect device");
- msg.setData(bundle);
- this.handler.sendMessage(msg);
- BluetoothService.this.startConnection();
- }
- private void connectionLost() {
- this.setBluetoothState(STATE_LISTEN);
- Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
- Bundle bundle = new Bundle();
- bundle.putString(Globals.TOAST, "Device connection was lost");
- msg.setData(bundle);
- this.handler.sendMessage(msg);
- BluetoothService.this.startConnection();
- }
- public synchronized int getBluetoothState() {
- return this.bluetoothState;
- }
- private synchronized void setBluetoothState(int bluetoothState) {
- this.bluetoothState = bluetoothState;
- }
- private class AcceptThread extends Thread {
- private BluetoothServerSocket serverSocket = null;
- public AcceptThread() {
- BluetoothServerSocket tempBluetoothServerSocket = null;
- try {
- tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
- } catch (IOException e) {
- Log.e(TAG, "listen() failed", e);
- }
- this.serverSocket = tempBluetoothServerSocket;
- }
- public void run() {
- this.setName("AcceptThread");
- BluetoothSocket socket = null;
- while (bluetoothState != STATE_CONNECTED) {
- try {
- socket = this.serverSocket.accept();
- } catch (IOException e) {
- Log.e(TAG, "accept() failed", e);
- break;
- }
- if (socket != null) {
- synchronized (BluetoothService.this) {
- switch (bluetoothState) {
- case STATE_LISTEN:
- case STATE_CONNECTING:
- connected(socket, socket.getRemoteDevice());
- break;
- case STATE_NONE:
- case STATE_CONNECTED:
- try {
- socket.close();
- } catch (IOException e) {
- Log.e(TAG, "Could not close unwanted socket", e);
- }
- break;
- }
- }
- }
- }
- }
- public void cancel() {
- try {
- this.serverSocket.close();
- } catch (IOException e) {
- Log.e(TAG, "close() of server failed", e);
- }
- }
- }
- private class ConnectThread extends Thread {
- private BluetoothSocket bluetoothSocket = null;
- private BluetoothDevice bluetoothDevice = null;
- public ConnectThread(BluetoothDevice bluetoothDevice) {
- this.bluetoothDevice = bluetoothDevice;
- BluetoothSocket tempBluetoothSocket = null;
- try {
- tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- Log.e(TAG, "Socket Type: " + "create() failed", e);
- }
- this.bluetoothSocket = tempBluetoothSocket;
- }
- public void run() {
- this.setName("ConnectThread");
- bluetoothAdapter.cancelDiscovery();
- try {
- this.bluetoothSocket.connect();
- } catch (IOException e) {
- Log.e(TAG, e.getMessage(), e);
- connectionFailed();
- try {
- this.bluetoothSocket.close();
- } catch (IOException e2) {
- Log.e(TAG, "unable to close() socket during connection failure", e2);
- }
- return;
- }
- synchronized (BluetoothService.this) {
- connectThread = null;
- }
- connected(this.bluetoothSocket, this.bluetoothDevice);
- }
- public void cancel() {
- try {
- this.bluetoothSocket.close();
- } catch (IOException e) {
- Log.e(TAG, "close() of connect socket failed", e);
- }
- }
- }
- private class ConnectedThread extends Thread {
- private BluetoothSocket bluetoothSocket = null;
- private InputStream inputStream = null;
- private OutputStream outputStream = null;
- public ConnectedThread(BluetoothSocket bluetoothSocket) {
- this.bluetoothSocket = bluetoothSocket;
- InputStream tempInputStream = null;
- OutputStream tempOutputStream = null;
- try {
- tempInputStream = this.bluetoothSocket.getInputStream();
- tempOutputStream = this.bluetoothSocket.getOutputStream();
- } catch (IOException e) {
- Log.e(TAG, "temp sockets not created", e);
- }
- this.inputStream = tempInputStream;
- this.outputStream = tempOutputStream;
- }
- public void run() {
- byte[] buffer = new byte[1024];
- int bytes = 0;
- while (true) {
- try {
- bytes = this.inputStream.read(buffer);
- handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
- } catch (IOException e) {
- Log.e(TAG, "disconnected", e);
- connectionLost();
- BluetoothService.this.start();
- break;
- }
- }
- }
- public void write(byte[] buffer) {
- try {
- this.outputStream.write(buffer);
- handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
- } catch (IOException e) {
- Log.e(TAG, "Exception during write", e);
- }
- }
- public void cancel() {
- try {
- this.bluetoothSocket.close();
- } catch (IOException e) {
- Log.e(TAG, "close() of connect socket failed", e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment