added README file; switched to DeviceList from the samples; bugfixes
authorbongo <bongo@bird.localdomain>
Fri, 30 Sep 2011 23:29:27 +0000 (01:29 +0200)
committerbongo <bongo@bird.localdomain>
Fri, 30 Sep 2011 23:29:27 +0000 (01:29 +0200)
30 files changed:
.classpath
AndroidManifest.xml
README.txt [new file with mode: 0644]
bin/HQBeamer.apk
bin/classes.dex
bin/de/bongo/android/hqbeamer/DeviceListActivity$1.class [new file with mode: 0644]
bin/de/bongo/android/hqbeamer/DeviceListActivity$2.class [new file with mode: 0644]
bin/de/bongo/android/hqbeamer/DeviceListActivity$3.class [new file with mode: 0644]
bin/de/bongo/android/hqbeamer/DeviceListActivity.class [new file with mode: 0644]
bin/de/bongo/android/hqbeamer/HQBeamerActivity$1.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$2.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$3.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$4.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$5.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$6.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$7.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$8.class [new file with mode: 0644]
bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectThread.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectedThread.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity.class
bin/de/bongo/android/hqbeamer/R$id.class
bin/de/bongo/android/hqbeamer/R$layout.class
bin/de/bongo/android/hqbeamer/R$string.class
bin/resources.ap_
gen/de/bongo/android/hqbeamer/R.java
res/layout/device_list.xml [new file with mode: 0644]
res/layout/device_name.xml [new file with mode: 0644]
src/de/bongo/android/hqbeamer/BluetoothService.java [new file with mode: 0644]
src/de/bongo/android/hqbeamer/DeviceListActivity.java [new file with mode: 0644]
src/de/bongo/android/hqbeamer/HQBeamerActivity.java

index 609aa00..dcc75db 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-       <classpathentry kind="src" path="src"/>
+       <classpathentry excluding="de/bongo/android/hqbeamer/BluetoothService.java" kind="src" path="src"/>
        <classpathentry kind="src" path="gen"/>
        <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
        <classpathentry kind="output" path="bin"/>
index bfdebcb..fd632c7 100644 (file)
@@ -15,6 +15,7 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <activity android:name="DeviceListActivity" android:theme="@android:style/Theme.Dialog"></activity>
 
     </application>
 </manifest>
