我正在开发一款支持蓝牙的Android应用,以便在Androind和蓝牙模块之间进行通信。一切正常,但点击设备时会遇到异常。它说"无法开始服务活动"。
这一行有例外:
mmSocket.Connect();
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp" >
<Button
android:id="@+id/turnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/on" />
<Button
android:id="@+id/turnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/off" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="80dp" >
<Button
android:id="@+id/paired"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/List" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Find" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="200dp" >
</ListView>
</LinearLayout>
</RelativeLayout>
爪哇:
myListView = (ListView)findViewById(R.id.listView1);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
myBluetoothAdapter.cancelDiscovery();
final String info=((TextView)view).getText().toString();
String address=info.substring(info.length()-17);
device=myBluetoothAdapter.getRemoteDevice(address);
try{
mmSocket=device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
mmSocket.connect(); //EXCEPTION
sendInfo("A");
Toast.makeText(getApplicationContext(),"Bluetooth Connected",Toast.LENGTH_LONG).show();
}
catch (IOException e){
Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
logcat的:
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: s 62, cmd 3
04-20 21:17:37.025 1842-1842/com.example.app D/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: fd (-1:62), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: s 62, cmd 4
04-20 21:17:37.025 1842-1842/com.example.app D/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: fd (-1:62), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
04-20 21:17:37.025 1842-1842/com.example.app D/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: transparant fcntl
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: s 64, cmd 3
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: wsock not found, pass through transparantly
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: s 64, cmd 4
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_wrp_fcntl: wsock not found, pass through transparantly
04-20 21:17:37.025 1842-1842/com.example.app I/BLZ20_WRAPPER﹕ blz20_set_asocket: success (62,63,64)
04-20 21:17:37.025 1742-1742/? V/BluetoothEventRedirector﹕ Received android.bluetooth.adapter.action.DISCOVERY_FINISHED
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ java.io.IOException: Unable to start Service Discovery
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:374)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:203)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at com.example.app.MainActivity$5.onItemClick(MainActivity.java:125)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.widget.ListView.performItemClick(ListView.java:3561)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:587)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.os.Looper.loop(Looper.java:143)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4277)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:507)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-20 21:17:37.046 1842-1842/com.example.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-20 21:17:37.596 1760-1760/? I//system/bin/btld﹕ ##### USERIAL_Ioctl: BT_Sleep, USERIAL_IO_BT_WAKE_DEASSERT ####
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ got scan complete
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ wpa_supplicant_get_scan_results:return scan results2
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ AP:ssid[Ibrahim],rssi[-89],BSSID=64:66:b3:c9:05:22
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ AP:ssid[PTCL-BB],rssi[-95],BSSID=00:07:26:47:db:9b
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ AP:ssid[MUBASHIR],rssi[-61],BSSID=e8:de:27:b5:79:20
04-20 21:17:38.697 352-352/? I/wpa_supplicant﹕ AP:ssid[lynxnet03212360906],rssi[-87],BSSID=e8:de:27:b5:4e:0a