fixed some NullPointer bugs and changed storage type for BluetoothDevices
authorbongo <bongo@bird.localdomain>
Tue, 27 Sep 2011 20:49:46 +0000 (22:49 +0200)
committerbongo <bongo@bird.localdomain>
Tue, 27 Sep 2011 20:49:46 +0000 (22:49 +0200)
17 files changed:
AndroidManifest.xml
bin/HQBeamer.apk
bin/classes.dex
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 [deleted file]
bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectThread.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity$ConnectedThread.class
bin/de/bongo/android/hqbeamer/HQBeamerActivity.class
bin/resources.ap_
res/layout/main.xml
src/de/bongo/android/hqbeamer/HQBeamerActivity.java

index 6c16b07..bfdebcb 100644 (file)
@@ -5,6 +5,7 @@
       android:versionName="1.0">
     <uses-sdk android:minSdkVersion="8" />
     <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
+    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
 
     <application android:icon="@drawable/icon" android:label="@string/app_name">
         <activity android:name=".HQBeamerActivity"
index 70c8452..c1c93e4 100644 (file)
Binary files a/bin/HQBeamer.apk and b/bin/HQBeamer.apk differ
index c505cf2..082d08b 100644 (file)
Binary files a/bin/classes.dex and b/bin/classes.dex differ
index 6201462..cc4fcb3 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 68091d9..3b804d4 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 1dfbf45..3bd8833 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 21a0258..1abc8f7 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 c97ecad..dbacf70 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 4587581..8eb8a30 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 14171b9..bc997c4 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
deleted file mode 100644 (file)
index 2bd5596..0000000
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity$8.class and /dev/null differ
index 3e84ce5..0303074 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 eccdf7d..e091c9b 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 9d338d7..14db4c5 100644 (file)
Binary files a/bin/de/bongo/android/hqbeamer/HQBeamerActivity.class and b/bin/de/bongo/android/hqbeamer/HQBeamerActivity.class differ
index bdbdb97..377c713 100644 (file)
Binary files a/bin/resources.ap_ and b/bin/resources.ap_ differ
index f10227c..dcbf9de 100644 (file)
@@ -1,5 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
-    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
+    <RelativeLayout android:id="@+id/relativeLayout1"
+                               android:layout_width="fill_parent"
+                               android:layout_height="fill_parent"
+                               xmlns:android="http://schemas.android.com/apk/res/android">
         <ToggleButton android:layout_height="wrap_content"
                                  android:text="powerToggle"
                                  android:layout_width="wrap_content"
index 8115955..360a653 100644 (file)
@@ -6,7 +6,9 @@ import java.io.OutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
-import java.util.List;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Set;
 
 import android.app.Activity;
@@ -31,44 +33,32 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.ToggleButton;
 
 public class HQBeamerActivity extends Activity {
-    ToggleButton powerToggle = (ToggleButton) findViewById(R.id.powerToggle);
+    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 BluetoothAdapter mBluetoothAdapter;
-       private ArrayList<String> availableDevices;
-       private int deviceChosen = 0;
+       private ArrayList<BluetoothDevice> availableDevices;
+       private int indexDeviceChosen;
        private ConnectThread connectThread;
        private ConnectedThread connectedThread;
+       private BroadcastReceiver mReceiver;
        /* BEGIN Bluetooth / Beamer Messages */
                /* Begin IDs */
                private static final int MESSAGE_READ = 0;
                private static final int MESSAGE_WRITE = 1;
                /* end IDs */
-       private static final String POWER_ON = "!";
-       private static final String POWER_OFF = Character.toString((char)0x22);
+       private static final byte[] POWER_ON = {0x21};
+       private static final byte[] POWER_OFF = {0x22};
        /* END Bluetooth / Beamer Messages */
-
-       // Create a BroadcastReceiver for ACTION_FOUND
-    private final BroadcastReceiver 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.getName() + "\n" + device.getAddress());
-               }
-           }
-       };
        
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
-       
+        powerToggle = (ToggleButton) findViewById(R.id.powerToggle);
+        
         /* Start Bluetooth Initialization */
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
@@ -95,21 +85,35 @@ public class HQBeamerActivity extends Activity {
                }
                /* Bluetooth is guaranteed to be avaliable now */
         
-               /* Prepare blouetooth connection */
-        if (connectThread != null) {connectThread.cancel(); connectThread = null;}
+               /* 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());
-                // TODO
+        connectThread = new ConnectThread(availableDevices.get(indexDeviceChosen));
+
                
                /* UI Element Setup */
                this.powerToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
+                               connectedThread.write(POWER_ON);
                        }
                });
     }
     
     private boolean doBluetoothInquiry() {
-       availableDevices = new ArrayList<String>();
+       this.availableDevices = new ArrayList<BluetoothDevice>();
                ProgressDialog dialog = ProgressDialog.show(this, "", 
                 "Scanning for bluetooth devices...", true);
                dialog.show();
@@ -119,18 +123,18 @@ public class HQBeamerActivity extends Activity {
                    // 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.getName() + "\n" + device.getAddress());
+                       availableDevices.add(device);
                    }
                }
                showDialog(CHOOSE_DEVICE_DIALOG);
-               if(deviceChosen == 0){ 
+               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(deviceChosen != 0){
+                       if(availableDevices.get(indexDeviceChosen)==null){
                                return true;
                        }
                        else {
@@ -280,13 +284,17 @@ protected Dialog onCreateDialog(int id){
        Dialog dialog;
        switch(id){
        case CHOOSE_DEVICE_DIALOG:
-               dialog = new AlertDialog.Builder(HQBeamerActivity.this)
-                                                               .setTitle("Pick a RFCOMM Device")
-                                                               .setItems((String[])availableDevices.toArray(), new DialogInterface.OnClickListener() {
-                                                                   public void onClick(DialogInterface dialog, int item) {
-                                                                       HQBeamerActivity.this.deviceChosen = item;
-                                                                   }
-                                                               }).create();
+               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:
                dialog = new AlertDialog.Builder(HQBeamerActivity.this)
@@ -304,6 +312,16 @@ protected Dialog onCreateDialog(int id){
                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){