Browse Source

Fixed bluetooth to close connections properly.
Small bluetooth view fix.

Lars Pandikow 10 years ago
parent
commit
0faeafd676

+ 4 - 5
AndroidManifest.xml

@@ -45,17 +45,16 @@
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:label="@string/app_name"
             android:screenOrientation="portrait" > 
-           	<intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>  
   
         </activity>
         <activity
             android:name="de.tudarmstadt.informatik.hostage.ui.MainActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:label="@string/app_name" >
- 
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>              
        
         </activity>
         <activity

+ 7 - 0
res/layout/activity_bluetooth.xml

@@ -14,5 +14,12 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
+    
+    <ListView
+        android:id="@+id/bluetoothListView"
+        android:visibility="gone"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        />
 
 </LinearLayout>

+ 60 - 29
src/de/tudarmstadt/informatik/hostage/sync/bluetooth/BluetoothSyncActivity.java

@@ -2,7 +2,6 @@ package de.tudarmstadt.informatik.hostage.sync.bluetooth;
 
 import java.util.UUID;
 
-import android.util.Log;
 import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
@@ -17,7 +16,6 @@ import android.os.Message;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
-import android.widget.LinearLayout;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.AdapterView.OnItemClickListener;
@@ -49,7 +47,6 @@ public class BluetoothSyncActivity extends Activity{
 	
 	private TextView mInfoText;
 	private ListView listView;
-	private LinearLayout layout;
 	
 	
 	@Override
@@ -58,11 +55,29 @@ public class BluetoothSyncActivity extends Activity{
 		setContentView(R.layout.activity_bluetooth);		
 		
 		serviceUUID = UUID.fromString(getResources().getString(R.string.UUID));
-		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();					
+		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();	
 		arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_view_bluetooth_devices);	
-		setLayoutElement();
+		
+		setLayoutElement();		
 		registerBroadcastReceiver();
-				
+		
+		if(savedInstanceState != null){
+			CharSequence text = savedInstanceState.getCharSequence("mInfoText");
+			mInfoText.setText(text); 		
+		    String[] data = savedInstanceState.getStringArray("adapter");
+		    if(data != null){
+			    for(int i = 0; i < data.length; i++){
+					arrayAdapter.add(data[i]);
+				    arrayAdapter.notifyDataSetChanged();
+			    }
+		    }	
+		    if(savedInstanceState.getBoolean("listView")){
+		    	listView.setVisibility(View.VISIBLE);
+		    }else{
+		    	listView.setVisibility(View.GONE);
+		    }
+		}
+							
 		if (mBluetoothAdapter == null) {
 			// Device does not support Bluetooth
 			mInfoText.setText("Bluetooth is not available on this device.");
@@ -71,7 +86,7 @@ public class BluetoothSyncActivity extends Activity{
 			mInfoText.setText("Enable Bluetooth before synchronizing.");
 			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 			startActivity(enableBtIntent);
-		} else {
+		} else if(savedInstanceState == null){
 			startConnectionListener();
 			chooseDevice();
 		}
@@ -84,37 +99,42 @@ public class BluetoothSyncActivity extends Activity{
 		if(mRecieverRegistered){
 			unregisterBroadcastReceiver();
 		}
-		if(commThread != null) {
-			commThread.cancel();
-		}
-		if(clientThread != null){
-			clientThread.cancel();
-		}
-		if(serverThread != null){
-			serverThread.cancel();
+		cancelThreads();
+	}
+	
+	@Override
+	protected void onSaveInstanceState(Bundle outState){
+		String[] data = new String[arrayAdapter.getCount()];
+		for(int i = 0; i < arrayAdapter.getCount(); i++){
+			data[i] = arrayAdapter.getItem(i);
 		}
+		outState.putStringArray("adapter", data);
+		outState.putCharSequence("mInfoText", mInfoText.getText());
+		outState.putBoolean("listView", listView.isShown());
+		super.onSaveInstanceState(outState);
 	}
 	
+	
 	/**
-	 * Starts discorvry of bluetooth devices.
+	 * Starts discovery of bluetooth devices.
 	 */
 	private void chooseDevice(){
-		arrayAdapter.clear();
 		if (!mBluetoothAdapter.startDiscovery())
 			return;
 		mInfoText.setText("Choose Device for synchronizing:\n");
-		layout.addView(listView);
-		setContentView(layout);
+		listView.setVisibility(View.VISIBLE);
 	}
 
 	/**
-	 * Start a ServerThread to listen for incomming connections
+	 * Start a ServerThread to listen for incoming connections
 	 * @see ServerThread
 	 */
 	private void startConnectionListener() {
-		Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
-		discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
-		startActivity(discoverableIntent);
+		if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
+			Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
+			discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
+			startActivity(discoverableIntent);
+		}
 
 		serverThread = new ServerThread(mHandler, getResources().getString(R.string.app_name));
 		serverThread.start();
@@ -129,7 +149,7 @@ public class BluetoothSyncActivity extends Activity{
 		mBluetoothAdapter.cancelDiscovery();
 		unregisterBroadcastReceiver();
 		
-		layout.removeView(listView);
+		listView.setVisibility(View.GONE);
 		String deviceName = socket.getRemoteDevice().getName();
 		mInfoText.setText("Synchronizing with " + deviceName + "...");	
 		
@@ -153,13 +173,12 @@ public class BluetoothSyncActivity extends Activity{
 				arrayAdapter.notifyDataSetChanged();
 			}else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
 				int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
-				Log.i("BluetoothSync", state + "");
 				if(state == BluetoothAdapter.STATE_ON){
 					startConnectionListener();
 					chooseDevice();
 				}else if(state == BluetoothAdapter.STATE_OFF ||  state == BluetoothAdapter.STATE_TURNING_OFF){
 					mInfoText.setText("Enable Bluetooth before synchronizing.");
-					layout.removeView(listView);
+					listView.setVisibility(View.GONE);
 				}
 			}
 		}
@@ -192,8 +211,7 @@ public class BluetoothSyncActivity extends Activity{
 	 */
 	private void setLayoutElement(){
 		mInfoText = (TextView) findViewById(R.id.bluetoothInfoText);
-		layout = (LinearLayout) findViewById(R.id.bluetoothLayout);
-		listView = new ListView(this);
+		listView = (ListView) findViewById(R.id.bluetoothListView);
 		listView.setAdapter(arrayAdapter);
 		listView.setOnItemClickListener(new OnItemClickListener() {
 			@Override
@@ -208,6 +226,18 @@ public class BluetoothSyncActivity extends Activity{
 		});		
 	}	
 	
+	private void cancelThreads(){
+		if(commThread != null) {
+			commThread.cancel();
+		}
+		if(clientThread != null){
+			clientThread.cancel();
+		}
+		if(serverThread != null){
+			serverThread.cancel();
+		}
+	}
+	
 	/**
 	 * Handles message sent from the background threads and updates UI.
 	 */
@@ -217,7 +247,7 @@ public class BluetoothSyncActivity extends Activity{
         public void handleMessage(Message msg) {
         	switch(msg.what){
         		case CONNECTING:       
-        			layout.removeView(listView);
+        			listView.setVisibility(View.GONE);
         			mInfoText.setText("Connecting to " + (String)msg.obj + "!");
         			break;
         		case CONNECTION_ESTABLISHED: 
@@ -231,6 +261,7 @@ public class BluetoothSyncActivity extends Activity{
         			mInfoText.setText("Synchronization successfull!");
         			break;	
         		case SYNC_FAILED: 
+        			commThread.cancel();
         			mInfoText.setText("Synchronization failed!");
         			break;	
         	}        		

+ 3 - 0
src/de/tudarmstadt/informatik/hostage/sync/bluetooth/CommunicationThread.java

@@ -58,6 +58,8 @@ public class CommunicationThread extends Thread {
 	/* Call this from the main activity to shutdown the connection */
 	public void cancel() {
 		try {
+			objectInput.close();
+			objectOuput.close();
 			mmSocket.close();
 		} catch (IOException e) {
 		}
@@ -77,6 +79,7 @@ public class CommunicationThread extends Thread {
 		} catch (ClassNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
+			cancel();
 			e.printStackTrace();
 			break;
 		}