// 藍牙設備連接程序
private class BluetoothConnect extends Thread
{
private BluetoothSocket tempSocket;
private BluetoothDevice tempDevice;
public BluetoothConnect(BluetoothDevice device)
{
BluetoothSocket temp = null;
tempDevice = device;
//以下程序用于獲取BluetoothSocket
try
{
temp = tempDevice.createRfcommSocketToServiceRecord(UUID.fromString(BluetoothCOM_UUID));
Toast.makeText(getApplication(), device.getAddress(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
tempSocket = temp;
}
public void run()
{
myBluetoothAdapter.cancelDiscovery();
//以下程序用于連接藍牙設備
try
{
tempSocket.connect();
// MainActivity.myBluetoothSocket=x;//此處注意
Message msg = new Message();
msg.arg1 = 1;
handler.sendMessage(msg);
}
catch (IOException e)
{
try
{
tempSocket.close();
Message msg = new Message();
msg.arg1 = 2;
handler.sendMessage(msg);
}
catch (IOException ee)
{
}
return;
}
} 連接原理,到底是怎么回事?
|