private async void CompleteFriendshipNFC(Windows.Networking.Proximity.ProximityDevice sender, Windows.Networking.Proximity.ProximityMessage message) { // Stop subscribing to the message. This means the other user finished sending a friend request AppServices.myProximityDevice.StopSubscribingForMessage(this.subscribedMessageID); this.subscribedMessageID = -1; // Stop publishing friend request sent message as the user has already processed it AppServices.myProximityDevice.StopPublishingMessage(this.publishedMessageID); this.publishedMessageID = -1; // Other user accepted our friend request successfully so we add them to our list and save the encryption key if (message.DataAsString.Equals("true")) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { AppServices.friendsData.friendsList.Add(new Tap_Chat.ViewModel.FriendsListEntry() { encryptedUsername = this.nfcFriendName, isOnline = true, encryptionKey = AppServices.encryptionAlgorithim.CreateSymmetricKey(keyBuff) }); this.EndAsyncOperationsSuccess(); this.PrintMessage("Added Friend Successfully!"); }); } // Other user failed to accept our friend request so we print out a message and end else { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Even though the add failed we call success to remove the progress indicator and enable input this.EndAsyncOperationsSuccess(); this.PrintMessage("Other user had an error. Add friend failed"); }); } }
private async void AcceptFriendRequestNFC(Windows.Networking.Proximity.ProximityDevice sender, Windows.Networking.Proximity.ProximityMessage message) { // Stop subscribing to the message. This means the other user finished sending a friend request AppServices.myProximityDevice.StopSubscribingForMessage(this.subscribedMessageID); this.subscribedMessageID = -1; // Other user successfully sent friend request (message is not "false"), so we receive the generated encryption key if (message.DataAsString.Equals("false") == false) { // Accept the friend request await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.StartAsyncOperations("Accepting friend request..."); }); AppServices.operationComplete = false; AppServices.buddyService.AcceptFriendRequest(AppServices.localUsernameEncrypted, this.nfcFriendName, new AcceptRequestCallback()); //await Task.Run(() => //{ while (AppServices.operationComplete == false) { ; } //}); // Unable to accept friend request. Error out and tell the other user if (AppServices.operationSuccessful == false) { this.publishedMessageID = AppServices.myProximityDevice.PublishMessage(this.friendRequestCompleteNFCMessageType, "false"); await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.EndAsyncOperationsError(); }); return; } // Accepted friend request successfully. Tell the other user and add them to our friends list this.publishedMessageID = AppServices.myProximityDevice.PublishMessage(this.friendRequestCompleteNFCMessageType, "true"); this.newEncryptionKey = message.DataAsString; IBuffer keyBuff = CryptographicBuffer.DecodeFromHexString(this.newEncryptionKey); await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { AppServices.friendsData.friendsList.Add(new Tap_Chat.ViewModel.FriendsListEntry() { encryptedUsername = this.nfcFriendName, isOnline = true, encryptionKey = AppServices.encryptionAlgorithim.CreateSymmetricKey(keyBuff) }); this.EndAsyncOperationsSuccess(); this.PrintMessage("Added Friend Successfully!"); }); } // Other user had an issue sending a request so we print a message and end else { // We ended in success but the other user had an error so we hide the progress indicator and print a different message await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.EndAsyncOperationsSuccess(); }); this.PrintMessageAsync("Other user had an error. Unable to add friend."); return; } }
private async void ReceiveUsernameNFC(Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message) { // Once a message is received stop subscribing for it AppServices.myProximityDevice.StopSubscribingForMessage(this.subscribedMessageID); this.subscribedMessageID = -1; // Check to see who's username is greater. The one with the greater username sends the request and generates a key int compareResult = string.Compare(AppServices.localUsernameEncrypted, message.DataAsString); this.nfcFriendName = message.DataAsString; // I must send friend request using the App42 API if (compareResult > 0) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.StartAsyncOperations("Establishing friendship..."); }); AppServices.operationComplete = false; AppServices.buddyService.SendFriendRequest(AppServices.localUsernameEncrypted, message.DataAsString, "Friend Request", new FriendRequestCallback()); //await Task.Run(() => //{ while (AppServices.operationComplete == false) { ; } //}); // Once the friend request is sent completely we stop publishing our name AppServices.myProximityDevice.StopPublishingMessage(this.publishedMessageID); this.publishedMessageID = -1; // If sending the request failed we notify the other user if (AppServices.operationSuccessful == false) { this.publishedMessageID = AppServices.myProximityDevice.PublishMessage(this.friendRequestSentNFCMessageType, "false"); await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.EndAsyncOperationsError(); }); return; } // The send was successful so we tell the other user our encryption key and now wait for them to accept and notify us keyBuff = CryptographicBuffer.GenerateRandom(256); this.newEncryptionKey = CryptographicBuffer.EncodeToHexString(keyBuff); this.publishedMessageID = AppServices.myProximityDevice.PublishMessage(this.friendRequestSentNFCMessageType, this.newEncryptionKey); this.subscribedMessageID = AppServices.myProximityDevice.SubscribeForMessage(this.friendRequestCompleteNFCMessageType, CompleteFriendshipNFC); } // I must wait for a friend request from the other user using the App42 API else { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.StartAsyncOperations("Waiting for friendship data..."); }); this.subscribedMessageID = AppServices.myProximityDevice.SubscribeForMessage(this.friendRequestSentNFCMessageType, AcceptFriendRequestNFC); AppServices.myProximityDevice.StopPublishingMessage(this.publishedMessageID); this.publishedMessageID = -1; } }
private void messageReceived( Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message) { MessageBlock.Text += "Message received: " + message.DataAsString + "\n"; }
private void messageReceived(Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message) { WriteMessageText("Message received: " + message.DataAsString + "\n"); }
private void ReceiveEncryptionKey(Windows.Networking.Proximity.ProximityDevice sender, Windows.Networking.Proximity.ProximityMessage message) { string keyString = message.DataAsString; this.keyBuff = CryptographicBuffer.DecodeFromHexString(keyString); lastSelected.encryptionKey = AppServices.encryptionAlgorithim.CreateSymmetricKey(this.keyBuff); this.PrintMessageAsync("Secure connection established! You may now separate your devices."); }