关于蓝牙socket 服务器端首次accept总出错!

sunnysnowhi 2012-09-19 11:11:07
我使用蓝牙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处,程序都会意外终止,再重新点开程序试一次就不会出错了
这样很影响用户体验……


求各位大神帮帮忙!!感激不尽!!!!
虽然剩的分不多了……
...全文
891 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gao521521521 2014-09-12
  • 打赏
  • 举报
回复
这句话中( BluetoothServerSocket) listenMethod.invoke(mBluetoothAdapter, new Object[]{ 29}); 最后29代表啥呀??没弄懂。。。求大神指导一下
nonoyet 2012-12-30
  • 打赏
  • 举报
回复
我测试了一下代码,怎么连不上啊,也不报错,也连不上
sunnysnowhi 2012-10-07
  • 打赏
  • 举报
回复
我换了种写法

就参考网上说的


发送端
//建立socket连接
BluetoothSocket tmp = null;
mdevice = device;
try {
Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
try{

tmp = (BluetoothSocket)m.invoke(mdevice, Integer.valueOf(29));

}catch(Exception e){
System.out.println("Exception111");
e.printStackTrace();
}

}catch(NoSuchMethodException e){
System.out.println("NoSuchMethodException");
e.printStackTrace();
}
msocket = tmp;

接收端
Method listenMethod = null;
try {
listenMethod = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[]{int.class});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = ( BluetoothServerSocket) listenMethod.invoke(mBluetoothAdapter, new Object[]{ 29});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mmServerSocket = tmp;

结果就好了
AMinfo 2012-09-20
  • 打赏
  • 举报
回复
做个循环,当错误时重新执行过,直到无错误后退出循环,这个应该是UUID识别的问题。
sunnysnowhi 2012-09-20
  • 打赏
  • 举报
回复
楼上的我试了一下,还是不太行……

我在错误信息中发现如下行报错

@Override
public void onStart() {
super.onStart();

//启动服务器的线程
if (mAcceptThread == null) {
//点击打印出的错误信息,光标会移动到这里
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
}

public Handler getHandler(){
return this.handler;
}

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) {
//这里报出异常
System.out.println("Rfcomm出错"); 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);
socket = mmServerSocket.accept();
} catch (IOException e) {
e.printStackTrace();
finish();
break;
}
// If a connection was accepted
if (socket != null) {
try {


怎么解决呢 各位大牛们!
sunnysnowhi 2012-09-19
  • 打赏
  • 举报
回复
挥泪请求大牛帮助……

80,392

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