我正在开发两个蓝牙应用程序,它们都基于 bluetoothChat 示例,但其中一个也能够使用 USB 附件模式从微控制器发送/接收数据。这些应用程序在两个不同的设备上运行,一个 Nexus 4 和一个 V301(中国手机)。一般来说,一切正常,问题只是在第一次尝试时这两个设备无法建立蓝牙连接。bluetoothChat 应用程序根本无法建立连接,如果我尝试建立连接,另一个应用程序 (BT+USB) 会停止并崩溃。但问题是,在这次崩溃之后,一切正常,我最终能够从这两个应用程序建立连接。这是两个android manifest和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 应用(称为 bluetoothx10y)的 android manifest:
<?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);
}