Note that there are some explanatory texts on larger screens.

plurals
  1. POApp crashes when trying to connect to bluetooth android
    text
    copied!<p>I am trying to establish a bluetooth connection between my phone and a bluetooth device, but the app keeps crashing. By commenting, i have found out that the error is in the openBT() function. Can anyone help me out please?</p> <pre><code> import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class PageOne extends Activity { TextView myLabel; TextView deviceFound; Button openButton,closeButton; BluetoothAdapter mBluetoothAdapter; BluetoothDevice mmDevice; BluetoothSocket mmSocket; OutputStream mmOutputStream; InputStream mmInputStream; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pageone); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); setUp(); openButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (openButton.getText().equals("Enable")) { findBT(); } if(openButton.getText().equals("Start Connection")){ System.out.println("here"); try{ openBT(); }catch (IOException e){e.printStackTrace();}; } } }); closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { closeBT(); System.out.println("here too"); } }); } private void setUp() { openButton = (Button) findViewById(R.id.button1); myLabel = (TextView) findViewById(R.id.textView1); closeButton = (Button) findViewById(R.id.button2); deviceFound = (TextView) findViewById(R.id.textView2); setButtonText(); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { setButtonText(); } }; IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver (receiver, filter); } private void setButtonText() { closeButton.setText("Disable bluetooth"); if (mBluetoothAdapter.isEnabled()) { openButton.setText("Start Connection"); myLabel.setText("Bluetooth is enabled"); } else { openButton.setText("Enable"); myLabel.setText("Bluetooth is disabled"); } } private void findBT(){ if(mBluetoothAdapter == null) { myLabel.setText("No bluetooth adapter available"); } if (!mBluetoothAdapter.isEnabled()) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } Set&lt;BluetoothDevice&gt; pairedDevices = mBluetoothAdapter.getBondedDevices(); if(pairedDevices.size() &gt; 0) { for(BluetoothDevice device : pairedDevices) { if(device.getName().equals("Hauwa")) { mmDevice = device; break; } } } deviceFound.setText("Bluetooth Device Found"); } private void closeBT(){ if (mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.disable(); deviceFound.setText("bvnbvnvb"); } } void openBT() throws IOException{ final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); } } </code></pre> <p>and here is the logcat error</p> <pre><code> 01-07 02:55:23.189: W/dalvikvm(11382): threadid=1: thread exiting with uncaught exception (group=0x401f0560) 01-07 02:55:23.189: E/AndroidRuntime(11382): FATAL EXCEPTION: main 01-07 02:55:23.189: E/AndroidRuntime(11382): java.lang.NullPointerException 01-07 02:55:23.189: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147) 01-07 02:55:23.189: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.view.View.performClick(View.java:2579) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.view.View$PerformClick.run(View.java:9246) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Handler.handleCallback(Handler.java:587) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Handler.dispatchMessage(Handler.java:92) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Looper.loop(Looper.java:130) 01-07 02:55:23.189: E/AndroidRuntime(11382): at android.app.ActivityThread.main(ActivityThread.java:3735) 01-07 02:55:23.189: E/AndroidRuntime(11382): at java.lang.reflect.Method.invokeNative(Native Method) 01-07 02:55:23.189: E/AndroidRuntime(11382): at java.lang.reflect.Method.invoke(Method.java:507) 01-07 02:55:23.189: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 01-07 02:55:23.189: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662) 01-07 02:55:23.189: E/AndroidRuntime(11382): at dalvik.system.NativeStart.main(Native Method) 01-07 02:55:23.199: E/AndroidRuntime(11382): [Blue Error Handler] Make Debugging Report file for main 01-07 02:55:23.199: E/AndroidRuntime(11382): java.lang.NullPointerException 01-07 02:55:23.199: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147) 01-07 02:55:23.199: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.view.View.performClick(View.java:2579) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.view.View$PerformClick.run(View.java:9246) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Handler.handleCallback(Handler.java:587) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Handler.dispatchMessage(Handler.java:92) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Looper.loop(Looper.java:130) 01-07 02:55:23.199: E/AndroidRuntime(11382): at android.app.ActivityThread.main(ActivityThread.java:3735) 01-07 02:55:23.199: E/AndroidRuntime(11382): at java.lang.reflect.Method.invokeNative(Native Method) 01-07 02:55:23.199: E/AndroidRuntime(11382): at java.lang.reflect.Method.invoke(Method.java:507) 01-07 02:55:23.199: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 01-07 02:55:23.199: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662) 01-07 02:55:23.199: E/AndroidRuntime(11382): at dalvik.system.NativeStart.main(Native Method) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload