public static String ErrorMessage="No Error";
private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static BluetoothAdapter myBluetoothAdapter;
private static BluetoothDevice myDevice;
private static BluetoothSocket mySocket = null;
private static OutputStream myOutStream = null;
private static InputStream myInStream = null;
public static boolean SPPOpen(BluetoothAdapter bluetoothAdapter, BluetoothDevice btDevice)
{boolean error=false;
myBluetoothAdapter = bluetoothAdapter;
myDevice = btDevice;
BluetoothSocket tmp=null;
if(!myBluetoothAdapter.isEnabled())
{
ErrorMessage = "蓝牙适配器没有打开";
return false;
}
try
{
Method m = myDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
try
{
tmp = (BluetoothSocket)m.invoke(myDevice, Integer.valueOf(1));
}catch(Exception e)
{
}
}catch(NoSuchMethodException e)
{
}
mySocket = tmp;
myBluetoothAdapter.cancelDiscovery();
try
{
mySocket.connect();
}
catch (IOException e2)
{
ErrorMessage = e2.getLocalizedMessage();//"无法连接蓝牙打印机";
mySocket = null;
return false;
}
try
{
myOutStream = mySocket.getOutputStream();
}
catch (IOException e3)
{
myOutStream = null;
error = true;
}
try
{
myInStream = mySocket.getInputStream();
}
catch (IOException e3)
{
myInStream = null;
error = true;
}
if(error)
{
SPPClose();
return false;
}
return true;
}