示例#1
0
        /// <summary>
        ///     Connects to the Parrot RF Service, if that succeeds
        ///     it starts waiting for data to handle.
        ///
        ///     Throws an exception when the connection failed.
        /// </summary>
        public async Task ConnectAsync()
        {
            // Find the Parrot RF Service.
            var parrotService = Device.InstalledServices.FirstOrDefault(x => ParrotRfServiceGuids.Contains(x));

            if (parrotService == null)
            {
                throw new Exception("Couldn't find the parrot rf service.");
            }

            // Connect to the service.
            BluetoothClient.Connect(Device.DeviceAddress, parrotService);

            if (!BluetoothClient.Connected)
            {
                throw new Exception("Couldn't connect to the parrot rf service.");
            }

            // Send initial packet and stop other packets from being sent.
            var initialPacket = new byte[] { 0x00, 0x03, 0x00 };

            await BluetoothClient.GetStream().WriteAsync(initialPacket, 0, initialPacket.Length);

            await BluetoothClient.GetStream().FlushAsync();

            await BluetoothClient.GetStream().ReadAsync(new byte[3], 0, 3);

            // Dispatch event
            ConnectedEvent.AsyncSafeInvoke(this, EventArgs.Empty);
        }