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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:07:05+00:00 2026-06-05T20:07:05+00:00

I’m working on an app using Bluetooth discovery pretty heavily (some detail here ).

  • 0

I’m working on an app using Bluetooth discovery pretty heavily (some detail here). The nature of the app is that I’m pretty much keeping the BT always on and waiting around for changes in BT names from some broadcasting devices. Power usage is not an issue since my device is permanently charging.

I’ve found that at certain times my app “loses the thread” and doesn’t discover anything on BT, even though BT is turned on, and there are devices in range. In a manual way, I can solve this by turning the BT off and on on the device – presto! It works again. So I wanted to do this in code. I have a whole timeout timer, which sends a message to a handler:

        bluetoothHandler = new Handler() {
        @Override
        public void  handleMessage(Message msg) {
            if (!btAdapter.isDiscovering())
            {
                btAdapter.disable();
                btAdapter.enable();
                btAdapter.startDiscovery();
                // some other stuff
            }

Well, this code seems to sometimes crash with a null pointer exception on the “disable” line

Here’s the ACRA info:

"java.lang.NullPointerException
    at android.os.Parcel.readException(Parcel.java:1328)
    at android.os.Parcel.readException(Parcel.java:1276)
    at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:604)
    at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:562)
    at com.myapp.stuff.MainActivity$8.handleMessage(MainActivity.java:574)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

As you can see, it doesn’t make sense that the btAdapter is really null otherwise we would have crashed on the line before (the “if”) – and even assuming the code is reentrant, I don’t ever null the btAdapter in my code at any point – I set it in OnCreate and leave it forever so how does it get to be null?

I saw a couple of people posting that they got NullPointerException doing code with enable/disable – and suggestions to use Intents to start/stop Bluetooth – this is not an option here – there is no user intervention – the user (if they want this rather specialized app to work) needs the BT to be running (and if not running right to be fixed) automatically.

I’m assuming I can’t put a try/catch around a NullPointerException (even though it is bogus) – does anyone know enough about the Bluetooth internals to figure out why disable is crashing on me?

  • 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-05T20:07:05+00:00Added an answer on June 5, 2026 at 8:07 pm

    Well, I don’t really understand what was going wrong, but my fix was fairly simple and stupid:

    Instead of leaving the one value around for btAdapter I check it each time before using it and try/catch for any exception (though i assume this wouldn’t capture a REAL null pointer)

    So every time I cll disable (which was crashing) I do this

                    btAdapter = BluetoothAdapter.getDefaultAdapter();
                    if (null == btAdapter)
                    {
                        Log.e(LOG_TAG,"Bluetooth adapter is NULL!!!");
                        return;
                    }
                    try {
                        btAdapter.disable();
                    }
                    catch (Exception e) {
                        //don't do anything
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

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.