蓝牙连接

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
 
private void connect(BluetoothDevice device) { 
     m_Device
= device; 
     
BluetoothSocket tmp = null; 
 
     
// Get a BluetoothSocket for a connection with the 
     
// given BluetoothDevice 
     
try { 
         tmp
= device.createRfcommSocketToServiceRecord(MY_UUID); 
     
} catch (IOException e) { 
 
     
} 
     m_Socket
= tmp; 
 
     m_BluetoothAdapter
.cancelDiscovery(); 
 
     
try { 
         
// This is a blocking call and will only return on a 
         
// successful connection or an exception 
         m_Socket
.connect(); 
     
} catch (IOException e) { 
     
try { 
       m_Socket
.close(); 
     
}catch (IOException e2) { 
 
         
} 
     
return; 
 
     
} 
用上述方法连接老是出现错误,不知道他人这样用过没 后来

tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
改为

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
         tmp
= (BluetoothSocket) m.invoke(device, 1); 
可以了 无语中

你可能感兴趣的:(socket)