Guest User

Untitled

a guest
Feb 16th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.01 KB | None | 0 0
  1.     import java.io.IOException;
  2.     import java.io.InputStream;
  3.     import java.io.OutputStream;
  4.     import java.util.UUID;
  5.  
  6.     import android.bluetooth.BluetoothAdapter;
  7.     import android.bluetooth.BluetoothDevice;
  8.     import android.bluetooth.BluetoothServerSocket;
  9.     import android.bluetooth.BluetoothSocket;
  10.     import android.os.Bundle;
  11.     import android.os.Handler;
  12.     import android.os.Message;
  13.     import android.util.Log;
  14.    
  15.     public class BluetoothService extends Thread {
  16.         private static final String TAG = BluetoothService.class.getSimpleName();
  17.         private static final String NAME = "IntelligentLine";
  18.         private static final UUID MY_UUID = UUID.fromString("d0c722b0-7e15-11e1-b0c4-0800200c9a66");
  19.  
  20.         public static final int STATE_NONE = 0;  
  21.         public static final int STATE_LISTEN = 1;
  22.         public static final int STATE_CONNECTING = 2;
  23.         public static final int STATE_CONNECTED = 3;
  24.  
  25.         private BluetoothAdapter bluetoothAdapter = null;
  26.         private Handler handler = null;
  27.         private AcceptThread acceptThread = null;
  28.         private ConnectThread connectThread = null;
  29.         private ConnectedThread connectedThread = null;
  30.         private int bluetoothState = STATE_NONE;
  31.  
  32.         public BluetoothService(Handler handler) {
  33.             this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  34.             this.bluetoothState = STATE_NONE;
  35.             this.handler = handler;
  36.         }
  37.        
  38.         public synchronized void startConnection() {
  39.             if (this.connectThread != null) {
  40.                 this.connectThread.cancel();
  41.                 this.connectThread = null;
  42.             }
  43.  
  44.             if (this.connectedThread != null) {
  45.                 this.connectedThread.cancel();
  46.                 this.connectedThread = null;
  47.             }
  48.  
  49.             this.setBluetoothState(STATE_LISTEN);
  50.            
  51.             if (this.acceptThread == null) {
  52.                 this.acceptThread = new AcceptThread();
  53.                 this.acceptThread.start();
  54.             }
  55.         }
  56.        
  57.         public synchronized void connect(BluetoothDevice device) {
  58.             if (this.bluetoothState == STATE_CONNECTING) {
  59.                 if (this.connectThread != null) {
  60.                     this.connectThread.cancel();
  61.                     this.connectThread = null;
  62.                 }
  63.             }
  64.  
  65.             if (this.connectedThread != null) {
  66.                 this.connectedThread.cancel();
  67.                 this.connectedThread = null;
  68.             }
  69.  
  70.             this.connectThread = new ConnectThread(device);
  71.             this.connectThread.start();
  72.                
  73.             this.setBluetoothState(STATE_CONNECTING);
  74.         }
  75.  
  76.         public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
  77.             if (this.connectThread != null) {
  78.                 this.connectThread.cancel();
  79.                 this.connectThread = null;
  80.             }
  81.  
  82.             if (this.connectedThread != null) {
  83.                 this.connectedThread.cancel();
  84.                 this.connectedThread = null;
  85.             }
  86.            
  87.             if (this.acceptThread != null) {
  88.                 this.acceptThread.cancel();
  89.                 this.acceptThread = null;
  90.             }
  91.  
  92.             this.connectedThread = new ConnectedThread(socket);
  93.             this.connectedThread.start();
  94.            
  95.             this.setBluetoothState(STATE_CONNECTED);
  96.             Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME);
  97.            
  98.             Bundle bundle = new Bundle();
  99.            
  100.             bundle.putString(Globals.EXTRA_DEVICE_NAME, device.getName());
  101.             msg.setData(bundle);
  102.  
  103.             this.handler.sendMessage(msg);
  104.         }
  105.  
  106.         public synchronized void stopConnection() {
  107.             if (this.connectThread != null) {
  108.                 this.connectThread.cancel();
  109.                 this.connectThread = null;
  110.             }
  111.  
  112.             if (this.connectedThread != null) {
  113.                 this.connectedThread.cancel();
  114.                 this.connectedThread = null;
  115.             }
  116.            
  117.             if (this.acceptThread != null) {
  118.                 this.acceptThread.cancel();
  119.                 this.acceptThread = null;
  120.             }
  121.            
  122.             this.setBluetoothState(STATE_NONE);
  123.         }
  124.  
  125.         public void write(byte[] out) {
  126.             ConnectedThread connectedThread = null;
  127.  
  128.             synchronized (this) {
  129.                 if (this.bluetoothState != STATE_CONNECTED) {
  130.                     return;
  131.                 }
  132.                
  133.                 connectedThread = this.connectedThread;
  134.             }
  135.  
  136.             connectedThread.write(out);
  137.         }
  138.        
  139.         private void connectionFailed() {
  140.             this.setBluetoothState(STATE_LISTEN);
  141.            
  142.             Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
  143.            
  144.             Bundle bundle = new Bundle();
  145.             bundle.putString(Globals.TOAST, "Unable to connect device");
  146.            
  147.             msg.setData(bundle);
  148.            
  149.             this.handler.sendMessage(msg);
  150.  
  151.             BluetoothService.this.startConnection();
  152.         }
  153.  
  154.         private void connectionLost() {
  155.             this.setBluetoothState(STATE_LISTEN);
  156.            
  157.             Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
  158.             Bundle bundle = new Bundle();
  159.            
  160.             bundle.putString(Globals.TOAST, "Device connection was lost");
  161.             msg.setData(bundle);
  162.            
  163.             this.handler.sendMessage(msg);
  164.  
  165.             BluetoothService.this.startConnection();
  166.         }
  167.  
  168.         public synchronized int getBluetoothState() {
  169.             return this.bluetoothState;
  170.         }
  171.  
  172.         private synchronized void setBluetoothState(int bluetoothState) {
  173.             this.bluetoothState = bluetoothState;
  174.         }
  175.        
  176.         private class AcceptThread extends Thread {
  177.             private BluetoothServerSocket serverSocket = null;
  178.  
  179.             public AcceptThread() {
  180.                 BluetoothServerSocket tempBluetoothServerSocket = null;
  181.  
  182.                 try {
  183.                     tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
  184.                 } catch (IOException e) {
  185.                     Log.e(TAG, "listen() failed", e);
  186.                 }
  187.                
  188.                 this.serverSocket = tempBluetoothServerSocket;
  189.             }
  190.  
  191.             public void run() {
  192.                 this.setName("AcceptThread");
  193.                
  194.                 BluetoothSocket socket = null;
  195.  
  196.                 while (bluetoothState != STATE_CONNECTED) {
  197.                     try {
  198.                         socket = this.serverSocket.accept();
  199.                     } catch (IOException e) {
  200.                         Log.e(TAG, "accept() failed", e);
  201.                        
  202.                         break;
  203.                     }
  204.  
  205.                     if (socket != null) {
  206.                         synchronized (BluetoothService.this) {
  207.                             switch (bluetoothState) {
  208.                             case STATE_LISTEN:
  209.                             case STATE_CONNECTING:
  210.                                 connected(socket, socket.getRemoteDevice());
  211.                                
  212.                                 break;
  213.                             case STATE_NONE:
  214.                             case STATE_CONNECTED:
  215.                                 try {
  216.                                     socket.close();
  217.                                 } catch (IOException e) {
  218.                                     Log.e(TAG, "Could not close unwanted socket", e);
  219.                                 }
  220.  
  221.                                 break;
  222.                             }
  223.                         }
  224.                     }
  225.                 }
  226.             }
  227.  
  228.             public void cancel() {
  229.                 try {
  230.                     this.serverSocket.close();
  231.                 } catch (IOException e) {
  232.                     Log.e(TAG, "close() of server failed", e);
  233.                 }
  234.             }
  235.         }
  236.        
  237.         private class ConnectThread extends Thread {
  238.             private BluetoothSocket bluetoothSocket = null;
  239.             private BluetoothDevice bluetoothDevice = null;
  240.  
  241.             public ConnectThread(BluetoothDevice bluetoothDevice) {
  242.                 this.bluetoothDevice = bluetoothDevice;
  243.                
  244.                 BluetoothSocket tempBluetoothSocket = null;
  245.  
  246.                 try {
  247.                     tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
  248.                 } catch (IOException e) {
  249.                     Log.e(TAG, "Socket Type: " + "create() failed", e);
  250.                 }
  251.                
  252.                 this.bluetoothSocket = tempBluetoothSocket;
  253.             }
  254.  
  255.             public void run() {
  256.                 this.setName("ConnectThread");
  257.  
  258.                 bluetoothAdapter.cancelDiscovery();
  259.  
  260.                 try {
  261.                     this.bluetoothSocket.connect();
  262.                 } catch (IOException e) {
  263.                     Log.e(TAG, e.getMessage(), e);
  264.                    
  265.                     connectionFailed();
  266.                    
  267.                     try {
  268.                         this.bluetoothSocket.close();
  269.                     } catch (IOException e2) {
  270.                         Log.e(TAG, "unable to close() socket during connection failure", e2);
  271.                     }
  272.                    
  273.                     return;
  274.                 }
  275.  
  276.                 synchronized (BluetoothService.this) {
  277.                     connectThread = null;
  278.                 }
  279.  
  280.                 connected(this.bluetoothSocket, this.bluetoothDevice);
  281.             }
  282.  
  283.             public void cancel() {
  284.                 try {
  285.                     this.bluetoothSocket.close();
  286.                 } catch (IOException e) {
  287.                     Log.e(TAG, "close() of connect socket failed", e);
  288.                 }
  289.             }
  290.         }
  291.  
  292.         private class ConnectedThread extends Thread {
  293.             private BluetoothSocket bluetoothSocket = null;
  294.             private InputStream inputStream = null;
  295.             private OutputStream outputStream = null;
  296.  
  297.             public ConnectedThread(BluetoothSocket bluetoothSocket) {
  298.                 this.bluetoothSocket = bluetoothSocket;
  299.                
  300.                 InputStream tempInputStream = null;
  301.                 OutputStream tempOutputStream = null;
  302.  
  303.                 try {
  304.                     tempInputStream = this.bluetoothSocket.getInputStream();
  305.                     tempOutputStream = this.bluetoothSocket.getOutputStream();
  306.                 } catch (IOException e) {
  307.                     Log.e(TAG, "temp sockets not created", e);
  308.                 }
  309.  
  310.                 this.inputStream = tempInputStream;
  311.                 this.outputStream = tempOutputStream;
  312.             }
  313.  
  314.             public void run() {
  315.                 byte[] buffer = new byte[1024];
  316.                 int bytes = 0;
  317.  
  318.                 while (true) {
  319.                     try {
  320.                         bytes = this.inputStream.read(buffer);
  321.  
  322.                         handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
  323.                     } catch (IOException e) {
  324.                         Log.e(TAG, "disconnected", e);
  325.                        
  326.                         connectionLost();
  327.                        
  328.                         BluetoothService.this.start();
  329.                        
  330.                         break;
  331.                     }
  332.                 }
  333.             }
  334.  
  335.             public void write(byte[] buffer) {
  336.                 try {
  337.                     this.outputStream.write(buffer);
  338.  
  339.                     handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
  340.                 } catch (IOException e) {
  341.                     Log.e(TAG, "Exception during write", e);
  342.                 }
  343.             }
  344.  
  345.             public void cancel() {
  346.                 try {
  347.                     this.bluetoothSocket.close();
  348.                 } catch (IOException e) {
  349.                     Log.e(TAG, "close() of connect socket failed", e);
  350.                 }
  351.             }
  352.         }
  353.     }
Advertisement
Add Comment
Please, Sign In to add comment