Ejemplo n.º 1
0
        public void PingAdversary(ProximityDevice device, NotifyNfcReady notify)
        {
            if (subscribeId != -1)
            {
                proximityDevice.StopSubscribingForMessage(subscribeId);
                subscribeId = -1;
            }

            if (publishId != -1)
            {
                proximityDevice.StopPublishingMessage(publishId);
                publishId = -1;
            }

            if (state == NfcManager.ProtoState.Busy)
            {
                return;
            }

            state       = NfcManager.ProtoState.NotReady;
            notifyReady = notify;
            initialMessage.devicetime = random.NextDouble();
            MemoryStream           stream     = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(initialMessage.GetType());

            serializer.WriteObject(stream, initialMessage);
            stream.Position = 0;
            var dataWriter = new DataWriter();

            dataWriter.WriteBytes(stream.GetBuffer());
            proximityDevice = device;
            publishId       = proximityDevice.PublishBinaryMessage("Windows.CarTrumps", dataWriter.DetachBuffer());
            subscribeId     = proximityDevice.SubscribeForMessage("Windows.CarTrumps", OnMessageReceived);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Stops all, publishing and subscribing.
        /// </summary>
        public void StopAll()
        {
            if (_publishedMsgId != -1)
            {
                _proximityDevice.StopPublishingMessage(_publishedMsgId);
                _publishedMsgId = -1;
            }

            if (_subscribedMsgId != -1)
            {
                _proximityDevice.StopSubscribingForMessage(_subscribedMsgId);
                _subscribedMsgId = -1;
            }
        }
Ejemplo n.º 3
0
        private void btnWinMime_Click(object sender, RoutedEventArgs e)
        {
            ProximityDevice device = ProximityDevice.GetDefault();

            if (device != null)
            {
                if (_subscriptionId > -1)
                {
                    device.StopSubscribingForMessage(_subscriptionId);
                }
                _subscriptionId = device.SubscribeForMessage(
                    "WindowsMime",
                    (proximityDevice, message) =>
                {
                    var buffer   = message.Data.ToArray();
                    int mimesize = 0;
                    //suchen nach '\0'
                    for (mimesize = 0; mimesize < 256 && buffer[mimesize] != 0; ++mimesize)
                    {
                    }

                    //MimeType bestimmen
                    var mimeType = Encoding.UTF8.GetString(buffer, 0, mimesize).Trim();
                    if (mimeType == "text/plain")
                    {
                        //convert data to string. This traitement depend on mimetype value.
                        tbMime.Text = Encoding.UTF8.GetString(buffer, 256, buffer.Length - 256);
                    }
                });
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Unsubsctibe from reading certain types of NFC messages
 /// </summary>
 /// <param name="type"></param>
 public void UnsubscribeForMessage(string type)
 {
     if ((pDevice != null) && (subscriptionIds.ContainsKey(type)))
     {
         pDevice.StopSubscribingForMessage(subscriptionIds[type]);
     }
 }
Ejemplo n.º 5
0
 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     if (_subscriptionId > 0)
     {
         _proximity.StopSubscribingForMessage(_subscriptionId);
     }
 }
Ejemplo n.º 6
0
    /**
     * Start/stop listening on the proximity device for a tag.
     *
     * @param input {object}
     * @param input.stop {bool} True to stop listening.
     * @param input.gotTag {Function(Buffer)} The callback for when a tag is received.
     * @return true on success. false if there is no such device.
     */
    public async Task <object> Invoke(dynamic input)
    {
        ProximityDevice dev     = ProximityDevice.GetDefault();
        bool            success = false;

        if (dev != null)
        {
            if (input.stop)
            {
                // Stop listening.
                if (subscriptionId >= 0)
                {
                    dev.StopSubscribingForMessage(subscriptionId);
                    subscriptionId = -1;
                }

                gotTagCallback = null;
            }
            else if (subscriptionId < 0)
            {
                // Start listening.
                gotTagCallback = input.gotTag;
                subscriptionId = dev.SubscribeForMessage("NDEF", new MessageReceivedHandler(GotTag));
            }
            success = true;
        }

        return(success);
    }
Ejemplo n.º 7
0
        private void OnDeviceDeparted(ProximityDevice sender)
        {
            var stopwatch = (Stopwatch)null;

            if (Debug)
            {
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }

            if (subscriptionIdNdef != 0)
            {
                sender.StopSubscribingForMessage(subscriptionIdNdef);
                subscriptionIdNdef = 0;
            }

            var args = new DeviceStatusChangedEventArgs()
            {
                DeviceStatus = StatusEnum.DeviceDeparted
            };

            if (Debug)
            {
                stopwatch.Stop();
                args.ExecutionTime = stopwatch.Elapsed;
                args.MethodName    = "TagReader.OnDeviceDeparted";
            }

            OnDeviceStatusChanged(this, args);
        }
Ejemplo n.º 8
0
 public void UnsubscribeForNfcMessage()
 {
     if (proximityDevice != null)
     {
         proximityDevice.StopSubscribingForMessage(messageId);
         messageId = -1;
     }
 }
Ejemplo n.º 9
0
 private void StopProximityDevice()
 {
     if (_ProximityDevice != null)
     {
         _ProximityDevice.StopSubscribingForMessage(ProximitySubscribeId);
         _ProximityDevice = null;
     }
 }
Ejemplo n.º 10
0
 private void Unsubscribe()
 {
     if (subscriptionReference != 0)
     {
         proximityDevice.StopSubscribingForMessage(subscriptionReference);
         subscriptionReference = 0;
     }
 }
 private void StopSubscribingForMessage()
 {
     if (subscribeId != -1)
     {
         proximityDevice.StopSubscribingForMessage(subscribeId);
         subscribeId = -1;
     }
 }
Ejemplo n.º 12
0
 private void UnsubscribeNFC(long subscritpionId)
 {
     if (nfcDevice == null)
     {
         return;
     }
     nfcDevice.StopSubscribingForMessage(subscritpionId);
     subscritpionId = 0;
 }
Ejemplo n.º 13
0
        private async void WritableTagDetectedHandler(ProximityDevice device, ProximityMessage message)
        {
            device.StopSubscribingForMessage(detectWriteableTagId);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteTag.IsEnabled = true;
            });
        }
