Connect your phone to RasPBX via Bluetooth (with chan_mobile)

Connect your phone to RasPBX via Bluetooth (with chan_mobile)

If you would like to use your mobile using the RasPBX system, in this article we will explain how to connect to the Raspberry Pi via bluetooth. We will describe how to use the chan_mobile module for this. We will add a custom trunk and define the settings so that we can initiate outgoing calls using our mobile phone.

Table of Contents

1) Prerequisities

We will not write again at this point that you need a power supply, SD card for your Raspberry Pi, or how to upload the system to the card. We’ll assume you have RasPBX already installed and configured.

If you do not have the system mentioned yet, we recommend that you follow this guide. Of course, you can skip the installation of USB modems, but in the first steps we have already described how to upload the system to an SD card and how to install the system.

In this tutorial we will use the following hardware and software (these are options, not requirements):

  • Raspberry Pi 4 equipped with a Bluetooth adapter (it’s built-in),
  • a mobile phone with Bluetooth (in our case, iPhone SE from 2016),
  • operating system with Asterisk 16, chan_mobile.so and hcitool

You may have read other materials that suggest that bluetoothctl is best tool for pairing devices. And it’s true that you can pair your bluetooth device this way. But this software can be more useful if you connect a mouse or keyboard, for example, and bluetoothctl is not used to pair chan_mobile devices.

It is also worth making sure that the Bluetooth service is working properly and, if necessary, start it with the service bluetooth start.

				
					service bluetooth status
				
			

2) Preparing chan_mobile.conf

In a perfect world, we would log into the Asterisk console (asterisk -r) and load the module with module load chan_mobile.so. However, at this stage we do not have a properly configured file yet, so we will get the error Errors reading config file chan_mobile.conf. Not loading module.

In the basic configuration, chan_mobile.conf will contain three sections:
  • [general] – global settings such as refresh time for connected devices
  • [adapter] – information about the Bluetooth adapter that will wirelessly connect to phones and/or headphones
  • [telefon] – information about the device that will be connected to the adapter
We can literally fit everything in just a few lines.
[general]
interval=30   ; Number of seconds between trying to connect to devices

[adapter]
id=hci0   ; adapter name (can be any name, but must be unique)
address=AA:BB:DD:CC:EE:FF   ; MAC address of the adapter

[phone0]
context=incoming-mobile   ; context for incoming connections
address=00:11:22:33:44:55   ; MAC address of the phone
adapter=hci0   ; adapter used for connection (as above in the [adapter] section)
port=1   ; rfcomm port number (from mobile search) 
First, let’s define our Bluetooth Adapter. We will do it with the command:
				
					hcitool dev
				
			

We will add the result of this command in a moment to our chan_mobile.conf file. It is worth writing down the MAC address of our Bluetooth transmitter/receiver.

Next up will be detecting the device we want to connect to. We’ll do it with:

				
					hcitool scan
				
			

This command may take a little longer and may need to be run several times to find our device. Depending on your location, you may find more devices. For example, if you do the tests in your office, the adapter can detect all your and your colleagues’ smartphones, headphones, and smartbands.

Select the device you want to connect from the list and remember its MAC address.

If you haven’t created chan_mobile.conf yet, now’s a good time. It is worth remembering that it should have the right “owner”.

				
					touch /etc/asterisk/chan_mobile.conf
chown asterisk: /etc/asterisk/chan_mobile.conf
				
			

Let’s enter all the obtained information into the file.

The only variable that we are not able to determine at the moment is the port, so we can enter “1” for now. We’ll change that in a moment.

We can now go back to the Asterisk console. If the file contains all the necessary configuration, incl. information about the adapter and the phone, we will be able to properly load the module. Below is a list of commands that we want to execute in the console (asterisk -rvvv):

				
					module load chan_mobile.so
mobile search
mobile show devices
				
			

As an explanation:

  • module load chan_mobile.so – loading the module for handling mobile channels
  • mobile search – discovering Bluetooth devices
  • mobile show devices – display a list of connected devices

The above commands will also show the port that we will be able to update in the chan_mobile.conf file.

