Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid bluetooth connection fails on the first try
    primarykey
    data
    text
    <p>I'm developing two bluetooth apps, both are based on the bluetoothChat sample but one of them is also able to send/receive data from a microcontroller using the USB Accessory mode. These apps run on two different devices, a Nexus 4 and a V301 (a chinese phone). In general everything works fine, the problem is just that on the first try the two devices are not able to make a bluetooth connection. The bluetoothChat app is simply not able to make a connection, the other app (BT+USB) stops and crashes if I try to make a connection. The thing is though, after this crash, everything works fine and I'm able to eventually make a connection from both the two apps. Here are the two android manifest and the error that comes out when the BT+USB app crashes. </p> <p>The error displayed by the Eclipse console:</p> <pre><code>06-24 12:40:10.090: W/dalvikvm(5886): threadid=1: thread exiting with uncaught exception (group=0x40a98390) 06-24 12:40:10.110: E/AndroidRuntime(5886): FATAL EXCEPTION: main 06-24 12:40:10.110: E/AndroidRuntime(5886): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.bluetoothx10y/com.example.bluetoothx10y.BluetoothChat}: java.lang.NullPointerException 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread.deliverResults(ActivityThread.java:2988) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3031) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread.access$1100(ActivityThread.java:126) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.os.Handler.dispatchMessage(Handler.java:99) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.os.Looper.loop(Looper.java:137) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread.main(ActivityThread.java:4482) 06-24 12:40:10.110: E/AndroidRuntime(5886): at java.lang.reflect.Method.invokeNative(Native Method) 06-24 12:40:10.110: E/AndroidRuntime(5886): at java.lang.reflect.Method.invoke(Method.java:511) 06-24 12:40:10.110: E/AndroidRuntime(5886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 06-24 12:40:10.110: E/AndroidRuntime(5886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 06-24 12:40:10.110: E/AndroidRuntime(5886): at dalvik.system.NativeStart.main(Native Method) 06-24 12:40:10.110: E/AndroidRuntime(5886): Caused by: java.lang.NullPointerException 06-24 12:40:10.110: E/AndroidRuntime(5886): at com.example.bluetoothx10y.BluetoothChat.onActivityResult(BluetoothChat.java:537) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.Activity.dispatchActivityResult(Activity.java:4649) 06-24 12:40:10.110: E/AndroidRuntime(5886): at android.app.ActivityThread.deliverResults(ActivityThread.java:2984) 06-24 12:40:10.110: E/AndroidRuntime(5886): ... 11 more </code></pre> <p>The android manifest of the BT+USB app (called bluetoothx10y):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetoothx10y" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="9" android:targetSdkVersion="15" /&gt; &lt;uses-feature android:name="android.hardware.usb.accessory"/&gt; &lt;uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH"/&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name="com.example.bluetoothx10y.BluetoothChat" android:label="@string/app_name" android:screenOrientation="landscape" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter"&gt; &lt;/meta-data&gt; &lt;/activity&gt; &lt;activity android:name=".DeviceListActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" android:screenOrientation="landscape" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The android manifest of the BT app (called BluetoothChat):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.BluetoothChat" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="6" android:targetSdkVersion="9" /&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt; &lt;application android:label="@string/app_name" android:icon="@drawable/app_icon" &gt; &lt;activity android:name=".BluetoothChat" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".DeviceListActivity" android:label="@string/select_device" android:theme="@android:style/Theme.Dialog" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>At line 536 and 537 I have:</p> <pre><code>mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device mChatService.connect(device); </code></pre> <p>mChatService is an object of the class responsible for setting up and managing the Bluetooth connection</p> <pre><code>public synchronized void connect(BluetoothDevice device) { if (D) Log.d(TAG, "connect to: " + device); // Cancel any thread attempting to make a connection if (mState == STATE_CONNECTING) { if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} } // Cancel any thread currently running a connection if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} // Start the thread to connect with the given device mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(STATE_CONNECTING); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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