Ejemplo n.º 14
0
        private void btnUnsubscribe_Click(object sender, RoutedEventArgs e)
        {
            ProximityDevice device = ProximityDevice.GetDefault();

            // Prüfen ob NFC auf diesem Gerät verfügbar ist
            if (device != null && _subscriptionId > -1)
            {
                device.StopSubscribingForMessage(_subscriptionId);
            }
        }
Ejemplo n.º 15
0
        private async void MessageReceivedHandler(ProximityDevice device, ProximityMessage message)
        {
            device.StopSubscribingForMessage(subscribedMessageId);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                // Data:  byte array stored in the NFC tag
                var dialog = new MessageDialog("NFC Data: " + message.DataAsString);
                await dialog.ShowAsync();
            });
        }
Ejemplo n.º 16
0
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
#if WP8
            if (proximityDevice != null)
            {
                proximityDevice.StopSubscribingForMessage(nfcSubsId);
            }
#endif
            LuaEngine.Instance.OnGuiEvent(this, LuaEngine.GuiEvents.GUI_EVENT_PAUSE, luaContext);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Stops tags detection
        /// </summary>
        public void StopListening()
        {
            _defaultDevice.DeviceArrived  -= OnDeviceArrived;
            _defaultDevice.DeviceDeparted -= OnDeviceDeparted;

            if (_ndefSubscriptionId != -1)
            {
                _defaultDevice.StopSubscribingForMessage(_ndefSubscriptionId);
                _ndefSubscriptionId = -1;
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Stops tags detection
        /// </summary>
        public void StopListening()
        {
            _defaultDevice.DeviceArrived  -= OnDeviceArrived;
            _defaultDevice.DeviceDeparted -= OnDeviceDeparted;

            if (_ndefSubscriptionId != -1)
            {
                _defaultDevice.StopSubscribingForMessage(_ndefSubscriptionId);
                _ndefSubscriptionId = -1;
            }

            OnTagListeningStatusChanged?.Invoke(false);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Gets called when a message is received. Updates the UI by adding
        /// the details into the log and scrolling the log if necessary.
        /// Note that subscription for messages needs to be redone.
        /// </summary>
        /// <param name="sender">ProximityDevice instance</param>
        /// <param name="message">A message to be handled.</param>
        private void MessageReceived(ProximityDevice sender, ProximityMessage message)
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (device != null)
                {
                    device.StopSubscribingForMessage(subscriptionId);
                }

                logText = logText + ParseNDEF(message);
                ScrollViewer.UpdateLayout();
                ScrollViewer.ScrollToVerticalOffset(0);
                SubscribeForMessage();
            });
        }
