服务发现失败

时间:2014-01-15 20:19:14

标签: android bluetooth

自从我尝试连接2个Android手机之后,它已经存在了3天多。 Whoz版本是4.1.2和4.0.4,但没有运气。我已经尝试了stackoverflow和谷歌的所有可能的解决方案。我面临的问题是,我可以使用以下方法发现我的应用程序的uuid但仍然当我尝试连接服务器时,我收到消息“服务发现失败”。需要一些帮助的人。我的服务器端代码如下所示,它有点乱,只关注未注释的行:)。评论的代码是我尝试但失败的代码,以及未注释的代码。

anotherDevice.fetchUuidsWithSdp();

发现uuid的方法(工作正常)

services name found ... but still getting service discovery failed message 

ERROR like java.io.IOException: Service discovery failed

package com.example.agent;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;




public class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;
    BluetoothAdapter mBluetoothAdapter;
    Context mContext;
    BluetoothDevice mmDevice;
//    String uuidString = "755e5400-77bc-11e3-981f-0800200c9a66";
    String  uuidString = "00001101-0000-1000-8000-00805F9B34FB";
    public AcceptThread(Context context,BluetoothDevice device) throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        // Use a temporary object that is later assigned to mmServerSocket,
        // because mmServerSocket is final
        mContext = context;
        mmDevice = device;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothServerSocket tmp = null;


        //TelephonyManager tManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
        //String uuid = tManager.getDeviceId();
        UUID uid = UUID.fromString(uuidString);
//       tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("HackingService", uid);
           tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Hackng", uid);
//         
//         Method m = null;
//      try {
//          m = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
//      } catch (NoSuchMethodException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
//         tmp = (BluetoothServerSocket) m.invoke(mBluetoothAdapter, 1);
//       tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mBluetoothAdapter, "Hacker", uid, false);

        mmServerSocket = tmp;
         start();
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                 socket = mmServerSocket.accept();
            } catch (IOException e) {
        //        break; 
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
     //           manageConnectedSocket(socket);
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }
}

这是我的客户端代码

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) { }
    }
}

0 个答案:

没有答案