2012-04-26 122 views
4

我有一堆設備是不可發現的,但我知道他們的MAC地址。目前我可以使用ConnectThread連接多個設備:Android - 連接到多個藍牙設備無需配對

Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class }); 

tmp = (BluetoothSocket) m.invoke(device, 1); 

問題是我想支持2.3之前的設備的不安全的RFCOMM。我在另一個答案,它可以讓我做這件事,而不是再found this

tmp = InsecureBluetooth.createRfcommSocketToServiceRecord(device,MY_UUID, true); 

其中在連接到一臺設備,無需配對完美的作品。

我的問題是我怎樣才能獲得兩全其美,並在InsecureBluetooth類中使用反射?或者是否需要在InsecureBluetooth類中完成,如果是這樣的話?以下是InsecureBluetooth類的相關部分:

private static BluetoothSocket createRfcommSocketToServiceRecord(
     BluetoothDevice device, int port, UUID uuid, boolean encrypt) 
     throws IOException { 
    try { 
     BluetoothSocket socket = null; 
     Constructor<BluetoothSocket> constructor = BluetoothSocket.class 
       .getDeclaredConstructor(int.class, int.class, boolean.class, 
         boolean.class, BluetoothDevice.class, int.class, ParcelUuid.class); 
     if (constructor == null) 
      throw new RuntimeException("can't find the constructor for socket"); 

     constructor.setAccessible(true); 
     Field f_rfcomm_type = BluetoothSocket.class 
       .getDeclaredField("TYPE_RFCOMM"); 
     f_rfcomm_type.setAccessible(true); 
     int rfcomm_type = (Integer) f_rfcomm_type.get(null); 
     socket = constructor.newInstance(new Object[] { rfcomm_type, -1, false, 
       true, device, port, uuid != null ? new ParcelUuid(uuid) : null }); 
     return socket; 
    } catch (NoSuchMethodException e) { 
     throw new RuntimeException(e); 
    } catch (NoSuchFieldException e) { 
     throw new RuntimeException(e); 
    } catch (IllegalAccessException e) { 
     throw new RuntimeException(e); 
    } catch (InstantiationException e) { 
     throw new RuntimeException(e); 
    } catch (InvocationTargetException e) { 
     if (e.getCause() instanceof IOException) { 
      throw (IOException) e.getCause(); 
     } 
     throw new RuntimeException(e.getCause()); 
    } 
} 

public static BluetoothSocket createRfcommSocketToServiceRecord(
     BluetoothDevice device, UUID uuid, boolean encrypt) throws IOException { 
    return createRfcommSocketToServiceRecord(device, -1, uuid, encrypt); 
} 

public static BluetoothSocket createRfcommSocket(BluetoothDevice device, 
     int port, boolean encrypt) throws IOException { 
    return createRfcommSocketToServiceRecord(device, port, null, encrypt); 
} 

回答

0

您可以使用以下類創建不安全的藍牙套接字連接。它會自動處理反射到提供此功能的SDK 2.3.3以下

import java.io.IOException; 
import java.lang.reflect.Constructor; 
import java.lang.reflect.Field; 
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.os.Binder; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.ParcelUuid; 
import android.util.Log; 

/** 
* Class to offer unsecure bluetooth socket connection.<br /> 
* It was intended to be used when building the app with API version below 10 (API < Android2.3.3) 
* @author waa 
* 
*/ 
@SuppressWarnings("all") 
public class UnpairedBluetooth { 
    static private class InUse extends RuntimeException { 
    } 

