locked
Problem in storing new contacts received via proximity in windows phone application

    Question

  • i am developing an application for windows phone with the help of which any one can send contacts via Bluetooth across windows phone. i am succeeded in doing so but the problem is that i can receive contacts in run-time but how to store these recently received contacts to the contacts store. can any one plz help me...    the code i tried is as ..

     private void send_Click(object sender, RoutedEventArgs e)
            {

                if (proximitySocket != null)
                {
                    SendContacts();
                }
                else
                {
                    WriteMessageText("You must enter proximity to send contacts.\n");
                }
            }

            Windows.Networking.Sockets.StreamSocket proximitySocket;
            Windows.Storage.Streams.DataWriter dataWriter;
            private void SendMessage(Windows.Networking.Sockets.StreamSocket streamSocket)
            {
                if (!_started)
                {
                    CloseSocket();
                    return;
                }

                proximitySocket = streamSocket;

                dataWriter = new Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream);

                Windows.Storage.Streams.DataReader dataReader = new Windows.Storage.Streams.DataReader(proximitySocket.InputStream);
                StartReader(proximitySocket, dataReader);
            }

            private async void SendContacts()
            {
                List<MyContact> list = new List<MyContact>();
                foreach (var item in listbox.SelectedItems)
                {
                    Contact c = item as Contact;
                    list.Add(new MyContact
                    {
                        Name = c.FirstName + " " + c.LastName,
                        Phone = c.Phones[0].Number
                    });
                }


                string msg = JsonConvert.SerializeObject(list);

                if (msg.Length > 0)
                {
                    var msgLength = dataWriter.MeasureString(msg);
                    dataWriter.WriteInt32(msg.Length);
                    dataWriter.WriteString(msg);
                    try
                    {
                        await dataWriter.StoreAsync();
                        WriteMessageText("Contacts Sent: " + msg + "\n");
                    }
                    catch (Exception e)
                    {
                        WriteMessageText("Send error: " + e.Message + "\n");
                        CloseSocket();
                    }
                }
            }
            private async void StartReader(Windows.Networking.Sockets.StreamSocket proximitySocket, Windows.Storage.Streams.DataReader dataReader)
            {
                try
                {
                    uint bytesRead = await dataReader.LoadAsync(sizeof(uint));
                    if (bytesRead > 0)
                    {
                        uint strLength = (uint)dataReader.ReadUInt32();
                        bytesRead = await dataReader.LoadAsync(strLength);
                        if (bytesRead > 0)
                        {
                            String message = dataReader.ReadString(strLength);
                            WriteMessageText("Received contacts: " + message + "\n");
                            StartReader(proximitySocket, dataReader);
                        }
                        else
                        {
                            WriteMessageText("The peer app closed the socket\n");
                            dataReader.Dispose();
                            CloseSocket();
                        }
                    }
                    else
                    {
                        WriteMessageText("The peer app closed the socket\n");
                        dataReader.Dispose();
                        CloseSocket();
                    }
                }
                catch
                {
                    WriteMessageText("The peer app closed the socket\n");
                    dataReader.Dispose();
                    CloseSocket();
                }
            }

            private void CloseSocket()
            {
                if (proximitySocket != null)
                {
                    proximitySocket.Dispose();
                    proximitySocket = null;
                }

                if (dataWriter != null)
                {
                    dataWriter.Dispose();
                    dataWriter = null;
                }
            }

    • Edited by Afsar Murad Tuesday, February 24, 2015 11:57 AM
    Tuesday, February 24, 2015 11:38 AM

Answers

  • Hi Afsar,

    From your description the main issue is saving contacts to the phone.

    I believe you would interested with this class: ContactStore class, by CreateOrOpenAsync we should be able to save a contact. Here are some code I've tested:

                var mycontactStore = await Windows.Phone.PersonalInformation.ContactStore.CreateOrOpenAsync(
                                   ContactStoreSystemAccessMode.ReadWrite,
                                   ContactStoreApplicationAccessMode.ReadOnly);
                var mycontact = new StoredContact(mycontactStore);
                var contactDetails = await mycontact.GetPropertiesAsync();
    
                if (!string.IsNullOrEmpty(KnownContactProperties.DisplayName))
                {
                    contactDetails.Add(KnownContactProperties.GivenName, "aaaaa");
                }
    
                await mycontact.SaveAsync();

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Afsar Murad Wednesday, February 25, 2015 8:09 AM
    Wednesday, February 25, 2015 6:08 AM
    Moderator

All replies

  • Hi Afsar,

    From your description the main issue is saving contacts to the phone.

    I believe you would interested with this class: ContactStore class, by CreateOrOpenAsync we should be able to save a contact. Here are some code I've tested:

                var mycontactStore = await Windows.Phone.PersonalInformation.ContactStore.CreateOrOpenAsync(
                                   ContactStoreSystemAccessMode.ReadWrite,
                                   ContactStoreApplicationAccessMode.ReadOnly);
                var mycontact = new StoredContact(mycontactStore);
                var contactDetails = await mycontact.GetPropertiesAsync();
    
                if (!string.IsNullOrEmpty(KnownContactProperties.DisplayName))
                {
                    contactDetails.Add(KnownContactProperties.GivenName, "aaaaa");
                }
    
                await mycontact.SaveAsync();

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Afsar Murad Wednesday, February 25, 2015 8:09 AM
    Wednesday, February 25, 2015 6:08 AM
    Moderator
  • thanks a lot it really works....

    Wednesday, February 25, 2015 8:10 AM