Search code examples
androidbluetoothscreen-orientation

Android BluetoothChat example: bluetooth connection lost upon screen rotation


I have just imported Android's BluetoothChat example into Eclipse. In the Android manifest, I can see that the option android:configChanges="orientation" was already there. However, when I tested the app on two real phones, the connection was always lost whenever I rotated the screen.

In another thread, a user reported that the connection was not lost when the android:configChanges="orientation" option was there, but this is not what I have experienced.

The only modification I have made to the code is to add the line

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

right after super.onCreate() in the onCreate() method. This change was made to fix a null pointer exception and I don't think it has anything to do with the screen rotation problem. Does anyone know what is the cause of lost connections and how to fix the issue?


Solution

  • Can you try this one? Should be able to do the trick.

    android:configChanges="orientation|screensize"
    

    Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize".

    From http://developer.android.com/guide/topics/resources/runtime-changes.html