    public static BluetoothServerSocket listenUsingRfcommWithServiceRecord(BluetoothAdapter adapter, String name, UUID uuid, boolean encrypt) throws IOException { 
     try { 
      Class c_rfcomm_channel_picker = null; 
      Class[] children = BluetoothAdapter.class.getDeclaredClasses(); 
      for(Class c : children) { 
       Log.e("TO", "class " + c.getCanonicalName()); 
       if(c.getCanonicalName().equals(BluetoothAdapter.class.getName() + ".RfcommChannelPicker")) { 
        c_rfcomm_channel_picker = c; 
        break; 
       } 
      } 
      if(c_rfcomm_channel_picker == null) 
       throw new RuntimeException("can't find the rfcomm channel picker class"); 

      Constructor constructor = c_rfcomm_channel_picker.getDeclaredConstructor(UUID.class); 
      if(constructor == null) 
       throw new RuntimeException("can't find the constructor for rfcomm channel picker"); 
      Object rfcomm_channel_picker = constructor.newInstance(new Object[] {uuid}); 
      Method m_next_channel = c_rfcomm_channel_picker.getDeclaredMethod("nextChannel", new Class[] {}); 
      m_next_channel.setAccessible(true); 

      BluetoothServerSocket socket = null; 

      int channel; 
      int errno; 
      while (true) { 
       channel = (Integer)m_next_channel.invoke(rfcomm_channel_picker, new Object[] {}); 

       if (channel == -1) { 
        throw new IOException("No available channels"); 
       } 

       try { 
        socket = listenUsingRfcomm(channel, encrypt); 
        break; 
       } catch(InUse e) { 
        continue; 
       } 
      } 

      Field f_internal_service = adapter.getClass().getDeclaredField("mService"); 
      f_internal_service.setAccessible(true); 
      Object internal_service = f_internal_service.get(adapter); 

      Method m_add_rfcomm_service_record = internal_service.getClass().getDeclaredMethod("addRfcommServiceRecord", new Class[] {String.class, ParcelUuid.class, int.class, IBinder.class}); 
      m_add_rfcomm_service_record.setAccessible(true); 

      int handle = (Integer)m_add_rfcomm_service_record.invoke(internal_service, new Object[] { name, new ParcelUuid(uuid), channel, new Binder() }); 

      if (handle == -1) { 
       try { 
        socket.close(); 
       } catch (IOException e) {} 
       throw new IOException("Not able to register SDP record for " + name); 
      } 
      Field f_internal_handler = adapter.getClass().getDeclaredField("mHandler"); 
      f_internal_handler.setAccessible(true); 
      Object internal_handler = f_internal_handler.get(adapter); 

      Method m_set_close_handler = socket.getClass().getDeclaredMethod("setCloseHandler", new Class[] {Handler.class, int.class}); 
      m_set_close_handler.setAccessible(true); 

      m_set_close_handler.invoke(socket, new Object[] { internal_handler, handle}); 
      return socket; 
     } catch (NoSuchMethodException e) { 
      throw new RuntimeException(e); 
     } catch (NoSuchFieldException e) { 
      throw new RuntimeException(e); 
     } catch(IllegalAccessException e) { 
      throw new RuntimeException(e); 
     } catch(InstantiationException e) { 
      throw new RuntimeException(e); 
     } catch(InvocationTargetException e) { 
      if(e.getCause() instanceof IOException) { 
       throw (IOException)e.getCause(); 
      } 
      throw new RuntimeException(e.getCause()); 
     } 
    } 
    private static BluetoothServerSocket listenUsingRfcomm(/*BluetoothAdapter adapter, */ int port, boolean encrypt, boolean reuse) throws IOException, InUse { 
     BluetoothServerSocket socket = null; 
     try { 
      Constructor<BluetoothServerSocket> constructor = BluetoothServerSocket.class.getDeclaredConstructor(int.class, boolean.class, boolean.class, int.class); 
      if(constructor == null) 
       throw new RuntimeException("can't find the constructor"); 
      constructor.setAccessible(true); 
      Field f_rfcomm_type = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM"); 
      f_rfcomm_type.setAccessible(true); 
      int rfcomm_type = (Integer)f_rfcomm_type.get(null); 

      Field f_e_addr_in_use = BluetoothSocket.class.getDeclaredField("EADDRINUSE"); 
      f_e_addr_in_use.setAccessible(true); 
      int e_addr_in_use = (Integer)f_e_addr_in_use.get(null); 

      socket = constructor.newInstance(new Object[] { rfcomm_type, false, encrypt, port }); 

      Field f_internal_socket = socket.getClass().getDeclaredField("mSocket"); 
      f_internal_socket.setAccessible(true); 
      Object internal_socket = f_internal_socket.get(socket); 
      Method m_bind_listen = internal_socket.getClass().getDeclaredMethod("bindListen", new Class[] {}); 
      m_bind_listen.setAccessible(true); 
      Object result = m_bind_listen.invoke(internal_socket, new Object[] {}); 

      int errno = (Integer)result; 
      if(reuse && errno == e_addr_in_use) { 
       throw new InUse(); 
      } else if (errno != 0) { 
       try { 
        socket.close(); 
       } catch (IOException e) {} 
       internal_socket.getClass().getMethod("throwErrnoNative", new Class[] {int.class}).invoke(internal_socket, new Object[] { errno }); 
      } 
      return socket; 
     } catch (NoSuchMethodException e) { 
      throw new RuntimeException(e); 
     } catch (NoSuchFieldException e) { 
      throw new RuntimeException(e); 
     } catch(IllegalAccessException e) { 
      throw new RuntimeException(e); 
     } catch(InstantiationException e) { 
      throw new RuntimeException(e); 
     } catch(InvocationTargetException e) { 
      if(e.getCause() instanceof IOException) { 
       throw (IOException)e.getCause(); 
      } 
      throw new RuntimeException(e.getCause()); 
     } 
    } 
    public static BluetoothServerSocket listenUsingRfcomm(int port, boolean encrypt) throws IOException { 
     return listenUsingRfcomm(port, encrypt, false); 
    } 
    private static BluetoothSocket createRfcommSocketToServiceRecord(BluetoothDevice device, int port, UUID uuid, boolean encrypt) throws IOException { 
     try { 
      BluetoothSocket socket = null; 
      Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
        int.class, int.class, boolean.class, boolean.class, BluetoothDevice.class, int.class, ParcelUuid.class); 
      if(constructor == null) 
       throw new RuntimeException("can't find the constructor for socket"); 

      constructor.setAccessible(true); 
      Field f_rfcomm_type = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM"); 
      f_rfcomm_type.setAccessible(true); 
      int rfcomm_type = (Integer)f_rfcomm_type.get(null); 
//   socket = constructor.newInstance(new Object[] { rfcomm_type, -1, false, true, device, port, uuid != null ? new ParcelUuid(uuid) : null}); 
      socket = constructor.newInstance(new Object[] { rfcomm_type, -1, false, encrypt, device, port, uuid != null ? new ParcelUuid(uuid) : null}); 
      return socket; 
     } catch (NoSuchMethodException e) { 
      throw new RuntimeException(e); 
     } catch (NoSuchFieldException e) { 
      throw new RuntimeException(e); 
     } catch(IllegalAccessException e) { 
      throw new RuntimeException(e); 
     } catch(InstantiationException e) { 
      throw new RuntimeException(e); 
     } catch(InvocationTargetException e) { 
      if(e.getCause() instanceof IOException) { 
       throw (IOException)e.getCause(); 
      } 
      throw new RuntimeException(e.getCause()); 
     } 
    } 
    public static BluetoothSocket createRfcommSocketToServiceRecord(BluetoothDevice device, UUID uuid, boolean encrypt) throws IOException{ 
     return createRfcommSocketToServiceRecord(device, -1, uuid, encrypt); 
    } 
    public static BluetoothSocket createRfcommSocket(BluetoothDevice device, int port, boolean encrypt) throws IOException { 
     return createRfcommSocketToServiceRecord(device, port, null, encrypt); 
    } 
} 

欲瞭解更多信息,請參閱this鏈接。

+0

是的,這與我在我的問題中包含的類和鏈接相同。問題是,當我在每個設備上執行一個while循環時,它會覆蓋我使用此方法時已連接到的設備。我會更新我的問題來解釋我的意思。 – 7wonders 2012-04-26 12:46:34

+0

我認爲你可以連接到一個設備在同一時間爲相同的藍牙配置文件。不是嗎? – waqaslam 2012-04-26 12:54:19

+0

是的,但我想同時連接到多個設備,並在後臺執行每個設備的數據檢索。 – 7wonders 2012-04-26 13:22:58