============问题描述============
我使用蓝牙socket通信,服务器端使用socket = mmServerSocket.accept()这个语句,可是这里总是报错! 错误如下:
java.io.IOException: Not able to register SDP record for mango
at android.bluetooth.BluetoothAdapter.createNewRfcommSocketAndRecord(BluetoothAdapter.java:890)
at android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:813)
at com.card.test.receivecard$AcceptThread.<init>(receivecard.java:118)
at com.card.test.receivecard.onStart(receivecard.java:96)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3797)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
null
threadid=9: thread exiting with uncaught exception (group=0x401f6560)
ATAL EXCEPTION: Thread-10
java.lang.NullPointerException
at com.card.test.receivecard$AcceptThread.run(receivecard.java:132)
我的接收端代码片段如下:
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
private InputStream mmInStream;
public AcceptThread() {
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothServerSocket tmp = null;
try {
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(Name,uuid);
} catch (IOException e) {
e.printStackTrace();
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
System.out.println(mmServerSocket);//由打印信息知mmServerSocket为null
socket = mmServerSocket.accept(); //所以这里出错了
} catch (IOException e) {
e.printStackTrace();
finish();
break;
}
// If a connection was accepted
if (socket != null) {
try {
以下省略。。。
}
发送端代码片段如下:
private class ClientThread extends Thread {
private final BluetoothSocket msocket;
private final BluetoothDevice mdevice;
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
public ClientThread(BluetoothDevice device) {
//建立socket连接
BluetoothSocket tmp = null;
mdevice = device;
try {
tmp = mdevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) { e.printStackTrace(); }
msocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
msocket.connect();
} catch (IOException e) {
e.printStackTrace();
}
// Do work to manage the connection (in a separate thread)
try {
以下省略。。。
我这两段代码是在两个不同的activity里面写的(sendcard.activity和receivecard.activity).想要的功能是用蓝牙将名片发送出去,使用时,手机A点发送按钮,跳到sendcard.activity,手机B基本上同时点接收按钮,跳到receivecard.activity。但是问题是:几乎每次刚安装好程序,首次使用时,就会accept报错(如上文错误信息),然后多尝试几次发送接收,就能成功……
不知道我说清楚没 就是首次点接收按钮,运行到accept处,程序都会意外终止,再重新点开程序试一次就不会出错了
这样很影响用户体验……
求各位大神帮帮忙!!感激不尽!!!!
虽然剩的分不多了……
============解决方案1============
做个循环,当错误时重新执行过,直到无错误后退出循环,这个应该是UUID识别的问题。