[英]Android bluetooth connection fails on the first try
我正在開發兩個藍牙應用程序,它們都基於bluetoothChat示例,但是其中一個還能夠使用USB附件模式從微控制器發送/接收數據。 這些應用程序可以在兩種不同的設備上運行,分別是Nexus 4和V301(中文手機)。 通常,一切正常,問題就在於,第一次嘗試時,這兩個設備無法建立藍牙連接。 藍牙聊天應用程序根本無法建立連接,如果我嘗試建立連接,另一個應用程序(BT + USB)停止並崩潰。 問題是,在崩潰之后,一切正常,我最終能夠從兩個應用程序建立連接。 這是兩個Android清單,以及BT + USB應用崩潰時出現的錯誤。
Eclipse控制台顯示的錯誤:
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
BT + USB應用程序的Android清單(稱為bluex10x):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothx10y"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-feature android:name="android.hardware.usb.accessory"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.bluetoothx10y.BluetoothChat"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter">
</meta-data>
</activity>
<activity android:name=".DeviceListActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
android:screenOrientation="landscape" />
</application>
</manifest>
BT應用程序的Android清單(稱為BluetoothChat):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.BluetoothChat"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="6"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:label="@string/app_name"
android:icon="@drawable/app_icon" >
<activity android:name=".BluetoothChat"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeviceListActivity"
android:label="@string/select_device"
android:theme="@android:style/Theme.Dialog" />
</application>
</manifest>
在536和537行,我有:
mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device
mChatService.connect(device);
mChatService是該類的對象,負責建立和管理藍牙連接
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);
}
根據您的代碼, mChatService == null
這就是導致NullPointerException的原因。 使用前,應注意對其進行初始化。 如果沒有其他問題,請對該字段使用延遲初始化 。 但是,我建議您嘗試將代碼設計為具有盡可能多的final
字段。 在對象的構造函數中初始化您需要的所有內容,而不嘗試更改其值。 通過可變(可設置)字段引入可變狀態會增加代碼的復雜性,並使其真正難以調試。 特別是在多線程應用程序中。
注意:對於以后的問題,學習讀取堆棧跟蹤信息非常有用。 從http://marxsoftware.blogspot.hu/2010/10/reading-java-stack-traces-few-tips.html開始,這可能是一篇不錯的文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.