0

I created an application with bluetooth, but when bluetooth is closed, apk close with toast and I have this error... java.lang.NullPointerException in io.webbt.Activity2.openBT

void findBT()
{
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter == null)
    {
        myLabel.setText("No bluetooth adapter available");
    }

    if(!mBluetoothAdapter.isEnabled())
    {
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
    }     
}    

I have a message if I want open bluetooth I choise yes...but after not continue to my activity...Any help?How i come back to my activity if bluetooth is closed?

01-16 15:36:08.483: W/dalvikvm(3677): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-16 15:36:08.483: E/AndroidRuntime(3677): FATAL EXCEPTION: main
01-16 15:36:08.483: E/AndroidRuntime(3677): java.lang.RuntimeException: Unable to start activity ComponentInfo{io.webbt/io.webbt.Activity2}: java.lang.NullPointerException
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.os.Looper.loop(Looper.java:137)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at java.lang.reflect.Method.invoke(Method.java:511)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at dalvik.system.NativeStart.main(Native Method)
01-16 15:36:08.483: E/AndroidRuntime(3677): Caused by: java.lang.NullPointerException
01-16 15:36:08.483: E/AndroidRuntime(3677):     at io.webbt.Activity2.openBT(Activity2.java:192)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at io.webbt.Activity2.onStart(Activity2.java:130)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at   android.app.Activity.performStart(Activity.java:4475)
01-16 15:36:08.483: E/AndroidRuntime(3677):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
01-16 15:36:08.483: E/AndroidRuntime(3677):     ... 11 more

 void openBT() throws IOException
  {
  UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standar      
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);       
  mmSocket.connect();
  mmOutputStream = mmSocket.getOutputStream();
  mmInputStream = mmSocket.getInputStream();

  myLabel.setText("Bluetooth Opened");

  protected void onStart() {
    super.onStart();

    try
    {         
        //findBT();
        openBT();                            
    }
    catch (IOException ex) { }

    runnable.run();

}

1 Answers1

0

Do you have a BroadcastReceiver? i think it will help you to listen to the bluetooth status and the ability to do what you need in each status..

take a look here

Community
  • 1
  • 1
Elior
  • 3,178
  • 6
  • 37
  • 67