public async Task Connect() { PeerFinder.AlternateIdentities["Bluetooth:SDP"] = ServiceGUID.ToString(); var peers = await PeerFinder.FindAllPeersAsync(); foreach (var p in peers) { var h = p.ServiceName; } // TODO: obviously list these instead of taking the first var peer = peers.FirstOrDefault(); if (peer != null) { var socket = new StreamSocket(); try { // default service name? await socket.ConnectAsync(peer.HostName, ServiceName); var connection = new DuplexConnection(socket); var device = new BluetoothDevice(peer.DisplayName, peer.HostName, peer.ServiceName); if (ConnectionEstablished != null) { ConnectionEstablished(this, new ClientConnectedEventArgs(device, connection)); } } catch (Exception ex) { } } }
private void ListenerOnConnectionReceived(StreamSocketListener Listener, StreamSocketListenerConnectionReceivedEventArgs Args) { var deviceName = ""; var serviceName = ""; HostName hostName = null; try { // Only supports a single connection for the moment so we won't need these again _serviceProvider.StopAdvertising(); Listener.Dispose(); _socket = Args.Socket; _writer = new DataWriter(_socket.OutputStream); _reader = new DataReader(_socket.InputStream); if (_socket.Information != null) { // Turns out this only gives us the host address and an // arbitrary number for service name. Pretty useless. deviceName = _socket.Information.RemoteHostName.DisplayName; hostName = _socket.Information.RemoteHostName; serviceName = _socket.Information.RemoteServiceName; } } catch (Exception ex) { ServerState = BluetoothServerState.Faulted; if (ConnectionError != null) ConnectionError(this, ex); return; } ServerState = BluetoothServerState.Connected; if (ClientConnected != null) { var device = new BluetoothDevice(deviceName, hostName, serviceName); var connection = new DuplexConnection(_reader, _writer); ClientConnected(this, new ClientConnectedEventArgs(device, connection)); } }