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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:31:02+00:00 2026-05-29T07:31:02+00:00

I am trying to adapt the Android BluetoothChat example to save the InputStream to

  • 0

I am trying to adapt the Android BluetoothChat example to save the InputStream to file using openFileOutput from within the BluetoothChatService class. When using the Environment.getExternalStorageDirectory and saving to the sdcard using filewriter there is no problem, but using the openFileOutput method with MODE_PRIVATE returns a nullPointerException as mentioned in several other questions/answers. I cant for the life of me figure out the correct way to get the correct context.

   private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private Context mContext;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Save to file
                    String FILENAME = "chat.txt";

                    FileOutputStream fos = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE);
                    fos.write(bytes);
                    fos.close();
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }
  • 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-29T07:31:04+00:00Added an answer on May 29, 2026 at 7:31 am

    Well, right now mContext is null. You’re not assigning anything to it anywhere. You could change your constructor signature to pass it in:

    public ConnectedThread(Context context, BluetoothSocket socket) {
        this.mContext = context;
        ...
    }
    

    If you’re actually following the example, you could do the same type of thing in the BluetoothChatService… note that they don’t do anything with the context passed in. Store that context off in a field and it would also be accessible to the ConnectedThread.

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

Sidebar

Related Questions

I'm trying to adapt this DES encrypting example to AES, so I made the
I'm working with Android in Eclipse and was trying to combine code from a
I'm trying to adapt Andy Langton's show/hide/mini-accordion ( http://andylangton.co.uk/jquery-show-hide ) to work within a
I'm currently trying to adapt my application to use the Compatibility Libraries for Android
I am trying to work through an android example given on the developers' page.
I am trying to adapt my data model to use a BaseEntity base-class. The
I'm trying to adapt this defstruct example by adding the select- functions described in
I'm trying to adapt an example provided by Apple in order to programmatically draw
I'm trying to adapt the following example: http://docs.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-ScrollDemoProject.zip The purpose of i want to
I am trying to make an android app that retrieves info from google app

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.