コード例 #1
0
 private async Task Read()
 {
     try
     {
         byte[] buffer = new byte[port.ReadBufferSize];
         while (port.IsOpen)
         {
             int dataLength = 0;
             try
             {
                 dataLength = await port.BaseStream.ReadAsync(buffer, 0, 1);
             }
             catch (IOException)
             {
                 throw new PortClosedException();
             }
             byte[] data = new byte[dataLength];
             Array.Copy(buffer, data, dataLength);
             BytesReceivedEventArgs eventArgs = new BytesReceivedEventArgs(data);
             OnDataReceived(eventArgs);
         }
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
         OnReadExceptionOccured(eventArgs);
     }
 }
コード例 #2
0
        private async Task StartWrite(byte[] data)
        {
            try
            {
                DataWriter dataWriter   = new DataWriter();
                int        bytesSent    = 0;
                int        maxSliceSize = 20; //empirically determined maximum (index out of array bonds exception in gattCharacteristic_.WriteValueAsync(...) for higher values)

                while (bytesSent < data.Length)
                {
                    int sliceSize = ((data.Length - bytesSent) > maxSliceSize) ?     //if   more bytes remain than the maximum ble msg size
                                    maxSliceSize :                                   //     send maximum msg size amount of bytes
                                    (data.Length - bytesSent);                       //else send remaining bytes
                    byte[] slice = new byte[sliceSize];
                    System.Buffer.BlockCopy(data, bytesSent, slice, 0, sliceSize);   //copy the respective bytes from data to a new array called slice
                    dataWriter.WriteBytes(slice);
                    await characteristic.WriteValueAsync(dataWriter.DetachBuffer()); //send data slice to the bluetooth module

                    bytesSent += sliceSize;
                }
            }
            catch (Exception e)
            {
                ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
                OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
            }
        }
コード例 #3
0
        public async Task Read()
        {
            try
            {
                byte[] buffer = new byte[tcpClient.ReceiveBufferSize];
                while (true)
                {
                    if (!tcpClient.Connected)
                    {
                        throw new PortClosedException();
                    }
                    NetworkStream inputStream = tcpClient.GetStream();
                    int           dataLength  = await inputStream.ReadAsync(buffer, 0, 100);

                    if (dataLength == 0)
                    {
                        Close();
                        PortClosedEventArgs portClosedEventArgs = new PortClosedEventArgs("Remote tcp endpoint");
                        OnClose(portClosedEventArgs);
                        break;
                    }
                    byte[] data = new byte[dataLength];
                    Array.Copy(buffer, data, dataLength);
                    BytesReceivedEventArgs eventArgs = new BytesReceivedEventArgs(data);
                    OnDataReceived(eventArgs);
                }
            }
            catch (Exception e)
            {
                ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
                OnReadExceptionOccured(eventArgs);
            }
        }
コード例 #4
0
 protected void OnConnectionRequest(ConnectionRequestedEventArgs eventArgs)
 {
     byte[] bytes = eventArgs.bytes;
     if (bytes != null && bytes.Length == connectionRequestMessageLength)
     {
         try
         {
             Client.TYPE type      = Client.Identify((byte)(bytes[0]));
             string      name      = Client.typenames[type] + "_" + Encoding.ASCII.GetString(bytes, 1, 2);
             Client      newClient = Client.CreateNew(type, name, this);
             ClientConnectionRequestedEventArgs newClientEventArgs = new ClientConnectionRequestedEventArgs(newClient);
             OnClientConnectionRequested(newClientEventArgs);
         }
         catch (Exception e)
         {
             ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
             OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
             StartWaitingForConnectionRequest();
         }
     }
     else
     {
         StartWaitingForConnectionRequest();
     }
 }
コード例 #5
0
        private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            BluetoothLEDevice device = null;

            try
            {
                watcher.Stop();

                device = await ConnectToDevice(eventArgs);

                GattDeviceService service = GetServiceData(device, wantedServiceUuidString);

                GattCharacteristic characteristic = GetCharacteristicData(service, wantedCharacteristicUuidString);

                BLEPort blePort = new BLEPort(device, characteristic);

                device.ConnectionStatusChanged += async(BluetoothLEDevice dev, object o) =>
                {
                    try
                    {
                        if (dev.ConnectionStatus == BluetoothConnectionStatus.Connected)
                        {
                            GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
                                GattClientCharacteristicConfigurationDescriptorValue.Notify);

                            PortRequestedEventArgs portEventArgs = new PortRequestedEventArgs(blePort);
                            OnPortRequested(portEventArgs);
                        }
                    }
                    catch (Exception e)
                    {
                        if (dev != null)
                        {
                            dev.Dispose();
                        }

                        ExceptionOccuredEventArgs exceptionEventArgs = new ExceptionOccuredEventArgs(e);
                        OnWaitForPortConnectionsExceptionOccured(exceptionEventArgs);
                    }
                };
            }
            catch (Exception e)
            {
                if (device != null)
                {
                    device.Dispose();
                }

                ExceptionOccuredEventArgs exceptionEventArgs = new ExceptionOccuredEventArgs(e);
                OnWaitForPortConnectionsExceptionOccured(exceptionEventArgs);
            }
            finally
            {
                watcher.Start();
            }
        }
コード例 #6
0
 private void BLEDataReceived(GattCharacteristic gattCharacteristic, GattValueChangedEventArgs eventArgs)
 {
     try
     {
         DataReader dataReader = DataReader.FromBuffer(eventArgs.CharacteristicValue);
         byte[]     data       = new byte[dataReader.UnconsumedBufferLength];
         dataReader.ReadBytes(data);
         BytesReceivedEventArgs bytesReceivedEventArgs = new BytesReceivedEventArgs(data);
         OnDataReceived(bytesReceivedEventArgs);
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
         OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
     }
 }
