通过反射调用Android的L2CAP接口

目前的Android蓝牙接口里,通过BluetoothDevice,我们只能调用到RFCOMM和SCO。

更底层的L2CAP接口并没有显式的提供出来。

 

其实通过java的反射机制,我们也是是可以调用到L2CAP接口的。

[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.lang.reflect.Constructor;  
  3. import java.lang.reflect.InvocationTargetException;  
  4. import android.bluetooth.BluetoothSocket;  
  5.   
  6. public class BluetoothSocketFactory  
  7. {  
  8.     public static final int TYPE_RFCOMM = 1;  
  9.     public static final int TYPE_SCO = 2;  
  10.     public static final int TYPE_L2CAP = 3;  
  11.   
  12.     // address must use upper case  
  13.     public static final BluetoothSocket createBluetoothSocket(int type, int fd,  
  14.             boolean auth, boolean encrypt, String address, int port)  
  15.             throws IOException  
  16.     {  
  17.         BluetoothSocket socket = null;  
  18.         try  
  19.         {  
  20.             Constructor<BluetoothSocket> cs = BluetoothSocket.class  
  21.                     .getDeclaredConstructor(int.classint.class,  
  22.                             boolean.classboolean.class, String.class,  
  23.                             int.class);  
  24.             if (!cs.isAccessible())  
  25.             {  
  26.                 cs.setAccessible(true);  
  27.             }  
  28.             socket = cs.newInstance(type, fd, auth, encrypt, address, port);  
  29.         }  
  30.         catch (SecurityException e)  
  31.         {  
  32.         }  
  33.         catch (NoSuchMethodException e)  
  34.         {  
  35.         }  
  36.         catch (IllegalArgumentException e)  
  37.         {  
  38.         }  
  39.         catch (InstantiationException e)  
  40.         {  
  41.         }  
  42.         catch (IllegalAccessException e)  
  43.         {  
  44.         }  
  45.         catch (InvocationTargetException e)  
  46.         {  
  47.         }  
  48.         return socket;  
  49.     }  
  50.   
  51.     public static void main(String args[])  
  52.     {  
  53.         BluetoothSocket socket = null;  
  54.         try  
  55.         {  
  56.             socket = BluetoothSocketFactory.createBluetoothSocket(  
  57.                     BluetoothSocketFactory.TYPE_L2CAP, -1falsefalse,  
  58.                     "00:0B:24:35:4E:86"22135);  
  59.             socket.connect();  
  60.         }  
  61.         catch (IOException e)  
  62.         {  
  63.         }  
  64.         finally  
  65.         {  
  66.             if (null != socket)  
  67.             {  
  68.                 try  
  69.                 {  
  70.                     socket.close();  
  71.                 }  
  72.                 catch (IOException e)  
  73.                 {  
  74.                 }  
  75.             }  
  76.         }  
  77.     }  
  78. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值