public void SendMessage(string message, ConnectedDevice recipient)
        {
            string  s = System.Environment.MachineName;
            string  r = recipient.name;
            Message m = new Message(s, r, message);

            recipient.SendMessage(m);
        }
 public void AddDevice(ConnectedDevice device)
 {
     Logging.Log.Info("Added " + device.name);
     if (devices.Find(device) == null)
     {
         devices.AddLast(device);
     }
     else
     {
         Logging.Log.Error("Device already exists");
     }
 }
Example #3
0
        private async void RecieveConnection(StreamSocketListener listener, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            Console.WriteLine("Connection Received from: " + listener.Information);

            try
            {
                socket = args.Socket;
                var device = await BluetoothDevice.FromHostNameAsync(socket.Information.RemoteHostName);

                writer = new DataWriter(socket.OutputStream);
                var reader          = new DataReader(socket.InputStream);
                var connectedDevice = new ConnectedDevice(device.Name, device, writer, reader, netctl);
                netctl.AddDevice(connectedDevice);
                Logging.Log.Trace("Connected to Client: " + device.Name);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error while creating socket: " + e.Message);
            }
        }
 public void RemoveDevice(ConnectedDevice device)
 {
     Logging.Log.Trace("Removing " + device.name);
     Logging.Log.Info(device.name + " Has Disconnected.");
     devices.Remove(device);
 }
Example #5
0
        private async void Connect(DeviceInformation devInfo)
        {
            try
            {
                Logging.Log.Trace("Attempting to connect to Bluetooth Device: " + devInfo.Name);
                targetDevice = await BluetoothDevice.FromIdAsync(devInfo.Id);

                targetDevice.ConnectionStatusChanged += new TypedEventHandler <BluetoothDevice, object>(async(btd, obj) =>
                {
                    Logging.Log.Trace("Changed Connection Status for " + btd.Name + " to " + btd.ConnectionStatus);
                });

                if (targetDevice != null)
                {
                    const BluetoothCacheMode bluetoothCacheMode = BluetoothCacheMode.Uncached;
                    var targetBluetoothServices = await targetDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(Constants.Constants.broadcastGuid), bluetoothCacheMode);

                    Logging.Log.Trace("Searching Target Device " + devInfo.Name + " for Bluetooth Chat Service.");

                    var retrievedServices = targetBluetoothServices.Services;

                    if (retrievedServices.Count > 0)
                    {
                        Logging.Log.Trace("Bluetooth Chat Service Found on " + devInfo.Name);
                        var retrievedService = retrievedServices[0];
                        var attributes       = await retrievedService.GetSdpRawAttributesAsync();

                        var          attributeReader   = DataReader.FromBuffer(attributes[Constants.Constants.serviceNameID]);
                        var          attributeType     = attributeReader.ReadByte();
                        var          serviceNameLength = attributeReader.ReadByte();
                        StreamSocket bluetoothSocket   = null;
                        DataWriter   bluetoothWriter   = null;

                        //lock (this)
                        //  {
                        bluetoothSocket = new StreamSocket();
                        //}

                        await bluetoothSocket.ConnectAsync(retrievedService.ConnectionHostName, retrievedService.ConnectionServiceName);

                        bluetoothWriter = new DataWriter(bluetoothSocket.OutputStream);
                        DataReader chatReader = new DataReader(bluetoothSocket.InputStream);

                        Logging.Log.Trace("Connection to " + devInfo.Name + " Chat Service Established. Awaiting data...");

                        var connectedDevice = new ConnectedDevice(devInfo.Name, targetDevice, bluetoothWriter, chatReader, netctl);
                        netctl.AddDevice(connectedDevice);
                    }
                    else
                    {
                        Logging.Log.Trace("No valid services could be found on " + devInfo.Name + ". Ignoring.");
                    }
                }
                else
                {
                    Logging.Log.Trace("Target Device is Null.");
                }
            }
            catch (Exception ex)
            {
                Logging.Log.Error("An Exception Occured. The target device may not have an active service, or something went wrong.\n" + ex.Message);
                return;
            }
        }