Пример #1
0
            public async Task SendAsync(byte[] data)
            {
                using (await synchronization.LockAsync().ConfigureAwait(false)) {
                    Console.WriteLine("Sending to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));

                    try {
                        var stream = bluetoothClient.GetStream();
                        await stream.WriteAsync(BitConverter.GetBytes(data.Length), 0, 4).ConfigureAwait(false);

                        await stream.WriteAsync(data, 0, data.Length).ConfigureAwait(false);

                        Console.WriteLine("Sent to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));
                    } catch {
                        Console.WriteLine("Failed to send to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));
                        Teardown();
                        throw new NotConnectedException();
                    }
                }
            }
Пример #2
0
 private void Teardown()
 {
     Console.WriteLine("Teardown connection to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));
     bluetoothClient?.Dispose();
     bluetoothClient = null;
     disconnectedChannel.SetIsClosed(true);
 }
Пример #3
0
            public async Task <bool> TryHandshakeAsync(double minTimeoutSeconds)
            {
                try {
                    using (await synchronization.LockAsync().ConfigureAwait(false)) {
                        Console.WriteLine("Attempting to connect to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));

                        bluetoothClient = new BluetoothClient();
                        bluetoothClient.Authenticate = false;
                        bluetoothClient.Encrypt      = false;

                        await bluetoothClient.ConnectAsync(address, CAMPFIRE_NET_SERVICE_CLASS).ConfigureAwait(false);

                        disconnectedChannel.SetIsClosed(false);

                        Console.WriteLine("Connected. Their Adapter ID is " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));

                        ChannelsExtensions.Go(async() => {
                            Console.WriteLine("Entered BT Reader Task");
                            var networkStream = bluetoothClient.GetStream();
                            try {
                                while (!disconnectedChannel.IsClosed)
                                {
                                    Console.WriteLine("Reading BT Frame");
                                    var dataLengthBuffer = await ReadBytesAsync(networkStream, 4).ConfigureAwait(false);
                                    var dataLength       = BitConverter.ToInt32(dataLengthBuffer, 0);
                                    Console.WriteLine("Got BT Frame Length: " + dataLength);
                                    var data = await ReadBytesAsync(networkStream, dataLength).ConfigureAwait(false);
                                    await inboundChannel.WriteAsync(data).ConfigureAwait(false);
                                }
                            } catch (Exception e) {
                                Console.WriteLine(e);
                                Teardown();
                            }
                        }).Forget();
                        return(true);
                    }
                } catch (Exception e) {
                    Console.WriteLine("Failed to connect to ID " + AdapterId + " AKA " + string.Join(" ", AdapterId.ToByteArray()));
                    Console.WriteLine(e.GetType().FullName);
                    return(false);
                }
            }