Ejemplo n.º 20
0
 private void StopSubscription(bool writeToStatusOutput)
 {
     if (_subscriptionIdNdef != 0 && _device != null)
     {
         // Ask the proximity device to stop subscribing for NDEF messages
         _device.StopSubscribingForMessage(_subscriptionIdNdef);
         _subscriptionIdNdef = 0;
         // Update enabled / disabled state of buttons in the User Interface
         UpdateUiForNfcStatus();
         // Update status text for UI - only if activated
         if (writeToStatusOutput)
         {
             SetStatusOutput(AppResources.StatusSubscriptionStopped);
         }
     }
 }
Ejemplo n.º 21
0
        private void btnSubscribeWritable_Click(object sender, RoutedEventArgs e)
        {
            ProximityDevice device = ProximityDevice.GetDefault();

            // Prüfen ob NFC auf diesem Gerät verfügbar ist
            if (device != null)
            {
                if (_subscriptionId > -1)
                {
                    device.StopSubscribingForMessage(_subscriptionId);
                }
                _subscriptionId = device.SubscribeForMessage("WriteableTag", (proximityDevice, message) => {
                    var size = System.BitConverter.ToInt32(message.Data.ToArray(), 0);
                    Deployment.Current.Dispatcher.BeginInvoke(() => {
                        MessageBox.Show(string.Format("Beschreibbarer Tag, Größe:{0}Bytes", size));
                    });
                });
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// When the stop subscribing button is pressed, stop listening for other devices.
        /// </summary>
        /// <param name="sender">The CoronaRuntimeEnvironment that dispatched the event.</param>
        /// <param name="e">Provides the Lua event table's fields/properties.</param>
        private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnStopSubscribeAndPublish(
            CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender,
            CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)
        {
            if (_subscribedMessageID != -1)
            {
                _proximityDevice.StopSubscribingForMessage(_subscribedMessageID);
                _subscribedMessageID = -1;
            }

            if (_publishedMessageID != -1)
            {
                _proximityDevice.StopPublishingMessage(_publishedMessageID);
                _publishedMessageID = -1;
            }

            // Return a success message to Lua.
            return(CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Stopped subscribing and publishing"));
        }
Ejemplo n.º 23
0
        private void MessageReceived(ProximityDevice sender, ProximityMessage message)
        {
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                if (device != null)
                {
                    device.StopSubscribingForMessage(_subscriptionId);
                }

                var ndefRead = ParseNdef(message);
                _logText     = _logText + ndefRead;
                ScrollViewer.UpdateLayout();
                ScrollViewer.ScrollToVerticalOffset(0);
                SubscribeForMessage();

                if (!Frame.Navigate(typeof(NfcTagDetailsPage), ndefRead))
                {
                    throw new Exception("Ooops.");
                }
            });
        }
Ejemplo n.º 24
0
        private void btnSubscribe_Click(object sender, RoutedEventArgs e)
        {
            ProximityDevice device = ProximityDevice.GetDefault();

            // Prüfen ob NFC auf diesem Gerät verfügbar ist
            if (device != null)
            {
                if (_subscriptionId > -1)
                {
                    device.StopSubscribingForMessage(_subscriptionId);
                }
                _subscriptionId = device.SubscribeForMessage(
                    "Windows.WindowsDeveloper.MessageType",
                    (proximityDevice, message) => Dispatcher.BeginInvoke(() =>
                                                                         MessageBox.Show(
                                                                             "Nachricht empfangen " +
                                                                             message.DataAsString +
                                                                             " von " +
                                                                             proximityDevice.DeviceId)
                                                                         ));
            }
        }
Ejemplo n.º 25
0
 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     base.OnNavigatedFrom(e);
     proxDevice.StopSubscribingForMessage(subscriptionID);
 }