Android 服务发现失败
自从我尝试连接两部android手机以来,已经有3天多的时间了。Whoz版本是4.1.2和4.0.4,但运气不好。我已经尝试了stackoverflow和google所有可能的解决方案。我面临的问题是,我可以使用以下方法发现我的应用程序的uuid,但当我尝试连接服务器时,仍然会收到消息“Service Discovery failed”。需要一些帮助。下面列出了我的服务器端代码,有点凌乱,只需关注未注释的行:)。注释代码是我尝试过但失败的代码,以及未注释的代码Android 服务发现失败,android,bluetooth,Android,Bluetooth,自从我尝试连接两部android手机以来,已经有3天多的时间了。Whoz版本是4.1.2和4.0.4,但运气不好。我已经尝试了stackoverflow和google所有可能的解决方案。我面临的问题是,我可以使用以下方法发现我的应用程序的uuid,但当我尝试连接服务器时,仍然会收到消息“Service Discovery failed”。需要一些帮助。下面列出了我的服务器端代码,有点凌乱,只需关注未注释的行:)。注释代码是我尝试过但失败的代码,以及未注释的代码 anotherDevice.fet
anotherDevice.fetchUuidsWithSdp();
发现uuid的方法(工作正常)
这是我的客户端代码
package com.example.c_agent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.widget.Toast;
//import com.example.agent.MainActivity;
import android.bluetooth.*;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.UUID;
public class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
BluetoothAdapter mBluetoothAdapter;
Context mContext;
public InputStream mmInStream;
public OutputStream mmOutStream;
// String uuidString = "755e5400-77bc-11e3-981f-0800200c9a66";
String uuidString = "00001101-0000-1000-8000-00805F9B34FB";
public ConnectThread(BluetoothDevice device, Context context) throws IOException, Throwable, IllegalAccessException, InvocationTargetException {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mContext = context;
BluetoothSocket tmp = null;
mmInStream = null;
mmOutStream = null;
mmDevice = device;
// MY_UUID is the app's UUID string, also used by the server code
// TelephonyManager tManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
// String uuid = tManager.getDeviceId();
UUID uid = UUID.fromString(uuidString);
// tmp = mmDevice.createRfcommSocketToServiceRecord(uid);
tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(uid);
// tmp = InsecureBluetooth.createRfcommSocketToServiceRecord(mmDevice, uid, false);
// Class<?> clazz = mmDevice.getClass();
// Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
//
// Method m = clazz.getMethod("createRfcommSocket", paramTypes);
// Object[] params = new Object[] {Integer.valueOf(1)};
//
// tmp = (BluetoothSocket) m.invoke(mmDevice, params);
// fallbackSocket.connect();
// Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
// tmp = (BluetoothSocket) m.invoke(device, 1);
mmSocket = tmp;
// mBluetoothAdapter.cancelDiscovery();
start();
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
while(true)
{
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
// try {
// Toast.makeText(this, connectException.getMessage(), Toast.LENGTH_LONG).show();
Log.d("Exception", connectException.getMessage());
// mmSocket.close();
// }
// catch (IOException closeException) { }
// return;
}
}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
package com.example.c_代理;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.lang.reflect.InvocationTargetException;
导入java.lang.reflect.Method;
导入android.widget.Toast;
//导入com.example.agent.main活动;
导入android.bluetooth.*;
导入android.telephony.TelephonyManager;
导入android.util.Log;
导入android.view.view.OnClickListener;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
导入java.util.UUID;
公共类ConnectThread扩展线程{
私人蓝牙插座;
专用最终蓝牙设备;
蓝牙适配器mBluetoothAdapter;
语境;
公共输入流;
公共输出流mmOutStream;
//字符串UUIString=“755e5400-77bc-11e3-981f-0800200c9a66”;
字符串UUIString=“00001101-0000-1000-8000-00805F9B34FB”;
公共连接线程(Bluetooth设备、上下文上下文)引发IOException、Throwable、IllegaAccessException、InvocationTargetException{
//使用稍后指定给mmSocket的临时对象,
//因为mmSocket是最终的
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mContext=上下文;
BluetoothSocket tmp=null;
mmInStream=null;
mmOutStream=null;
mmDevice=设备;
//MY_UUID是应用程序的UUID字符串,也由服务器代码使用
//TelephonyManager tManager=(TelephonyManager)mContext.getSystemService(Context.TELEPHONY_服务);
//字符串uuid=tManager.getDeviceId();
UUID uid=UUID.fromString(uuidString);
//tmp=mmDevice.createrFComSocketToServiceRecord(uid);
tmp=mmDevice.createInsurerCommsocketToServiceRecord(uid);
//tmp=unsecureBluetooth.createrFComSocketToServiceRecord(mmDevice,uid,false);
//类clazz=mmDevice.getClass();
//类[]paramTypes=新类[]{Integer.TYPE};
//
//方法m=clazz.getMethod(“createRfcommSocket”,paramTypes);
//Object[]params=新对象[]{Integer.valueOf(1)};
//
//tmp=(BluetoothSocket)m.invoke(mmDevice,params);
//fallbackset.connect();
//方法m=device.getClass().getMethod(“CreateRfcomSocket”,新类[]{int.Class});
//tmp=(BluetoothSocket)m.invoke(设备,1);
mmSocket=tmp;
//mBluetoothAdapter.cancelDiscovery();
start();
}
公开募捐{
//取消查找,因为它会减慢连接速度
mBluetoothAdapter.cancelDiscovery();
while(true)
{
试一试{
//通过插座连接设备。这将阻止
//直到它成功或抛出异常
mmSocket.connect();
}捕获(IOException-connectException){
//无法连接;请关闭插座并退出
//试一试{
//Toast.makeText(this,connectException.getMessage(),Toast.LENGTH_LONG.show();
Log.d(“Exception”,connectException.getMessage());
//mmSocket.close();
// }
//捕获(IOException closeException){}
//返回;
}
}
//执行管理连接的工作(在单独的线程中)
//manageConnectedSocket(mmSocket);
}
/**将取消正在进行的连接,并关闭套接字*/
公开作废取消(){
试一试{
mmSocket.close();
}捕获(IOE){}
}
}
你能解释一下什么和怎么不起作用吗?当我们启动服务器时。。。。。。并从服务器的客户端获取UUID,我们成功地获取了UUID。。。。。。。但是当我们尝试连接时,我们收到消息“服务发现失败”,我也面临同样的问题。。。。如果有人找到解决办法,请告诉我
package com.example.c_agent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.widget.Toast;
//import com.example.agent.MainActivity;
import android.bluetooth.*;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.UUID;
public class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
BluetoothAdapter mBluetoothAdapter;
Context mContext;
public InputStream mmInStream;
public OutputStream mmOutStream;
// String uuidString = "755e5400-77bc-11e3-981f-0800200c9a66";
String uuidString = "00001101-0000-1000-8000-00805F9B34FB";
public ConnectThread(BluetoothDevice device, Context context) throws IOException, Throwable, IllegalAccessException, InvocationTargetException {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mContext = context;
BluetoothSocket tmp = null;
mmInStream = null;
mmOutStream = null;
mmDevice = device;
// MY_UUID is the app's UUID string, also used by the server code
// TelephonyManager tManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
// String uuid = tManager.getDeviceId();
UUID uid = UUID.fromString(uuidString);
// tmp = mmDevice.createRfcommSocketToServiceRecord(uid);
tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(uid);
// tmp = InsecureBluetooth.createRfcommSocketToServiceRecord(mmDevice, uid, false);
// Class<?> clazz = mmDevice.getClass();
// Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
//
// Method m = clazz.getMethod("createRfcommSocket", paramTypes);
// Object[] params = new Object[] {Integer.valueOf(1)};
//
// tmp = (BluetoothSocket) m.invoke(mmDevice, params);
// fallbackSocket.connect();
// Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
// tmp = (BluetoothSocket) m.invoke(device, 1);
mmSocket = tmp;
// mBluetoothAdapter.cancelDiscovery();
start();
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
while(true)
{
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
// try {
// Toast.makeText(this, connectException.getMessage(), Toast.LENGTH_LONG).show();
Log.d("Exception", connectException.getMessage());
// mmSocket.close();
// }
// catch (IOException closeException) { }
// return;
}
}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}