Xamarin Essentials Android Contacts: is there a way to determine which phone number? EG: Home, Work, Mobile? - xamarin

Xamarin Essentials Contacts: is there a way to determine which phone number is which? EG: Home, Work, Mobile?
This is just for Android only.
Currently it just has the phone number(s) only, but I have a need to only display Mobile phone numbers (for SMS) and I can't see a way to determine if a number is tagged as 'Mobile'. Like when you create a contact on your andriod phone, you can set the phone number to be Home, Mobile, Work, etc.
Or anyone know of another library that does this? thanks.

Let take the example from docs. You could use the property ContactType of type enum ContactType, but it only defines Unknown, Personal and Work values:
try
{
var contact = await Contacts.PickContactAsync();
if(contact == null)
return;
var phones = contact.Phones; // List of phone numbers
foreach (var phone in phones)
{
if (phone.ContactType == ContactType.Personal || phone.ContactType == ContactType.Work) //just an example
}
}
catch (Exception ex)
{
// Handle exception here.
}

Related

Unique ID for mobile device FMX

What is a good way to create a unique ID for mobile devices (iOS and Android) in a C++Builder FMX app?
In my case, I just want to let my app users vote, but only once per device (even if they delete the app and reinstall it). They stay anonymous, but just can't vote more than once.
I know Apple came out with DeviceCheck for Swift, but I don't know how to use it in C++.
iOS 11: The DeviceCheck API
DeviceCheck API - Unique Identifier for the iOS Devices
Update: This solution targets Android devices. I have no experience with iOS devices.
Original answer: It may be way too late but here's my solution using _di_JTelephonyManager
_di_JObject obj;
_di_JTelephonyManager tm;
UnicodeString id;
try {
obj = SharedActivityContext()->getSystemService(TJContext::JavaClass>TELEPHONY_SERVICE);
if (obj) {
tm = TJTelephonyManager::Wrap(static_cast<_di_ILocalObject>(obj)->GetObjectID());
if (tm) {
//only if SIM Card is in device:
//id = JStringToString(tm->getSubscriberId());
//will get IMEI or MEID number
id = JStringToString(TJSettings_Secure::JavaClass->getString
(SharedActivity()->getContentResolver(),
TJSettings_Secure::JavaClass->ANDROID_ID));
}
}
}
catch (Exception &e) {
//catch exceptions
}
Hope it is helpful :)

Xamarin IOS bluetooth scan not showing all device list

I am not getting all devices when I am trying to scan Bluetooth devices with my application. It does not show android and windows device list. I have attached screenshots to understand my problem.
Here is my code.
_centralManager = new CBCentralManager(DispatchQueue.CurrentQueue);
_centralManager.DiscoveredPeripheral += _centralManager_DiscoveredPeripheral;
_centralManager.UpdatedState += (object sender, EventArgs e) =>
{
var manager = sender as CBCentralManager;
if (manager.State == CBCentralManagerState.PoweredOn)
_centralManager.ScanForPeripherals(new CBUUID[0]);
};
Scan event:
public void _centralManager_DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
{
var device = e.Peripheral;
var rssi = e.RSSI;
var ads = e.AdvertisementData;
}
Note: In my app, I was show device which name is not equal to null or blank.
I had implemented GATTA server on an Android device and it worked.
First, run the app on an Android device, start the server and done.
https://github.com/androidthings/sample-bluetooth-le-gattserver/blob/master/java/app/src/main/java/com/example/androidthings/gattserver/GattServerActivity.java
In my case, I wanted to do this for android things. So I implemented the code in android things.

Get the list of Contacts on Windows 10 Phone

I am needing to know how to read the contact list on a Windows 10 Phone. I don't want to use a contact picker; I just need to be able to iterate through all contacts to access their name and phone number, and store it in a List. (Similar to how WhatsApp is able to read your contact list and display it in their app)
I've taken some code from another answer here.
With this code you should be able to get the contacts out.
public async Task IterateThroughContactsForContactListId()
{
ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
var contacts = await allAccessStore.FindContactsAsync();
foreach (var contact in contacts)
{
//process aggregated contacts
if (contact.IsAggregate)
{
//here contact.ContactListId is "" (null....)
//in this case if you need the the ContactListId then you need to iterate through the raw contacts
var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
foreach (var rawContact in rawContacts)
{
//Here you should have ContactListId
Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
}
}
else //not aggregated contacts should work
{
Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
}
}
}
He also notes:
And very important: In the appxmanifest you have to add the contacts
capability. Right click to it in the solution explorer and "View Code"
and then under Capabilities put
<uap:Capability Name="contacts" />
There is no UI for this. See this.

