diff options
Diffstat (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java')
| -rw-r--r-- | altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java | 54 | 
1 files changed, 19 insertions, 35 deletions
diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java b/altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java index 93cb75de..a93c08d6 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/BluetoothChatService.java @@ -19,9 +19,7 @@ package org.altusmetrum.AltosDroid;  import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream; -//import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - +import java.util.UUID;  import android.bluetooth.BluetoothAdapter;  import android.bluetooth.BluetoothDevice;  import android.bluetooth.BluetoothSocket; @@ -103,9 +101,8 @@ public class BluetoothChatService {      /**       * Start the ConnectThread to initiate a connection to a remote device.       * @param device  The BluetoothDevice to connect -     * @param secure Socket Security type - Secure (true) , Insecure (false)       */ -    public synchronized void connect(BluetoothDevice device, boolean secure) { +    public synchronized void connect(BluetoothDevice device) {          if (D) Log.d(TAG, "connect to: " + device);          // Cancel any thread attempting to make a connection @@ -117,7 +114,7 @@ public class BluetoothChatService {          if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}          // Start the thread to connect with the given device -        mConnectThread = new ConnectThread(device, secure); +        mConnectThread = new ConnectThread(device);          mConnectThread.start();          setState(STATE_CONNECTING);      } @@ -127,9 +124,8 @@ public class BluetoothChatService {       * @param socket  The BluetoothSocket on which the connection was made       * @param device  The BluetoothDevice that has been connected       */ -    public synchronized void connected(BluetoothSocket socket, BluetoothDevice -            device, final String socketType) { -        if (D) Log.d(TAG, "connected, Socket Type:" + socketType); +    public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { +        if (D) Log.d(TAG, "connected");          // Cancel the thread that completed the connection          if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} @@ -138,7 +134,7 @@ public class BluetoothChatService {          if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}          // Start the thread to manage the connection and perform transmissions -        mConnectedThread = new ConnectedThread(socket, socketType); +        mConnectedThread = new ConnectedThread(socket);          mConnectedThread.start();          // Send the name of the connected device back to the UI Activity @@ -224,37 +220,25 @@ public class BluetoothChatService {       * succeeds or fails.       */      private class ConnectThread extends Thread { +        private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");          private final BluetoothSocket mmSocket;          private final BluetoothDevice mmDevice; -        private String mSocketType; -        public ConnectThread(BluetoothDevice device, boolean secure) { +		public ConnectThread(BluetoothDevice device) {              mmDevice = device;              BluetoothSocket tmp = null; -            mSocketType = secure ? "Secure" : "Insecure"; -            // Get a BluetoothSocket for a connection with the -            // given BluetoothDevice              try { -                if (secure) { -                	Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); -                	tmp = (BluetoothSocket) m.invoke(device, 2); -//                    tmp = device.createRfcommSocket(1); -                } else { -                	Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); -                	tmp = (BluetoothSocket) m.invoke(device, 2); -//                    tmp = device.createInsecureRfcommSocket(1); -                } -            } catch (Exception e) { -                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); +				tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); +			} catch (IOException e) {  				e.printStackTrace();  			}              mmSocket = tmp;          }          public void run() { -            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType); -            setName("ConnectThread" + mSocketType); +            Log.i(TAG, "BEGIN mConnectThread"); +            setName("ConnectThread");              // Always cancel discovery because it will slow down a connection              mAdapter.cancelDiscovery(); @@ -269,8 +253,7 @@ public class BluetoothChatService {                  try {                      mmSocket.close();                  } catch (IOException e2) { -                    Log.e(TAG, "unable to close() " + mSocketType + -                            " socket during connection failure", e2); +                    Log.e(TAG, "unable to close() socket during connection failure", e2);                  }                  connectionFailed();                  return; @@ -282,14 +265,14 @@ public class BluetoothChatService {              }              // Start the connected thread -            connected(mmSocket, mmDevice, mSocketType); +            connected(mmSocket, mmDevice);          }          public void cancel() {              try {                  mmSocket.close();              } catch (IOException e) { -                Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e); +                Log.e(TAG, "close() of connect socket failed", e);              }          }      } @@ -303,8 +286,8 @@ public class BluetoothChatService {          private final InputStream mmInStream;          private final OutputStream mmOutStream; -        public ConnectedThread(BluetoothSocket socket, String socketType) { -            Log.d(TAG, "create ConnectedThread: " + socketType); +        public ConnectedThread(BluetoothSocket socket) { +            Log.d(TAG, "create ConnectedThread");              mmSocket = socket;              InputStream tmpIn = null;              OutputStream tmpOut = null; @@ -333,7 +316,7 @@ public class BluetoothChatService {                      bytes = mmInStream.read(buffer);                      // Send the obtained bytes to the UI Activity -                    mHandler.obtainMessage(AltosDroid.MESSAGE_READ, bytes, -1, buffer) +                    mHandler.obtainMessage(AltosDroid.MESSAGE_READ, bytes, -1, buffer.clone())                              .sendToTarget();                  } catch (IOException e) {                      Log.e(TAG, "disconnected", e); @@ -350,6 +333,7 @@ public class BluetoothChatService {          public void write(byte[] buffer) {              try {                  mmOutStream.write(buffer); +                mmOutStream.write('\n');                  // Share the sent message back to the UI Activity                  mHandler.obtainMessage(AltosDroid.MESSAGE_WRITE, -1, -1, buffer)  | 