\ No newline at end of file
diff --git a/README.txt b/README.txt
new file mode 100644 (file)
index 0000000..ab0db75
--- /dev/null
@@ -0,0 +1,2 @@
+THIS APPLICATION IS IN PRE-ALPHA STATE;
+It controls a beamer via bluetooth using a rs232 to bluetooth chip on a custom board.
index c1c93e4..eeeee98 100644 (file)
Binary files a/bin/HQBeamer.apk and b/bin/HQBeamer.apk differ
index 082d08b..e632f31 100644 (file)
Binary files a/bin/classes.dex and b/bin/classes.dex differ
diff --git a/bin/de/bongo/android/hqbeamer/DeviceListActivity$1.class b/bin/de/bongo/android/hqbeamer/DeviceListActivity$1.class
new file mode 100644 (file)
index 0000000..8deae11
Binary files /dev/null and b/bin/de/bongo/android/hqbeamer/DeviceListActivity$1.class differ
diff --git a/bin/de/bongo/android/hqbeamer/DeviceListActivity$2.class b/bin/de/bongo/android/hqbeamer/DeviceListActivity$2.class
new file mode 100644 (file)
index 0000000..a230414
Binary files /dev/null and b/bin/de/bongo/android/hqbeamer/DeviceListActivity$2.class differ
diff --git a/bin/de/bongo/android/hqbeamer/DeviceListActivity$3.class b/bin/de/bongo/android/hqbeamer/DeviceListActivity$3.class
new file mode 100644 (file)
index 0000000..d0da699
Binary files /dev/null and b/bin/de/bongo/android/hqbeamer/DeviceListActivity$3.class differ
diff --git a/bin/de/bongo/android/hqbeamer/DeviceListActivity.class b/bin/de/bongo/android/hqbeamer/DeviceListActivity.class
new file mode 100644 (file)
index 0000000..27284cf
Binary files /dev/null and b/bin/de/bongo/android/hqbeamer/DeviceListActivity.class differ
index cc4fcb3..6e5f0cd 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$1.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$1.class differ
index 3b804d4..a5e0d35 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$2.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$2.class differ
index 3bd8833..04d477e 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$3.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$3.class differ
index 1abc8f7..33c70ec 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$4.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$4.class differ
index dbacf70..e3c5ed5 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$5.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$5.class differ
index 8eb8a30..d1fdc02 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$6.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$6.class differ
index bc997c4..16899d8 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$7.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$7.class differ
diff --git a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$8.class b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$8.class
new file mode 100644 (file)
index 0000000..c27a44a
Binary files /dev/null and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$8.class differ
index 0303074..a19ff09 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectThread.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectThread.class differ
index e091c9b..e21ccda 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectedThread.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectedThread.class differ
index 14db4c5..758e707 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity.class differ
index 61b790a..720e4d8 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/R$id.class and b/bin/de/bongo/android/hqbeamer/R$id.class differ
index 6e33ae8..9ff10ce 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/R$layout.class and b/bin/de/bongo/android/hqbeamer/R$layout.class differ
index a2e088b..b8480ee 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/R$string.class and b/bin/de/bongo/android/hqbeamer/R$string.class differ
index 377c713..948a2fb 100644 (file)
Binary files a/bin/resources.ap_ and b/bin/resources.ap_ differ
index 614a62b..f04beb7 100644 (file)
@@ -14,11 +14,18 @@ public final class R {
         public static final int icon=0x7f020000;
     }
     public static final class id {
-        public static final int powerToggle=0x7f050001;
-        public static final int relativeLayout1=0x7f050000;
+        public static final int button_scan=0x7f050004;
+        public static final int new_devices=0x7f050003;
+        public static final int paired_devices=0x7f050001;
+        public static final int powerToggle=0x7f050006;
+        public static final int relativeLayout1=0x7f050005;
+        public static final int title_new_devices=0x7f050002;
+        public static final int title_paired_devices=0x7f050000;
     }
     public static final class layout {
-        public static final int main=0x7f030000;
+        public static final int device_list=0x7f030000;
+        public static final int device_name=0x7f030001;
+        public static final int main=0x7f030002;
     }
     public static final class string {
         public static final int app_name=0x7f040001;
diff --git a/res/layout/device_list.xml b/res/layout/device_list.xml
new file mode 100644 (file)
index 0000000..8f90dc3
--- /dev/null
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    >
+    <TextView android:id="@+id/title_paired_devices"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="Paired Devices"
+        android:visibility="gone"
+        android:background="#666"
+        android:textColor="#fff"
+        android:paddingLeft="5dp"
+    />
+    <ListView android:id="@+id/paired_devices"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:stackFromBottom="true"
+        android:layout_weight="1"
+    />
+    <TextView android:id="@+id/title_new_devices"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="Other Devices"
+        android:visibility="gone"
+        android:background="#666"
+        android:textColor="#fff"
+        android:paddingLeft="5dp"
+    />
+    <ListView android:id="@+id/new_devices"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:stackFromBottom="true"
+        android:layout_weight="2"
+    />
+    <Button android:id="@+id/button_scan"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="Scan"
+    />
+</LinearLayout>
\ No newline at end of file
diff --git a/res/layout/device_name.xml b/res/layout/device_name.xml
new file mode 100644 (file)
index 0000000..8fa358c
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:textSize="18sp"
+    android:padding="5dp"
+/>
\ No newline at end of file
diff --git a/src/de/bongo/android/hqbeamer/BluetoothService.java b/src/de/bongo/android/hqbeamer/BluetoothService.java
new file mode 100644 (file)
index 0000000..540b9ec
--- /dev/null
@@ -0,0 +1,480 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package de.bongo.android.hqbeamer;
+
+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.content.Context;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+
+/**
+ * This class does all the work for setting up and managing Bluetooth
+ * connections with other devices. It has a thread that listens for
+ * incoming connections, a thread for connecting with a device, and a
+ * thread for performing data transmissions when connected.
+ */
+public class BluetoothService {
+    // Debugging
+    private static final String TAG = "BluetoothService";
+    private static final boolean D = true;
+
+    // Member fields
+    private final BluetoothAdapter mAdapter;
+    private final Handler mHandler;
+    private AcceptThread mSecureAcceptThread;
+    private AcceptThread mInsecureAcceptThread;
+    private ConnectThread mConnectThread;
+    private ConnectedThread mConnectedThread;
+    private int mState;
+
+    // Constants that indicate the current connection state
+    public static final int STATE_NONE = 0;       // we're doing nothing
+    public static final int STATE_LISTEN = 1;     // now listening for incoming connections
+    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
+    public static final int STATE_CONNECTED = 3;  // now connected to a remote device
+
+    /**
+     * Constructor. Prepares a new BluetoothChat session.
+     * @param context  The UI Activity Context
+     * @param handler  A Handler to send messages back to the UI Activity
+     */
+    public BluetoothService(Context context, Handler handler) {
+        mAdapter = BluetoothAdapter.getDefaultAdapter();
+        mState = STATE_NONE;
+        mHandler = handler;
+    }
+
+    /**
+     * Set the current state of the chat connection
+     * @param state  An integer defining the current connection state
+     */
+    private synchronized void setState(int state) {
+        if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
+        mState = state;
+
+        // Give the new state to the Handler so the UI Activity can update
+        mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
+    }
+
+    /**
+     * Return the current connection state. */
+    public synchronized int getState() {
+        return mState;
+    }
+
+    /**
+     * Start the chat service. Specifically start AcceptThread to begin a
+     * session in listening (server) mode. Called by the Activity onResume() */
+    public synchronized void start() {
+        if (D) Log.d(TAG, "start");
+
+        // Cancel any thread attempting to make a connection
+        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
+
+        // Cancel any thread currently running a connection
+        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
+
+        setState(STATE_LISTEN);
+
+        // Start the thread to listen on a BluetoothServerSocket
+        if (mSecureAcceptThread == null) {
+            mSecureAcceptThread = new AcceptThread(true);
+            mSecureAcceptThread.start();
+        }
+        if (mInsecureAcceptThread == null) {
+            mInsecureAcceptThread = new AcceptThread(false);
+            mInsecureAcceptThread.start();
+        }
+    }
+
+    /**
+     * 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) {
+        if (D) Log.d(TAG, "connect to: " + device);
+
+        // Cancel any thread attempting to make a connection
+        if (mState == STATE_CONNECTING) {
+            if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
+        }
+
+        // Cancel any thread currently running a connection
+        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
+
+        // Start the thread to connect with the given device
+        mConnectThread = new ConnectThread(device, secure);
+        mConnectThread.start();
+        setState(STATE_CONNECTING);
+    }
+
+    /**
+     * Start the ConnectedThread to begin managing a Bluetooth connection
+     * @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);
+
+        // Cancel the thread that completed the connection
+        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
+
+        // Cancel any thread currently running a connection
+        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
+
+        // Cancel the accept thread because we only want to connect to one device
+        if (mSecureAcceptThread != null) {
+            mSecureAcceptThread.cancel();
+            mSecureAcceptThread = null;
+        }
+        if (mInsecureAcceptThread != null) {
+            mInsecureAcceptThread.cancel();
+            mInsecureAcceptThread = null;
+        }
+
+        // Start the thread to manage the connection and perform transmissions
+        mConnectedThread = new ConnectedThread(socket, socketType);
+        mConnectedThread.start();
+
+        // Send the name of the connected device back to the UI Activity
+        Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
+        Bundle bundle = new Bundle();
+        bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());
+        msg.setData(bundle);
+        mHandler.sendMessage(msg);
+
+        setState(STATE_CONNECTED);
+    }
+
+    /**
+     * Stop all threads
+     */
+    public synchronized void stop() {
+        if (D) Log.d(TAG, "stop");
+
+        if (mConnectThread != null) {
+            mConnectThread.cancel();
+            mConnectThread = null;
+        }
+
+        if (mConnectedThread != null) {
+            mConnectedThread.cancel();
+            mConnectedThread = null;
+        }
+
+        if (mSecureAcceptThread != null) {
+            mSecureAcceptThread.cancel();
+            mSecureAcceptThread = null;
+        }
+
+        if (mInsecureAcceptThread != null) {
+            mInsecureAcceptThread.cancel();
+            mInsecureAcceptThread = null;
+        }
+        setState(STATE_NONE);
+    }
+
+    /**
+     * Write to the ConnectedThread in an unsynchronized manner
+     * @param out The bytes to write
+     * @see ConnectedThread#write(byte[])
+     */
+    public void write(byte[] out) {
+        // Create temporary object
+        ConnectedThread r;
+        // Synchronize a copy of the ConnectedThread
+        synchronized (this) {
+            if (mState != STATE_CONNECTED) return;
+            r = mConnectedThread;
+        }
+        // Perform the write unsynchronized
+        r.write(out);
+    }
+
+    /**
+     * Indicate that the connection attempt failed and notify the UI Activity.
+     */
+    private void connectionFailed() {
+        // Send a failure message back to the Activity
+        Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
+        Bundle bundle = new Bundle();
+        bundle.putString(BluetoothChat.TOAST, "Unable to connect device");
+        msg.setData(bundle);
+        mHandler.sendMessage(msg);
+
+        // Start the service over to restart listening mode
+        BluetoothChatService.this.start();
+    }
+
+    /**
+     * Indicate that the connection was lost and notify the UI Activity.
+     */
+    private void connectionLost() {
+        // Send a failure message back to the Activity
+        Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
+        Bundle bundle = new Bundle();
+        bundle.putString(BluetoothChat.TOAST, "Device connection was lost");
+        msg.setData(bundle);
+        mHandler.sendMessage(msg);
+
+        // Start the service over to restart listening mode
+        BluetoothChatService.this.start();
+    }
+
+    /**
+     * This thread runs while listening for incoming connections. It behaves
+     * like a server-side client. It runs until a connection is accepted
+     * (or until cancelled).
+     */
+    private class AcceptThread extends Thread {
+        // The local server socket
+        private final BluetoothServerSocket mmServerSocket;
+        private String mSocketType;
+
+        public AcceptThread(boolean secure) {
+            BluetoothServerSocket tmp = null;
+            mSocketType = secure ? "Secure":"Insecure";
+
+            // Create a new listening server socket
+            try {
+                if (secure) {
+                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
+                        MY_UUID_SECURE);
+                } else {
+                    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
+                            NAME_INSECURE, MY_UUID_INSECURE);
+                }
+            } catch (IOException e) {
+                Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
+            }
+            mmServerSocket = tmp;
+        }
+
+        public void run() {
+            if (D) Log.d(TAG, "Socket Type: " + mSocketType +
+                    "BEGIN mAcceptThread" + this);
+            setName("AcceptThread" + mSocketType);
+
+            BluetoothSocket socket = null;
+
+            // Listen to the server socket if we're not connected
+            while (mState != STATE_CONNECTED) {
+                try {
+                    // This is a blocking call and will only return on a
+                    // successful connection or an exception
+                    socket = mmServerSocket.accept();
+                } catch (IOException e) {
+                    Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
+                    break;
+                }
+
+                // If a connection was accepted
+                if (socket != null) {
+                    synchronized (BluetoothChatService.this) {
+                        switch (mState) {
+                        case STATE_LISTEN:
+                        case STATE_CONNECTING:
+                            // Situation normal. Start the connected thread.
+                            connected(socket, socket.getRemoteDevice(),
+                                    mSocketType);
+                            break;
+                        case STATE_NONE:
+                        case STATE_CONNECTED:
+                            // Either not ready or already connected. Terminate new socket.
+                            try {
+                                socket.close();
+                            } catch (IOException e) {
+                                Log.e(TAG, "Could not close unwanted socket", e);
+                            }
+                            break;
+                        }
+                    }
+                }
+            }
+            if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
+
+        }
+
+        public void cancel() {
+            if (D) Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
+            try {
+                mmServerSocket.close();
+            } catch (IOException e) {
+                Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
+            }
+        }
+    }
+
+
+    /**
+     * This thread runs while attempting to make an outgoing connection
+     * with a device. It runs straight through; the connection either
+     * succeeds or fails.
+     */
+    private class ConnectThread extends Thread {
+        private final BluetoothSocket mmSocket;
+        private final BluetoothDevice mmDevice;
+        private String mSocketType;
+
+        public ConnectThread(BluetoothDevice device, boolean secure) {
+            mmDevice = device;
+            BluetoothSocket tmp = null;
+            mSocketType = secure ? "Secure" : "Insecure";
+
+            // Get a BluetoothSocket for a connection with the
+            // given BluetoothDevice
+            try {
+                if (secure) {
+                    tmp = device.createRfcommSocketToServiceRecord(
+                            MY_UUID_SECURE);
+                } else {
+                    tmp = device.createInsecureRfcommSocketToServiceRecord()
+                }
+            } catch (IOException e) {
+                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
+            }
+            mmSocket = tmp;
+        }
+
+        public void run() {
+            Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
+            setName("ConnectThread" + mSocketType);
+
+            // Always cancel discovery because it will slow down a connection
+            mAdapter.cancelDiscovery();
+
+            // Make a connection to the BluetoothSocket
+            try {
+                // This is a blocking call and will only return on a
+                // successful connection or an exception
+                mmSocket.connect();
+            } catch (IOException e) {
+                // Close the socket
+                try {
+                    mmSocket.close();
+                } catch (IOException e2) {
+                    Log.e(TAG, "unable to close() " + mSocketType +
+                            " socket during connection failure", e2);
+                }
+                connectionFailed();
+                return;
+            }
+
+            // Reset the ConnectThread because we're done
+            synchronized (BluetoothService.this) {
+                mConnectThread = null;
+            }
+
+            // Start the connected thread
+            connected(mmSocket, mmDevice, mSocketType);
+        }
+
+        public void cancel() {
+            try {
+                mmSocket.close();
+            } catch (IOException e) {
+                Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
+            }
+        }
+    }
+
+    /**
+     * This thread runs during a connection with a remote device.
+     * It handles all incoming and outgoing transmissions.
+     */
+    private class ConnectedThread extends Thread {
+        private final BluetoothSocket mmSocket;
+        private final InputStream mmInStream;
+        private final OutputStream mmOutStream;
+
+        public ConnectedThread(BluetoothSocket socket, String socketType) {
+            Log.d(TAG, "create ConnectedThread: " + socketType);
+            mmSocket = socket;
+            InputStream tmpIn = null;
+            OutputStream tmpOut = null;
+
+            // Get the BluetoothSocket input and output streams
+            try {
+                tmpIn = socket.getInputStream();
+                tmpOut = socket.getOutputStream();
+            } catch (IOException e) {
+                Log.e(TAG, "temp sockets not created", e);
+            }
+
+            mmInStream = tmpIn;
+            mmOutStream = tmpOut;
+        }
+
+        public void run() {
+            Log.i(TAG, "BEGIN mConnectedThread");
+            byte[] buffer = new byte[1024];
+            int bytes;
+
+            // Keep listening to the InputStream while connected
+            while (true) {
+                try {
+                    // Read from the InputStream
+                    bytes = mmInStream.read(buffer);
+
+                    // Send the obtained bytes to the UI Activity
+                    mHandler.obtainMessage(HQBeamerActivity.MESSAGE_READ, bytes, -1, buffer)
+                            .sendToTarget();
+                } catch (IOException e) {
+                    Log.e(TAG, "disconnected", e);
+                    connectionLost();
+                    break;
+                }
+            }
+        }
+
+        /**
+         * Write to the connected OutStream.
+         * @param buffer  The bytes to write
+         */
+        public void write(byte[] buffer) {
+            try {
+                mmOutStream.write(buffer);
+
+                // Share the sent message back to the UI Activity
+                mHandler.obtainMessage(HQBeamerActivity.MESSAGE_WRITE, -1, -1, buffer)
+                        .sendToTarget();
+            } catch (IOException e) {
+                Log.e(TAG, "Exception during write", e);
+            }
+        }
+
+        public void cancel() {
+            try {
+                mmSocket.close();
+            } catch (IOException e) {
+                Log.e(TAG, "close() of connect socket failed", e);
+            }
+        }
+    }
+}
diff --git a/src/de/bongo/android/hqbeamer/DeviceListActivity.java b/src/de/bongo/android/hqbeamer/DeviceListActivity.java
new file mode 100644 (file)
index 0000000..1fe4136
--- /dev/null
@@ -0,0 +1,204 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package de.bongo.android.hqbeamer;
+
+import java.util.Set;
+
+import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.Window;
+import android.view.View.OnClickListener;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ListView;
+import android.widget.TextView;
+import android.widget.AdapterView.OnItemClickListener;
+
+/**
+ * This Activity appears as a dialog. It lists any paired devices and
+ * devices detected in the area after discovery. When a device is chosen
+ * by the user, the MAC address of the device is sent back to the parent
+ * Activity in the result Intent.
+ */
+public class DeviceListActivity extends Activity {
+    // Debugging
+    private static final String TAG = "DeviceListActivity";
+    private static final boolean D = true;
+
+    // Return Intent extra
+    public static String EXTRA_DEVICE_ADDRESS = "device_address";
+
+    // Member fields
+    private BluetoothAdapter mBtAdapter;
+    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
+    private ArrayAdapter<String> mNewDevicesArrayAdapter;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // Setup the window
+        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+        setContentView(R.layout.device_list);
+
+        // Set result CANCELED incase the user backs out
+        setResult(Activity.RESULT_CANCELED);
+
+        // Initialize the button to perform device discovery
+        Button scanButton = (Button) findViewById(R.id.button_scan);
+        scanButton.setOnClickListener(new OnClickListener() {
+            public void onClick(View v) {
+                doDiscovery();
+                ((Button)v).setText("Rescan");
+            }
+        });
+
+        // Initialize array adapters. One for already paired devices and
+        // one for newly discovered devices
+        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
+        mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
+
+        // Find and set up the ListView for paired devices
+        ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
+        pairedListView.setAdapter(mPairedDevicesArrayAdapter);
+        pairedListView.setOnItemClickListener(mDeviceClickListener);
+
+        // Find and set up the ListView for newly discovered devices
+        ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
+        newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
+        newDevicesListView.setOnItemClickListener(mDeviceClickListener);
+
+        // Register for broadcasts when a device is discovered
+        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
+        this.registerReceiver(mReceiver, filter);
+
+        // Register for broadcasts when discovery has finished
+        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
+        this.registerReceiver(mReceiver, filter);
+
+        // Get the local Bluetooth adapter
+        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
+
+        // Get a set of currently paired devices
+        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
+
+        // If there are paired devices, add each one to the ArrayAdapter
+        if (pairedDevices.size() > 0) {
+            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
+            for (BluetoothDevice device : pairedDevices) {
+                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+            }
+        } else {
+            String noDevices = "No Paired Devices";
+            mPairedDevicesArrayAdapter.add(noDevices);
+        }
+    }
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+
+        // Make sure we're not doing discovery anymore
+        if (mBtAdapter != null) {
+            mBtAdapter.cancelDiscovery();
+        }
+
+        // Unregister broadcast listeners
+        this.unregisterReceiver(mReceiver);
+    }
+
+    /**
+     * Start device discover with the BluetoothAdapter
+     */
+    private void doDiscovery() {
+        if (D) Log.d(TAG, "doDiscovery()");
+
+        this.mNewDevicesArrayAdapter.clear();
+        
+        // Indicate scanning in the title
+        setProgressBarIndeterminateVisibility(true);
+        setTitle("Scanning...");
+
+        // Turn on sub-title for new devices
+        findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
+
+        // If we're already discovering, stop it
+        if (mBtAdapter.isDiscovering()) {
+            mBtAdapter.cancelDiscovery();
+        }
+
+        // Request discover from BluetoothAdapter
+        mBtAdapter.startDiscovery();
+    }
+
+    // The on-click listener for all devices in the ListViews
+    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
+        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
+            // Cancel discovery because it's costly and we're about to connect
+            mBtAdapter.cancelDiscovery();
+
+            // Get the device MAC address, which is the last 17 chars in the View
+            String info = ((TextView) v).getText().toString();
+            String address = info.substring(info.length() - 17);
+
+            // Create the result Intent and include the MAC address
+            Intent intent = new Intent();
+            intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
+
+            // Set result and finish this Activity
+            setResult(Activity.RESULT_OK, intent);
+            finish();
+        }
+    };
+
+    // The BroadcastReceiver that listens for discovered devices and
+    // changes the title when discovery is finished
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            String action = intent.getAction();
+
+            // When discovery finds a device
+            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
+                // Get the BluetoothDevice object from the Intent
+                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+                // If it's already paired, skip it, because it's been listed already
+                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
+                    mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+                }
+            // When discovery is finished, change the Activity title
+            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
+                setProgressBarIndeterminateVisibility(false);
+                setTitle("Select Device");
+                if (mNewDevicesArrayAdapter.getCount() == 0) {
+                    String noDevices = "None found";
+                    mNewDevicesArrayAdapter.add(noDevices);
+                }
+            }
+        }
+    };
+
+}
index 360a653..eccd08c 100644 (file)
@@ -10,6 +10,9 @@ import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.UUID;
+
+import de.bongo.android.hqbeamer.DeviceListActivity;
 
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -34,25 +37,33 @@ import android.widget.ToggleButton;
 
 public class HQBeamerActivity extends Activity {
     ToggleButton powerToggle;
-       private static final int REQUEST_ENABLE_BT = 0;
-       private static final int CHOOSE_DEVICE_DIALOG = 0;
-       private static final int WARN_CHOOSE_DEVICE = 1;
-       private Boolean isBluetoothEnabled = false;
+       private static final int ERROR_CONNECTING = 1;
+       private static final int WARN_CHOOSE_DEVICE = 2;
+       private static final int ERROR_BLUETOOTH_NOT_SUPPORTED = 3;
        private BluetoothAdapter mBluetoothAdapter;
-       private ArrayList<BluetoothDevice> availableDevices;
-       private int indexDeviceChosen;
        private ConnectThread connectThread;
        private ConnectedThread connectedThread;
        private BroadcastReceiver mReceiver;
+       private BluetoothDevice beamer;
        /* BEGIN Bluetooth / Beamer Messages */
                /* Begin IDs */
+               public static final String UUID_RFCOMM = "00000003-0000-1000-8000-00805f9b34fb";
                private static final int MESSAGE_READ = 0;
                private static final int MESSAGE_WRITE = 1;
                /* end IDs */
-       private static final byte[] POWER_ON = {0x21};
-       private static final byte[] POWER_OFF = {0x22};
+       private static final byte[] POWER_ON = {0x21}; // !
+       private static final byte[] POWER_OFF = {0x22}; // "
+
+       private static final byte[] preCmdSeq = {0x30, 0x30}; // 0 0
+       private static final byte[] postCmdSeq = {0x0D}; // \r
        /* END Bluetooth / Beamer Messages */
-       
+    // Intent request codes
+       private static final int REQUEST_ENABLE_BT = 1;
+    private static final int REQUEST_CONNECT_DEVICE = 2;       
+    
+    private static final Boolean D = true; // enable Debug mode
+    private static final String TAG = "HQBeamer Main";
+    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -62,95 +73,46 @@ public class HQBeamerActivity extends Activity {
         /* Start Bluetooth Initialization */
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
-                       this.isBluetoothEnabled = false;
-                       new AlertDialog.Builder(this).setTitle("Error")
-                                          .setMessage("Your device seems not to support Bluetooth :(")
-                                          .setIcon(android.R.drawable.stat_notify_error)
-                                      .setCancelable(false)
-                                      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
-                                          public void onClick(DialogInterface dialog, int id) {
-                                                  dialog.dismiss();
-                                          }
-                                      })
-                               .create().show();
+                       showDialog(ERROR_BLUETOOTH_NOT_SUPPORTED);
+                               finish();
                }
                else {
                        if (!mBluetoothAdapter.isEnabled()) {
-                           Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
-                           startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
-                       }
-                       else {
-                               this.isBluetoothEnabled = true;
+                                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+                                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                        }
                }