Adding Windows Phone 8 Tile functionality to Windows Phone OS 7.1 app

I'm trying to support the new Windows Phone tile functionality in my existing Windows Phone OS 7.1 application, using the documentation from MSDN. However, I can't seem to create an IconicTile through reflection as it keeps giving me NullReferenceExceptions and AmbiguousMatchExceptions. Here is the code I am using:
public static void CreateIconicTile(Uri tileId, string title, int count, string wideContent1, string wideContent2, string wideContent3, Uri smallIconImage, Uri iconImage, Color backgroundColor)
{
// Get the new IconicTileData type.
Type iconicTileDataType = Type.GetType("Microsoft.Phone.Shell.IconicTileData, Microsoft.Phone");
// Get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
// Get the constructor for the new IconicTileData class and assign it to our variable to hold the Tile properties.
StandardTileData CreateTileData = new StandardTileData();
// Set the properties.
SetProperty(CreateTileData, "Count", count);
SetProperty(CreateTileData, "WideContent1", wideContent1);
SetProperty(CreateTileData, "WideContent2", wideContent2);
SetProperty(CreateTileData, "WideContent3", wideContent3);
SetProperty(CreateTileData, "SmallIconImage", smallIconImage);
SetProperty(CreateTileData, "IconImage", iconImage);
SetProperty(CreateTileData, "BackgroundColor", backgroundColor);
// Invoke the new version of ShellTile.Create.
shellTileType.GetMethod("Create").Invoke(null, new Object[] { tileId, CreateTileData });
}
I also tried creating the tile using the Windows Phone OS 7.1 way (ShellTile.Create(...)) and then calling the UpdateIconicTile method via reflection as described in the MSDN documentation. But that didn't work either.
Any help would be greatly appreciated. Thanks!
EDIT: Just to clarify, I am checking the platform version to ensure this code only runs on Windows Phone 8 devices and I have added the necessary code to my manifest.
RESOLVED: Thanks to the answer given by Martin Suchan below, I was able to solve this problem. The problem was with my call to Invoke(...) missing some properties. Here is the new line I am using to actually create the tile:
shellTileType.GetMethod("Create", new Type[] { typeof(Uri), typeof(ShellTileData), typeof(bool) }).Invoke(null, new Object[] { tileId, CreateTileData, true });
Have you tried the library Mangopollo, that contains working wrapper for creation new tiles in WP7.1 apps when running on WP8?
http://mangopollo.codeplex.com/
You have to make sure that reflection is enabled across the code.
Iconic Tiles are only available for windows phone 8 therefore you can only put the code into a windows phone 7.1 project if you check the version
private static Version TargetedVersion = new Version(8, 0);
public static bool IsTargetedVersion {get{
return Environment.OSVersion.Version >= TargetedVersion;}}
Now see if the bool IsTargetedVersion is true. Basically,
if(IsTargetedVersion){
//iconic tile code
}
Thus only when a windows phone with compatible features (i.e. an wp8) runs your app then it will work.

Windows Phone 7.5 People Tile

I know that Windows Phone 7.5 has the ability to store contacts in the phone itself. I was wondering if there is a way to modify / extend the people tile so that I can add an option to save the contact to phone instead?
As far as I know from experience as a Windows Phone user and developer, a Contact can be saved to any linked account but not to a generic storage location on the phone. Contacts can be imported from the SIM card, but not saved to it. Contacts that appear to be stored on the phone are actually synced with a linked account (i.e. Hotmail, Google, etc). The People tile/hub aggregates Contact data from all linked accounts.
If you want to programmatically add a contact to the phone, you can use the SaveContactTask from the Microsoft.Phone.Tasks namespace.
using Microsoft.Phone.Tasks;
SaveContactTask _saveContactTask;
void SaveContact()
{
_saveContactTask = new SaveContactTask();
_saveContactTask.Completed +=
new EventHandler<SaveContactResult>(SaveContactTask_Completed);
try
{
saveContactTask.FirstName = "John";
saveContactTask.LastName = "Doe";
saveContactTask.MobilePhone = "2125551212";
saveContactTask.Show();
}
catch (System.InvalidOperationException ex)
{
MessageBox.Show("An error occurred.");
}
}
void SaveContactTask_Completed(object sender, SaveContactResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
MessageBox.Show("Contact saved successfully.");
break;
case TaskResult.Cancel:
MessageBox.Show("Contact save cancelled.");
break;
case TaskResult.None:
MessageBox.Show("Contact could not be saved.");
break;
}
}
The official "How To" documentation can be found here:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394013(v=vs.92).aspx
The contacts are stored by default to the phone itself. It is just synced to your windows live/google/any other account you like to!

Resources