Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:37:01+00:00 2026-05-25T13:37:01+00:00

I’m creating an app that has a bluetooth connection. I’ve created a library that

  • 0

I’m creating an app that has a bluetooth connection. I’ve created a library that uses callbacks every time a connection is lost/failed/established/etc. Everything about the connection works just fine, but I’m having a hard time handling orientation changes. I want the connection to remain open when the device changes orientation. At the same time I want the connection to close when the user closes the application. The API levels I’m designing this for are APIs 5-10. Here’s the issues I come across:

  1. If I close my connection on onDestroy(), the connection will close every time the app is closed but not when the app is simply paused (desired output). However, onDestroy() is called whenever the application orientation changes. Which leads to issue #2.

  2. If I use onRetainNonConfigurationInstance(), I can successfully keep the open socket. If I don’t close the socket in onDestroy(), then everything works just fine. However, the socket seems to remain open when the app closes (i.e. the other devices are still reading from it).

The setup I currently have looks similar to this:

BluetoothConnection btConnection;
@Override
public void onCreate(Bundle icicle)
{
   /** Activity setup **/

   btConnection = (BluetoothConnection) getLastNonConfigurationInstance();
   if(btConnection != null) // we already have a connection from a previous state, let's set it up to work with this state
   {
      /** Set up the connection since it already exists **/
   }
}

@Override
public Object onRetainNonConfigurationInstance()
{
   BluetoothConnection saveConnection = btConnection;
   return saveConnection;
}

@Override
protected void onDestroy()
{
   super.onDestroy()
   if(btConnection != null)
      btConnection.closeConnection();
}

With this current implementation, the socket will be saved but closed at the same time because both onRetainNonConfigurationInstance() is called as well as onDestroy().

I guess I’d like to know two things:

  1. Is onRetainNonConfigrationInstance() a good way to handle this problem to begin with?
  2. Is there a more elegant way to keep a connection open till an app closes? I’ve thought about creating a boolean like onRetainCalled to prevent the thing from closing, but it seems ugly to me (and possibly unreliable).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:37:02+00:00Added an answer on May 25, 2026 at 1:37 pm

    Optimally, you should put your Bluetooth connection code into a Service: I use this and it is quite excellent. You can open multiple Activities without having to think about it at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.