-               /* Bluetooth is guaranteed to be avaliable now */
         
-               /* Prepare bluetooth connection */
-               // Create a BroadcastReceiver for ACTION_FOUND
-           this.mReceiver = new BroadcastReceiver() {
-                   public void onReceive(Context context, Intent intent) {
-                       String action = intent.getAction();
-                       // When discovery finds a device
-                       if (BluetoothDevice.ACTION_FOUND.equals(action)) {
-                           // Get the BluetoothDevice object from the Intent
-                           BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-                           // Add the name and address to an array adapter to show in a ListView
-                           availableDevices.add(device);
-                       }
-                   }
-               };
                if (connectThread != null) {connectThread.cancel(); connectThread = null;}
-               while(doBluetoothInquiry());
-        connectThread = new ConnectThread(availableDevices.get(indexDeviceChosen));
-
-               
+        if (connectedThread != null) {connectedThread.cancel(); connectedThread = null;}
+       
+        //show device list to select a device
+        Intent serverIntent = new Intent(this, DeviceListActivity.class);
+        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
+        
                /* UI Element Setup */
                this.powerToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                               connectedThread.write(POWER_ON);
+                               if(connectedThread != null){
+                                       if(isChecked){
+                                               connectedThread.write(POWER_ON);
+                                       }
+                                       else {
+                                               connectedThread.write(POWER_OFF);
+                                       }
+                               }
+                               else {
+                                       Toast.makeText(HQBeamerActivity.this, "Connection failed", Toast.LENGTH_SHORT).show();
+                               }
                        }
                });
     }
     
