Android蓝牙连接首次尝试失败

时间:2013-06-24 11:11:12

标签: java android bluetooth

我正在开发两个蓝牙应用程序,两者都基于蓝牙的样本,但其中一个也能够使用USB附件模式从微控制器发送/接收数据。这些应用程序运行在两个不同的设备上,即Nexus 4和V301(中国手机)。 一般来说一切正常,问题只是在第一次尝试时,两个设备无法进行蓝牙连接。 bluetoothChat应用程序根本无法建立连接,如果我尝试建立连接,其他应用程序(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清单(称为bluetoothx10y):

<?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);
}

1 个答案:

答案 0 :(得分:0)

根据您的代码,mChatService == null这是导致NullPointerException的原因。在使用之前,您应该注意初始化它。如果不出意外,请使用lazy initialization作为字段。不过,我建议您尝试将代码设计为尽可能多的final字段。在对象的构造函数中初始化所需的所有内容,而不是尝试更改它们的值。引入具有可变(可设置)字段的可变状态将增加代码的复杂性并使其非常难以调试。特别是在多线程应用程序中。

注意:对于将来的问题,学习读取堆栈跟踪非常有用。这可能是一篇以http://marxsoftware.blogspot.hu/2010/10/reading-java-stack-traces-few-tips.html

开头的好文章