At this point, Asterisk will see your phone and you should be prompted to pair your device.

However, you may run into a problem at this stage. The phone paired, but disconnected after a while and you can see Error initializing Bluetooth device phone0 and Bluetooth Device phone0 has disconnected.

You can find different ways to solve this problem on the internet. For my part, I noticed that the use of rfcomm bind and optionally restarting Asterisk works well. The rfcomm command associates the paired device ID with the serial device name. So let’s execute this command, taking into account the port and MAC address of our phone.

				
					rfcomm bind 8 00:11:22:33:44:55
				
			
This should create an additional device that will be visible on our system.

Finally, it is worth verifying whether the device is visible and connected in the Asterisk console using mobile show devices.

If the device is connected, you can check if the connections are successful. We will do it from the Asterisk console (asterisk -rvvv).

As always …. we’ll call our main company number and play some music. Of course, when using channel originate, we can test the connection in any other way.

				
					channel originate mobile/phone0/227707070 application MusicOnHold
				
			

3) Adding custom trunk (chan_mobile)

Our phone is paired and available in the system. It’s time to add a custom trunk and define the settings, thanks to which we will be able to initiate connections using our mobile phone. You may notice that the setup is very similar to chan_dongle as we covered in this post.

Let’s log into the FreePBX system via a browser.

Select “Trunks” from the top “Connectivity” menu. On the “Trunks” page, add a new trunk and select “Add Custom Trunk”.

In the first tab “General”, at least in the minimum necessary configuration, we are interested in three fields. Below are some sample values with descriptions:

  • Trunk Name: mobile_phone0 (name of the connection, which will later appear e.g. on the list of outgoing routes)
  • Outbound CallerIDempty (presentation for the route; it is not necessary because it will be set upfront by the SIM card operator)
  • Maximum Channels: 1 (maximum number of simultaneous channels for this connection; logic dictates that 1 SIM = 1 channel)

In the third “Custom Settings” tab, define the text string that will be responsible for making the connections. It will be very similar to chan_dongle and also consists of three parts: channel type, connection device, and $OUTNUM$. Most often it will look like this:

  • Custom Dial String: mobile/phone0/$OUTNUM$

Although the first and last variables will remain unchanged, in the middle field, enter the name of your device (not the adapter!) that we defined in chan_mobile.conf.

After entering this data, save the configuration using the “Submit” button, which is usually located in the lower right corner of the screen.

Missing “Outboud CallerID” entry will throw an error, but for chan_mobile connections we don’t have to worry about it. However, if you do not want this error to occur, enter the telephone number in the field provided, preferably in the format 123456789 <123456789>.

4) Outbound route

The configuration of outgoing routes is carried out in the tab “Connectivity” > “Outbound Routes”. To add an outbound route, click “Add route”. In a new route, we need to add settings in two tabs.

In the “Route Settings” tab, we need at least two pieces of information:

  • Route Namemobile_out (unique route name)
  • Trunk Sequence for Matched Routesmobile_phone0 (dropdown trunk list; we will choose the one we just added)

If you want to have one shared outbound route for chan_mobile, you can add multiple items here, e.g. mobile_phone0 and then mobile_phone1. Then the route will try to establish a connection through the first available device.

You can also add a separate route for each device, call them e.g. mobile_out_phone0, mobile_out_phone1, etc … and add only individual devices in the Trunk Sequence.

Then, in the “Dial Patterns” tab, you need to define for which number pattern, we should make connections via this route.

Usually, it will be sufficient to set:

  • Match PatternZX.

With this setting, the route will be used for all calls that are at least 3 digits long and start with a digit other than 0. However, you may want to clarify it more, eg ZXXXXXXXX will only make connections to 9-digit numbers and 48ZX. only for numbers to Poland.

Of course, there can be many more such patterns for each individual route. However, it is worth considering whether the cost of mobile connections for a given route will not be too high.

After that, we will press "Submit" and then, in the upper right corner, we will “Apply” our entire configuration.

When calling outside, you will now make calls using your mobile phone.

Do you have questions or you need an offer?

Contact us!

Most popular

Related Posts