/// <summary> /// Sends a contact request to the specified Bluetooth device. /// </summary> /// <param name="device">The device to send the request to.</param> /// <param name="myAddress">The local address to send to the device.</param> /// <returns>The Erebus address of the device if the request was accepted; otherwise null.</returns> public static ErebusAddress?RequestAddContact(RfcommDeviceService device, ErebusAddress myAddress) { Log.RecordEvent(typeof(BluetoothUtils), $"Attempting to add Bluetooth device '{device.ConnectionHostName.CanonicalName}' as a contact.", LogEntrySeverity.Info); try { var socket = new StreamSocket(); new Func <Task>(async() => { await socket.ConnectAsync(device.ConnectionHostName, RfcommAddContactService.AsString(), SocketProtectionLevel.BluetoothEncryptionWithAuthentication); })().Wait(); using (var r = new BinaryReader(socket.InputStream.AsStreamForRead())) using (var w = new BinaryWriter(socket.OutputStream.AsStreamForWrite())) { w.Write(myAddress); var addr = r.ReadErebusAddress(); var vkp = PlatformServiceProvider.Create("VerificationKeyProvider"); var local = vkp.CreatePrivateKey(); w.Write(local.Item2.Length); w.Write(local.Item2); vkp.AddKeyPair(addr, local.Item1, r.ReadBytes(r.ReadInt32())); Log.RecordEvent(typeof(BluetoothUtils), $"Successfully negotiated contact with {addr} via Bluetooth RFCOMM.", LogEntrySeverity.Info); return(addr); } } catch (Exception e) { Log.RecordEvent(typeof(BluetoothUtils), $"Exception adding contact: {e.Message}; assuming other user rejected request.", LogEntrySeverity.Error); return(null); } }
public void StartListening(RfcommServiceId serviceId) { _logger.Info("StartListen() in: serviceId={0}", serviceId.AsString()); // Initialize the provider for the hosted RFCOMM service //_provider = RfcommServiceProvider.CreateAsync(serviceId).AsTask().Result; _provider = _factories.ProviderFactory.Invoke(RfcommServiceId.SerialPort); // Create a listener for this service and start listening IStreamSocketListenerWrapper listener = _factories.ListenerFactory.Invoke(); //StreamSocketListener listener = new StreamSocketListener(); listener.ConnectionReceived += OnConnectionReceived; listener.BindServiceNameAsync(serviceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication).AsTask().Wait(); // Set the SDP attributes and start advertising //InitializeServiceSdpAttributes(_provider); _provider.StartAdvertising(listener); _logger.Info("StartListen() out"); }
/// <summary> /// Connects to the specified <see cref="RfcommDeviceService"/> and returns a <see cref="Stream"/> of the encrypted connection. /// </summary> /// <param name="device">The device to connect to.</param> /// <returns>The connection stream.</returns> public static dynamic Connect(RfcommDeviceService device) { Log.RecordEvent(typeof(BluetoothUtils), $"Attempting to connect to {device.ConnectionHostName.CanonicalName} via Bluetooth RFCOMM.", LogEntrySeverity.Info); var socket = new StreamSocket(); new Func <Task>(async() => { await socket.ConnectAsync(device.ConnectionHostName, RfcommConnectionService.AsString(), SocketProtectionLevel.BluetoothEncryptionWithAuthentication); })().Wait(); Log.RecordEvent(typeof(BluetoothUtils), $"Connection to {device.ConnectionHostName.CanonicalName} via Bluetooth RFCOMM was successful.", LogEntrySeverity.Info); return(new SplitStream(socket.InputStream.AsStreamForRead(), socket.OutputStream.AsStreamForWrite())); }