コード例 #7
0
 public override void Write(byte[] data)
 {
     try
     {
         writeTask = port.BaseStream.WriteAsync(data, 0, data.Length);
     }
     catch (ObjectDisposedException)
     {
         throw new PortClosedException();
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
         OnWriteExceptionOccured(eventArgs);
     }
 }
コード例 #8
0
 public override void Write(byte[] data)
 {
     try
     {
         if (!tcpClient.Connected)
         {
             throw new PortClosedException();
         }
         NetworkStream outputStream = tcpClient.GetStream();
         writeTask = outputStream.WriteAsync(data, 0, data.Length);
         outputStream.Flush();
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
         OnWriteExceptionOccured(eventArgs);
     }
 }
コード例 #9
0
        private async Task MonitorPort()
        {
            byte[] buffer    = new byte[connectionRequestMessageLength];
            byte[] data      = new byte[connectionRequestMessageLength];
            int    bytesRead = 0;

            while (bytesRead != connectionRequestMessageLength)
            {
                try
                {
                    if (!tcpClient.Connected)
                    {
                        throw new PortClosedException();
                    }
                    NetworkStream inputStream = tcpClient.GetStream();
                    int           dataLength  = 0;
                    dataLength = await inputStream.ReadAsync(buffer, 0, connectionRequestMessageLength);

                    if (dataLength == 0)
                    {
                        Close();
                        break;
                    }
                    else if (dataLength <= connectionRequestMessageLength - bytesRead)
                    {
                        Array.Copy(buffer, 0, data, bytesRead, dataLength);
                        bytesRead += dataLength;
                    }
                    else
                    {
                        Array.Copy(buffer, 0, data, bytesRead, connectionRequestMessageLength - bytesRead);
                        bytesRead = connectionRequestMessageLength;
                    }
                }
                catch (Exception e)
                {
                    ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
                    OnReadExceptionOccured(exceptionOccuredEventArgs);
                }
            }
            ConnectionRequestedEventArgs eventArgs = new ConnectionRequestedEventArgs(data);

            OnConnectionRequest(eventArgs);
        }
コード例 #10
0
        private async Task WaitForPortConnections()
        {
            try
            {
                while (true)
                {
                    TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();

                    TCPPort tcpPort = new TCPPort(tcpClient);
                    PortRequestedEventArgs eventArgs = new PortRequestedEventArgs(tcpPort);
                    OnPortRequested(eventArgs);
                }
            }
            catch (Exception e)
            {
                ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
                OnWaitForPortConnectionsExceptionOccured(eventArgs);
            }
        }
コード例 #11
0
 private void WaitingForConnectionCallback(GattCharacteristic gattCharacteristic, GattValueChangedEventArgs eventArgs)
 {
     try
     {
         DataReader dataReader = DataReader.FromBuffer(eventArgs.CharacteristicValue);
         byte[]     data       = new byte[dataReader.UnconsumedBufferLength];
         dataReader.ReadBytes(data);
         if (data.Length == connectionRequestMessageLength)
         {
             ConnectionRequestedEventArgs conReqEventArgs = new ConnectionRequestedEventArgs(data);
             OnConnectionRequest(conReqEventArgs);
             characteristic.ValueChanged -= WaitingForConnectionCallback;
         }
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
         OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
     }
 }
コード例 #12
0
        private async Task MonitorPort()
        {
            byte[] buffer    = new byte[port.ReadBufferSize];
            byte[] data      = new byte[connectionRequestMessageLength];
            int    bytesRead = 0;

            while (bytesRead != connectionRequestMessageLength)
            {
                try
                {
                    if (!port.IsOpen)
                    {
                        throw new PortClosedException();
                    }
                    int dataLength = await port.BaseStream.ReadAsync(buffer, 0, connectionRequestMessageLength);

                    if (dataLength <= connectionRequestMessageLength - bytesRead)
                    {
                        Array.Copy(buffer, 0, data, bytesRead, dataLength);
                        bytesRead += dataLength;
                    }
                    else
                    {
                        Array.Copy(buffer, 0, data, bytesRead, connectionRequestMessageLength - bytesRead);
                        bytesRead = connectionRequestMessageLength;
                    }
                }
                catch (Exception e)
                {
                    ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
                    OnWaitForConnectionRequestExceptionOccured(eventArgs);
                    break;
                }
            }

            if (bytesRead == connectionRequestMessageLength)
            {
                ConnectionRequestedEventArgs eventArgs = new ConnectionRequestedEventArgs(data);
                OnConnectionRequest(eventArgs);
            }
        }
コード例 #13
0
 static void WaitForPortConnectionsTaskExceptionEventHandler(object sender, ExceptionOccuredEventArgs eventArgs)
 {
     Util.DisplayExceptionInfo(eventArgs.exception, "in WaitForPortConnectionsTask of " + ((Peripheral)sender).ID);
 }
コード例 #14
0
 static void PortExceptionOccuredEventHandler(object sender, ExceptionOccuredEventArgs eventArgs)
 {
     Util.DisplayExceptionInfo(eventArgs.exception, "in " + ((Port)sender).ID);
 }
コード例 #15
0
        protected void OnWaitForPortConnectionsExceptionOccured(ExceptionOccuredEventArgs eventArgs)
        {
            EventHandler <ExceptionOccuredEventArgs> handler = WaitForPortConnectionsTaskExceptionOccured;

            handler?.Invoke(this, eventArgs);
        }
コード例 #16
0
        protected void OnWriteExceptionOccured(ExceptionOccuredEventArgs eventArgs)
        {
            EventHandler <ExceptionOccuredEventArgs> handler = WriteTaskExceptionOccured;

            handler?.Invoke(this, eventArgs);
        }