2014-11-05 78 views

回答

0

我做了這樣的:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("Some_MAC"); 
     BluetoothSocket tmp = null; 
// Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice 
     try { 
      mBluetoothAdapter.cancelDiscovery(); 
      Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null); 
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
      int sdk = Integer.parseInt(Build.VERSION.SDK); 
      if(sdk >= 10){ 
       //sdk 2.3?? java.io.IOException: Connection refused 
       tmp = device.createInsecureRfcommSocketToServiceRecord(uuid); 
      }else { 
       tmp = device.createRfcommSocketToServiceRecord(uuid); 
      } 
      Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
      tmp = (BluetoothSocket) m.invoke(device, 1); 
      tmp.connect(); 
} catch (IOException e) { 
      Log.e(TAG, "failed: ", e); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     }