Fake a Phone Call from Wear OS Smartwatches using Xamarin, Azure Functions, and Twilio
Tags: dotnet, azure, twilio, serverless
Published: 2021-03-07 09:58:39

We have all been in that situation where we are stuck chatting with someone we want to run away from but we can't. It could be at a party, a convention, anywhere basically, but it happens.

<img src="https://media.giphy.com/media/26gsd0Fr9K5pV3R6w/giphy.gif" alt="this is so frustrating gif" width="350px" height="auto">

At that point, all that we hope for is someone's phone call to let you pull yourself away from the absolute madness you have to deal with in the name of social behaviour!

At that moment, you can never know whether your saviour shall appear in the form of a *tring tring* from your phone, but what if I told you that you could make that *"call"* happen without someone else calling you anyway?

<img src="https://media.giphy.com/media/3o7bu0uduvjqx3skz6/giphy.gif" alt="Chris Pratt surprised gif" width="350px" height="auto">

Ladies and gentlemen, I present to you all...🥁🥁🥁

### 🕵️‍♂️ FakeACall 📲

**FakeACall** is an application that I made for my **Wear OS by Google** smartwatch to **fake a phone call** to my phone to get out of tricky situations. It has been built with **Xamarin.Android** and uses an **Azure Function** and **Twilio Programmable Voice** to achieve this aim. 

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0lh6f6etfxwsrqtps7b.png" alt="FakeACall" width="800px" height="auto"> 

The best part about this app is that we can all build it for ourselves. And that's exactly what we're going to do now!!!

## Step 1: Understand the Application Flow

In this app, there will be two major components:

* The **Azure Function** to call the **Twilio Programmable Voice** API 
    * This will be responsible for making the phone call to your phone from your Twilio number
* The **Wear OS** smartwatch app built using **Xamarin.Android** to call the **Azure Function**
    * This will be responsible for triggering the Azure Function to ensure that the call is made

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6dkiwsv0hagylyq6kky.jpg" alt="FakeACall App Flow" width="800px" height="auto">  

Let's go ahead with developing our Azure Function first.

## Step 2: Build an **Azure Function** to call the **Twilio Programmable Voice** API using **.NET**

**Azure Functions** is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running. **Twilio Programmable Voice** is Twilio's API that allows you to quickly make and receive voice calls in your application.

We will build an Azure Function to call the Twilio Programmable Voice API which will make a call to your mobile phone. For this task, you will need the following:

* A **Microsoft Azure** Account
    * You can create an Azure account for free [here](https://azure.microsoft.com/en-us/free/). In case you're currently an enrolled student with a *.edu* email address, you can get $100 worth of Azure credits by availing the [Azure For Students offer](https://aka.ms/a4s) too.
<br><br>
* A **Twilio** Account and Phone Number
    * Head over to [the Twilio website](https://twilio.com) and sign up for an account if you don't have one already. Add a phone number to your account that has calling and SMS capabilities. If you're a student, you can get $50 worth of Twilio credits for free through the GitHub Student Developer Pack. Read more about this [here](https://www.twilio.com/blog/twilio-perks-students-and-educators-now-available-github-education).
<br><br>
* **Visual Studio Code** along with the relevant extensions
    * We will be using VS Code to build our Azure Function here. You can also use Visual Studio, the Azure Portal, etc. to achieve the same. Once you have [VS Code installed](https://code.visualstudio.com/download), please also install the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) and the [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions).
<br><br>
* **Azure Functions Core Tools** version 3.x
    * Azure Functions Core Tools lets you develop and test your functions on your local computer from the command prompt or terminal. Your local functions can connect to live Azure services, and you can debug your functions on your local computer using the full Functions runtime. You can even deploy a function app to your Azure subscription. Please install Azure Functions Core Tools from [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools).
<br><br>
* **.NET Core** 3.1
    * As we will be building our Azure Function with C# and .NET, you must [install the .NET Core 3.1 SDK](https://dotnet.microsoft.com/download). You can build Azure Functions using other languages such as Java, JavaScript, Python. etc. as well.

*Note: In case you have never tried building an Azure Function with .NET, please check out [this little tutorial](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-csharp)*.  

Once we have all of our necessities, let's begin:

<img src="https://media.giphy.com/media/iFxXouCf76ZencqIRP/giphy.gif" alt="go time gif" width="350px" height="auto">

1. In VS Code, choose the **Azure** icon in the Activity bar, then in the **Functions** area, select the **Create new project...** icon.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrfkrsik5rb4vw88f85p.png" alt="VS Code Azure Functions Area" height="300px" width="auto">

2. Select a directory location for your project.

3. Provide the following information at the prompts:

    1. Select a **language** for your function project: Choose `C#`

    2. Select a **template** for your project's first function: Choose `HTTP trigger`

    3. Provide a **function name**: Type `FakeCall`

    4. Provide a **namespace**: Type `Company.Function`

    5. **Authorization** level: Choose `Anonymous`

    6. Select how you would like to **open your project**: `Open in current window`

VS Code will use all this info to generate an Azure Functions project with an HTTP trigger.

Once that is done, navigate over to the `FakeCall.cs` file visible in your directory and replace the existing template with the following code:

{% gist https://gist.github.com/adityaoberai/49dc423dc7b5ae3eb1eb3358ce8e73f5 %}

This Azure Function here will utilise the Twilio SDK for C# and .NET to interact with the Voice API and make a phone call. 

Make sure that you install the [Twilio NuGet package](https://www.nuget.org/packages/Twilio/) using the .NET Core command-line tools by running the following command in your command prompt/shell:

```sh
dotnet add package Twilio
```  

You will also have noticed that there are 4 environment variables:  
 
* `TWILIO_ACCOUNTSID`: The Account SID from your Twilio dashboard
* `TWILIO_AUTHTOKEN`: The Auth Token from your Twilio dashboard
* `NUMBERTOCALL`: The phone number you intend to call, i.e. your own phone number
* `TWILIO_PHONENO`: The Twilio phone number you added to your account

We will add these to our Azure Function App in the portal after deployment. To do that, complete the following steps:

1. Choose the **Deploy to function app...** button in the **Functions** area we visited earlier
 
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbgjtuacqlp7ltajzfqh.png" alt="VS Code Azure Functions Area" height="300px" width="auto">

2. Provide the following information at the prompts:

    1. Select **subscription**: Choose the subscription to use (you won't see this if you only have one subscription)

    2. Select **Function App** in Azure: Choose `- Create new Function App`

    3. Enter a **globally unique name** for the function app: Type a name that is valid in a URL path

    4. Select a **location** for new resources: For better performance, choose a [region](https://azure.microsoft.com/regions/) near you.

3. Select **View Output** in the notification that will appear to view the creation and deployment result. You will be able to find the URL generated for the function here. If you miss the notification, select the bell icon in the lower right corner to see it again. 

We also have to [add the environment variables to our Azure Function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings), so log in to the Azure portal, visit your Function App, and find **Configuration** under **Settings** in the left pane. You can add your environment variables there as a **New application string**.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32mkrs07ex9ajntp34f4.png" alt="Function Application Strings" width="1000px" height="auto">

And our Azure Function is ready to run! You can send either a **GET** or a **POST** HTTP Request to the Function URL and it will call your phone.

<img src="https://media.giphy.com/media/4PT6v3PQKG6Yg/giphy-downsized.gif" alt="Denzel Washington Reaction GIF" width="350px" height="auto">

Let us now build the Wear OS app for our smartwatch that we will use to call this Azure Function and thus, fake a phone call.

## Step 3: Build the **Wear OS** smartwatch app with **Xamarin.Android**

**Xamarin** is an open-source platform for building modern and performant applications for iOS, Android, and Windows with .NET.

We will build a Wear OS app using Xamarin.Android, which, for our smartwatch now to call the Azure Function we just built. For this task, you will need the following:

* **Visual Studio IDE** with *Mobile development with .NET* workload
     * Visual Studio is the officially supported IDE for Xamarin development. If you have not yet, go ahead and install the Community edition for free from [here](https://visualstudio.microsoft.com/downloads/). When setting it up, download and install the *Mobile development with .NET* workload, which will install all Xamarin-related components. Once you do that, you will need to verify that you have the right versions of Xamarin.Android, Android SDK, and Java JDK installed. You can find the minimum requirements needed to build a Wear OS app [here](https://docs.microsoft.com/en-us/xamarin/android/wear/get-started/installation).

*Note: In case you have never tried building a Wear OS smartwatch app, do try [the **Hello, Wear** tutorial](https://docs.microsoft.com/en-us/xamarin/android/wear/get-started/hello-wear)*.  

Now we can go ahead, create a new project using the **Android Wear App (Xamarin)** template and name the solution **FakeACall**. 

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ao7icgm7vnp130abfu80.png" alt="Xamarin Project Creation" height="800px" width="auto"> 

As soon as that is done, let's get coding:

<img src="https://media.giphy.com/media/q6RoNkLlFNjaw/giphy.gif" alt="cat coding gif" width="350px" height="auto">

1. **Create the UI**

    Visit the **./Resources/layout** folder in the app directory, open the `activity_main.xml` file and replace the template with the following code:

    {% gist https://gist.github.com/adityaoberai/80b05830137eec28f94395f30d0bd9bb %}
    
    What we're essentially doing here is adding a Button with some text. This text will change when the application runs in order to notify you whether the call occurred successfully or not.     

    As soon as that is done, we will go ahead and create the backend for the app.

2. **Create the backend**

Go over to the `MainActivity.cs` file in the app directory and replace the template with the following code:

{% gist https://gist.github.com/adityaoberai/fa3c37ac7b7ab8af471cfad661cc0406 %}

Here, we are making a POST HTTP Request to our Azure Function and then closing the app automatically once the app notifies us whether the app sent the POST Request successfully or not.

Make sure that you install the following NuGet packages using the **NuGet Package Manager** through Visual Studio:

* [NETStandard.Library](https://www.nuget.org/packages/NETStandard.Library/)
* [System.Net.Http](https://www.nuget.org/packages/System.Net.Http/)

Once this step is complete, your app is ready for testing.

<img src="https://media.giphy.com/media/OcZp0maz6ALok/giphy.gif" alt="self fice gif" width="350px" height="auto">

## Step 4: Test the app and enjoy

You can now go ahead and test your app. Debugging the app on your Wear OS device instead of the emulator will deploy it there for usage later too, so you can do that instead by following the instructions [here](https://docs.microsoft.com/en-us/xamarin/android/wear/deploy-test/debug-on-device). In case you do so, make sure that your Wear OS device is connected to your smartphone before you run the app. I will recommend saving the Twilio number as a contact to make your fake call seem more legitimate (for the demo, I saved it as `Work Twilio`). 

If all has gone well, your experience will be the same as my demo:

{% youtube fppaQONIPpw %}

**And that is all for today folks!**

If you reached the end, my heartiest congratulations to you!

<img src="" alt="" width="350px" height="auto">

One of the best parts about this app here is that the Azure Function can be called from any device capable of making HTTP requests, so you can extend this functionality to other platforms too. Slight tweaking of the message content in the Twilio API can also turn this into a very useful SOS app.

I sincerely hope this app will be able to help you in case you're stuck in any troublesome situation. I am sharing my original **GitHub repo** for this app below as well. If you like it, do show some love by giving it a ⭐

{% github adityaoberai/FakeACall %}

#### Thank you so much for reading this article. Keep learning, building, sharing, and have a great day! ❤