# Handy -PC Verbindung



## nemo86 (28. Feb 2010)

Hallo alle,

ich möchte eine Anwendung schreiben bei der ein simuliertes Handy mit meinem PC kommuniziert. Implementiere ich die Kommunikation zwischen zwei Handys, die beide simuliert auf dem PC laufen, funktioniert alles perfekt, möchte ich aber den Client als PC Anwendung starten (die Logik nicht verändert), findet er den Server nicht. Alle externen Geräte wie z.B. mein Handy oder andere PC werden dabei erkannt, aber eben nicht das simulierte. Kann mir jemand sagen, wo das Problem liegt?


----------



## Atze (28. Feb 2010)

zeile 304, oder könnte auch 306 sein, schmeißt ne nullpointer ...

ne, mal im ernst, ohne code schwer zu sagen. vielleicht kann dir jemand helfen, wenn man code sieht


----------



## nemo86 (28. Feb 2010)

Alles klar Also Server aufm Handy


```
public class Server {
	private UUID serverUUID = new UUID("F0E0D0C0B0A000908070605040302010",
			false);
	private StreamConnectionNotifier notifer;
	private InputStream input;
	private OutputStream output;
	private boolean init;
	private String SERVER_RESPONSE = "Server is ready";
	private boolean isActive=false;
public Server() {
		initServer();
	}

	private void initServer() {
		try {
			LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);

			// prepare a URL to create a notifier
			StringBuffer url = new StringBuffer("btspp://");
			url.append("localhost").append(':');
			url.append(serverUUID.toString());
			url.append(";name=Diving Computer Server");
			url.append(";authorize=false");

			notifer = (StreamConnectionNotifier) Connector.open(url.toString());

			StreamConnection conn = notifer.acceptAndOpen();

			init = true;

			input = conn.openInputStream();
			output = conn.openOutputStream();
			
			isActive=true;
			System.out.println("*******Server*******");
			
			output.write(SERVER_RESPONSE.length());
			output.write(SERVER_RESPONSE.getBytes());

		} catch(BluetoothStateException e){
			System.out.println("Cannot initialize bluetooth");
			System.out.println(e);
		} catch (IOException e) {
			System.out.println("IOException");
			e.printStackTrace();
		}
	}
```

Und Client aufm PC

```
public class Client implements DiscoveryListener {
	private UUID serverUUID = new UUID("F0E0D0C0B0A000908070605040302010",
			false);
	private DiscoveryAgent agent;
	private boolean init = false;
	private boolean serverFound = false;
	private String url;
	private final String CLIENT_RESPONSE = "Client is ready";
	private OutputStream output;
	private InputStream input;
	private boolean isActive = false;
	private boolean complete=false;
	
	public boolean isActive(){
		return isActive;
	}
	public boolean isCompleted(){
		return complete;
	}

	public Client() {
	}

	public void searchDevices() {
		try {
			agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
			agent.startInquiry(DiscoveryAgent.GIAC, this);
		} catch (BluetoothStateException e) {
			System.out.println("Problem in searching the blue tooth devices");
			System.out.println(e.getMessage());
		}
	}

	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
		try {
			// Get Device Info
			System.out.println("Device Discovered");
			System.out.println("Major Device Class: "
					+ cod.getMajorDeviceClass() + " Minor Device Class: "
					+ cod.getMinorDeviceClass());
			System.out.println("Bluetooth Address: "
					+ btDevice.getBluetoothAddress());
			System.out.println("Bluetooth Friendly Name: "
					+ btDevice.getFriendlyName(true));

			// Search for Services
			UUID uuidSet[] = new UUID[1];
			uuidSet[0] = serverUUID;
			agent.searchServices(null, uuidSet, btDevice, this);
		} catch (Exception e) {
			System.out.println("Device Discovered Error: " + e);
		}
	}

	public void inquiryCompleted(int discType) {
		System.out.println("InquiryCompleted");
		complete=true;
	}

	public void serviceSearchCompleted(int transId, int respCode) {
		StreamConnection conn;
		if (serverFound) {
			try { // lets the communication start by setting the url and send
				// client reponse
				conn = (StreamConnection) Connector.open(url);

				output = conn.openOutputStream();
				input = conn.openInputStream();

				System.out.println("*******Client*******");
				
				output.write(CLIENT_RESPONSE.length());
				output.write(CLIENT_RESPONSE.getBytes());

				isActive = true;
				System.out.println("serviceSearchCompleted");
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

	}

	public void servicesDiscovered(int transId, ServiceRecord[] records) {
		for (int i = 0; i < records.length; i++) {
			url = records[i].getConnectionURL(
					ServiceRecord.AUTHENTICATE_ENCRYPT, false);

			if (url.startsWith("btspp")) {
				init = true;
				serverFound = true;
				break;
			}
		}
	}
```


----------