-    private boolean doBluetoothInquiry() {
-       this.availableDevices = new ArrayList<BluetoothDevice>();
-               ProgressDialog dialog = ProgressDialog.show(this, "", 
-                "Scanning for bluetooth devices...", true);
-               dialog.show();
-               Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
-               // If there are paired devices
-               if (pairedDevices.size() > 0) {
-                   // Loop through paired devices
-                   for (BluetoothDevice device : pairedDevices) {
-                       // Add the name and address to an array adapter to show in a ListView
-                       availableDevices.add(device);
-                   }
-               }
-               showDialog(CHOOSE_DEVICE_DIALOG);
-               if(availableDevices.get(indexDeviceChosen)==null){ 
-                       availableDevices.clear();
-                       // user hasn't chosen any of the paired devs, trying to actively scan for some
-                       // Register the BroadcastReceiver
-                       IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
-                       registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
-                       showDialog(CHOOSE_DEVICE_DIALOG);
-                       if(availableDevices.get(indexDeviceChosen)==null){
-                               return true;
-                       }
-                       else {
-                               showDialog(WARN_CHOOSE_DEVICE);
-                               return false;
-                       }
-               }
-               else {
-                       return true;
-               }
-    } 
-    
     private class ConnectThread extends Thread {
         private final BluetoothSocket mmSocket;
         private final BluetoothDevice mmDevice;
-     
+        private        Boolean connected = false;
+
         public ConnectThread(BluetoothDevice device) {
             // Use a temporary object that is later assigned to mmSocket,
             // because mmSocket is final
@@ -158,27 +120,24 @@ public class HQBeamerActivity extends Activity {
             mmDevice = device;
      
             // Get a BluetoothSocket to connect with the given BluetoothDevice
-            try {
-                // MY_UUID is the app's UUID string, also used by the server code
-               //FIXME may be not avail in later vers better would be API >=10 http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android
-               Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
-               tmp = (BluetoothSocket) m.invoke(device, 1);
-            } catch (SecurityException e) {
-                               e.printStackTrace();
-                       } catch (NoSuchMethodException e) {
-                               e.printStackTrace();
-                       } catch (IllegalArgumentException e) {
-                               e.printStackTrace();
-                       } catch (IllegalAccessException e) {
-                               e.printStackTrace();
-                       } catch (InvocationTargetException e) {
-                               e.printStackTrace();
+            // MY_UUID is the app's UUID string, also used by the server code
+               //FIXME may be not avail in later vers better would be API >=10 http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android
+               //Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[]{int.class});
+               //tmp = (BluetoothSocket)m.invoke(device, UUID_RFCOMM);
+               // better way ...
+               try {
+                               tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(UUID_RFCOMM));
+                       } catch (IOException e) {
+                Log.e(TAG, "create() failed", e);
                        }
+               if(D) Log.d(TAG, "BluetoothSocket:" + tmp.toString());
+               
             mmSocket = tmp;
         }
      
         public void run() {
-            // Cancel discovery because it will slow down the connection
+               if(D) Log.d(TAG, "started ConnectThread");
+               // Cancel discovery because it will slow down the connection
             mBluetoothAdapter.cancelDiscovery();
      
             try {
@@ -186,18 +145,23 @@ public class HQBeamerActivity extends Activity {
                 // until it succeeds or throws an exception
                 mmSocket.connect();
             } catch (IOException connectException) {
+               this.connected = false;
                 // Unable to connect; close the socket and get out
                 try {
                     mmSocket.close();
                 } catch (IOException closeException) { }
                 return;
             }
+            connected = true;
      
             // Do work to manage the connection (in a separate thread)
             manageConnectedSocket(mmSocket);
         }
-     
-        /** Will cancel an in-progress connection, and close the socket */
+        
+        public Boolean isConnected(){
+               return this.connected;
+        }
+        // Will cancel an in-progress connection, and close the socket
         public void cancel() {
             try {
                 mmSocket.close();
@@ -244,14 +208,16 @@ public class HQBeamerActivity extends Activity {
             }
         }
      
-        /* Call this from the main Activity to send data to the remote device */
+        // Call this from the main Activity to send data to the remote device 
         public void write(byte[] bytes) {
             try {
+               mmOutStream.write(preCmdSeq);
                 mmOutStream.write(bytes);
+                mmOutStream.write(postCmdSeq);
             } catch (IOException e) { }
         }
      
-        /* Call this from the main Activity to shutdown the connection */
+        // Call this from the main Activity to shutdown the connection
         public void cancel() {
             try {
                 mmSocket.close();
@@ -275,80 +241,129 @@ public class HQBeamerActivity extends Activity {
     };
 
     private void manageConnectedSocket(BluetoothSocket socket){
-       this.connectedThread = new ConnectedThread(socket);
-       connectedThread.start(); // FIXME run()?
-       // FIXME stub
+       if(D) Log.d(TAG, "entered manageConnectedSocket");
+       try {
+               socket.getOutputStream().write(Byte.parseByte("\0")); // test write with a nullbyte
+       }
+       catch(IOException excp){
+               showDialog(ERROR_CONNECTING);
+               // show warning; exit
+       }
+       if(connectThread.isConnected()){
+               this.connectedThread = new ConnectedThread(socket);
+               connectedThread.start();
+       }
+       else{
+               showDialog(ERROR_CONNECTING);
+       }
     }
     
 protected Dialog onCreateDialog(int id){
        Dialog dialog;
        switch(id){
-       case CHOOSE_DEVICE_DIALOG:
-               ArrayList<String> strings = new ArrayList<String>();
-               Iterator<BluetoothDevice> iter = availableDevices.iterator();
-               while(iter.hasNext()){
-                       BluetoothDevice curBtDev = iter.next();
-                       strings.add(curBtDev.getName()+"\n"+curBtDev.getAddress());
-               }
-               AlertDialog.Builder builder = new AlertDialog.Builder(HQBeamerActivity.this);
-               builder.setTitle("Pick a RFCOMM Device");
-               builder.setItems(strings.toArray(new String[availableDevices.size()]),
-                                                                                new DevChooserOnclickListener(strings));
-               dialog = builder.create();
-               return dialog;
-       case WARN_CHOOSE_DEVICE:
+       case ERROR_CONNECTING:
                dialog = new AlertDialog.Builder(HQBeamerActivity.this)
-                                                               .setTitle("Warning")
-                                                               .setMessage("It's required to choose a device!")
+                                                               .setTitle("Connection Error")
+                                                               .setMessage("It was not possible to establish a connection.")
                                                                .setPositiveButton("Rescan", new DialogInterface.OnClickListener() {
                                                                                @Override
                                                                                public void onClick(DialogInterface arg0, int arg1) {
-                                                                                       doBluetoothInquiry();                                                                                   
+                                                                                       Intent serverIntent = new Intent(HQBeamerActivity.this, DeviceListActivity.class);
+                                                                               startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);                                                                           
+                                                                               }
+                                                               })
+                                                                       .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
+                                                                               @Override
+                                                                               public void onClick(DialogInterface dialog, int which) {
+                                                                                       dialog.dismiss();
+                                                                                       finish();
                                                                                }
                                                                        }).create();
-               
+               return dialog;
+       case ERROR_BLUETOOTH_NOT_SUPPORTED:
+               dialog = new AlertDialog.Builder(this).setTitle("Error")
+                          .setMessage("Your device seems not to support Bluetooth :(")
+                          .setIcon(android.R.drawable.stat_notify_error)
+                      .setCancelable(false)
+                      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                          public void onClick(DialogInterface dialog, int id) {
+                                  dialog.dismiss();
+                          }
+                      }).create();
+               return dialog;
+       case WARN_CHOOSE_DEVICE:
+               dialog = new AlertDialog.Builder(this)
+                                               .setTitle("Warning")
+                                               .setMessage("You are required to choose a device!")
+                                               .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                                                               
+                                                               @Override
+                                                               public void onClick(DialogInterface dialog, int which) {
+                                                                       Intent serverIntent = new Intent(HQBeamerActivity.this, DeviceListActivity.class);
+                                                               startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
+                                                               }
+                                                       }).create();
                return dialog;
        default:
                return null;
        }
     }
