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 8987429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:47:26+00:00 2026-06-15T21:47:26+00:00

I would like to save the last bluetooth device my application has connected to.

  • 0

I would like to save the last bluetooth device my application has connected to. I want to not prompt the user at all, if there is a previous bluetooth connection. They will have the option of connecting to a new device, but they do not need to. If they opt to not choose a connection, they will use the application regularly, and then when the bluetooth device is needed, it will connect to the most recent device.

I have tried using the code provided in Tudor Luca's answer below, but the object will not write to the file. I am getting a NotSerializableException. The object which I am trying to save is a BluetoothDevice which is imported with import android.bluetooth.BluetoothDevice.

This is what I have tried to do to make the bluetooth device serializable:

import java.io.Serializable;
import android.bluetooth.BluetoothDevice;

public class SerializableObjects implements Serializable {
    private BluetoothDevice device;

    public SerializableObjects( BluetoothDevice device ) {
        this.device = device;
    }

    public BluetoothDevice getDevice() {
        return this.device;
    }
}

The LogCat Returns this:

12-11 17:46:24.032: W/System.err(24641): java.io.NotSerializableException: android.bluetooth.BluetoothDevice
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
12-11 17:46:24.032: W/System.err(24641):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
12-11 17:46:24.032: W/System.err(24641):    at my.eti.commander.LocalObjects.writeObjectToFile(LocalObjects.java:29)
12-11 17:46:24.032: W/System.err(24641):    at my.eti.commander.MainMenu$1.handleMessage(MainMenu.java:460)
12-11 17:46:24.032: W/System.err(24641):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 17:46:24.036: W/System.err(24641):    at android.os.Looper.loop(Looper.java:130)
12-11 17:46:24.036: W/System.err(24641):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-11 17:46:24.036: W/System.err(24641):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 17:46:24.036: W/System.err(24641):    at java.lang.reflect.Method.invoke(Method.java:507)
12-11 17:46:24.036: W/System.err(24641):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-11 17:46:24.036: W/System.err(24641):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-11 17:46:24.036: W/System.err(24641):    at dalvik.system.NativeStart.main(Native Method)
  • 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-06-15T21:47:27+00:00Added an answer on June 15, 2026 at 9:47 pm

    There is no way to directly serialize the BluetoothDevice class. Even if you serialize it, I do not think you will be able to re-use the object after the application closed. Instead, I have a helper class that will store the address of the device. You can save the device address and name and read that information later. You can then perform a discovery/search for bonded devices and obtain corresponding device.

    Here is a helper class I typically use

    public class BluetoothState implements Serializable {
        public static final int STATE_NOT_CONNECTED = 1;
        public static final int STATE_CONNECTED = 1;
    
        public static final String filename = "btState.pref";
    
        public static int connectionState = STATE_NOT_CONNECTED;
        public static String deviceAddress = "00:00:00:00:00:00";
        public static String deviceName = "";
    
        public static void setConnectionState(boolean connected,BluetoothDevice device) {
            if(connected)
                connectionState = STATE_CONNECTED;
            else
                connectionState = STATE_NOT_CONNECTED;
    
            if(device != null) {
                deviceAddress = device.getAddress();
                deviceName = device.getName();
            }
    
        }
    
        public static void saveConnectionState(Context cxt) throws IOException {        
            FileOutputStream fos = cxt.openFileOutput(filename, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);       
            oos.writeInt(connectionState);
            oos.writeUTF(deviceAddress);
            oos.writeUTF(deviceName);
        }
    
        public static void loadConnectionState(Context cxt) throws IOException {        
            FileInputStream fis = cxt.openFileInput(filename);
            ObjectInputStream ois = new ObjectInputStream(fis);     
            connectionState = ois.readInt();
            deviceAddress = ois.readUTF();
            deviceName = ois.readUTF();
        }
    
        public static BluetoothDevice getDevice() {
            BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
            if(!btAdapter.isEnabled())
                btAdapter.enable();
    
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    
            for(BluetoothDevice d : pairedDevices)
                if(d.getAddress().equalsIgnoreCase(deviceAddress))
                    return d;
    
            return null;        
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developping a JavaEE application using glassfish, and I would like to save
I would like to display last save date automatically in a cell in excel.
I would like to save and load a set of images in Octave /
I would like to save query result into redis using JSON serialization and query
I would like to save and restore the state of a WinForms Form ,
Scenario I would like to save images with alpha transparency as .png and images
I have a plist where I would like to save a some response details
I have created a MySQL table and would like to save the contents of
I've made some experimental code that I would like to save in the repository,
I am studying javascript and I've got some problems. I would like to save

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.