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;
/**
* 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
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);
}
* @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;}
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
* 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();
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;
}
// 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);
}
}
}
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;
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);
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)