-       
-       private class DevChooserOnclickListener implements DialogInterface.OnClickListener{
-               ArrayList<String> strings;
-               public DevChooserOnclickListener(ArrayList<String> strings){
-                       this.strings = strings;
-               }
-           public void onClick(DialogInterface dialog, int item) {
-               HQBeamerActivity.this.indexDeviceChosen = item;
-           }
-       }
     
     protected void onActivityResult(int requestCode, int resultCode, Intent data){
-       if(requestCode == REQUEST_ENABLE_BT){
-               if(resultCode != RESULT_OK){
-                       new AlertDialog.Builder(this).setTitle("Error")
-                                  .setMessage("Bluetooth not enabled")
-                                  .setIcon(android.R.drawable.stat_notify_error)
-                              .setCancelable(false)
-                              .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
-                                  public void onClick(DialogInterface dialog, int id) {
-                                          dialog.dismiss();
-                                  }
-                              })
-                              .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
-                                  public void onClick(DialogInterface dialog, int id) {
-                                                       if (!mBluetoothAdapter.isEnabled()) {
-                                                               HQBeamerActivity.this.isBluetoothEnabled = true;
-                                                               dialog.dismiss();
-                                                       }
-                                  }
-                              }).create().show();
-               }
-       }       
+       switch(requestCode){
+               case REQUEST_ENABLE_BT: 
+                       if(resultCode != RESULT_OK){
+                               new AlertDialog.Builder(this).setTitle("Error")
+                                          .setMessage("Bluetooth not enabled")
+                                          .setIcon(android.R.drawable.stat_notify_error)
+                                      .setCancelable(false)
+                                      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                                          public void onClick(DialogInterface dialog, int id) {
+                                                  dialog.dismiss();
+                                          }
+                                      })
+                                      .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
+                                          public void onClick(DialogInterface dialog, int id) {
+                                                               if (!mBluetoothAdapter.isEnabled()) {
+                                                                       dialog.dismiss();
+                                                               }
+                                          }
+                                      }).create().show();
+                       }
+                       break;
+               case REQUEST_CONNECT_DEVICE:
+                       if (resultCode == Activity.RESULT_OK) {
+                            // Get the device MAC address
+                            String address = data.getExtras()
+                                                 .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
+                            // Get the BluetoothDevice object
+                            beamer = mBluetoothAdapter.getRemoteDevice(address);
+                            // Attempt to connect to the device
+                            connectThread = new ConnectThread(beamer);
+                            connectThread.start();
+                       }
+                       else {
+                               showDialog(WARN_CHOOSE_DEVICE);
+                               Intent serverIntent = new Intent(this, DeviceListActivity.class);
+                           startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
+                       }
+                   break;
+       }
+    }
+    protected void onStart(){
+       super.onStart();
     }
     
     protected void onDestroy(){
-       unregisterReceiver(mReceiver);
+       if(mReceiver!=null){
+               unregisterReceiver(mReceiver);
+       }
+       if(connectThread!=null){
+               connectThread.stop();
+       }
+       if(connectedThread!=null){
+               connectThread.stop();
+       }
        super.onDestroy();
     }
 }
\ No newline at end of file