Beneth :: Blog - Tag - Nokia2023-08-31T15:23:27+02:00benethurn:md5:dfdc66ad106323f56392cd470185b1f4DotclearImport contacts on old Bluetooth Phonesurn:md5:89685f48326169f0455e2e2d85a42d132016-11-23T14:20:00+01:002016-11-28T09:09:13+01:00BenethinformatiquecaldavNokiapythonvCard<p>Few weeks ago, my <em>smart</em>phone died.
Someone lent me an old Nokia phone (6021) and I was looking for a way to send all my contacts and phone numbers to it.</p>
<p>The Nokia phone supports Bluetooth connectivity and seems to understand the vCard standard <a href="https://tools.ietf.org/html/rfc6350">(rfc6350)</a>.
All contacts can be exported as vCard file using Mozilla Thunderbird "Export" features in Address Book.</p>
<p>We will see that Nokia phone does not follow the vCard standard and we will need to hack the vCard information to be understandable by the phone. I use Python with module <a href="https://eventable.github.io/vobject/">vobject</a> to do that.</p>
<h3>Exporting contacts as vCard file</h3>
<p>I use Thunderbird as mail client, and my contact are stored on my server using the Caldav protocol.
Mozilla Thunderbird offers to export contacts in the main address book interface ( « Tools -> Export » and choose vCard export type)</p>
<p>If you use an other way of storing your contacts, like Google with Gmail, you can also export your contact with vCard export format.</p>
<p>I have created two contacts to show the incompatibility with the phone.</p>
<ul>
<li>test 1</li>
<li>test éàéç 2</li>
</ul>
<p>You will end up with a file like this one « <strong>vcard_test_export.vcf</strong> » :</p>
<pre><code>begin:vcard
fn:test 1
n:1;test
tel;cell:+33 6 00 00 00 00
version:2.1
end:vcard
begin:vcard
fn;quoted-printable:test =C3=A9=C3=A0=C3=A9=C3=A7 2
n;quoted-printable:2;test =C3=A9=C3=A0=C3=A9=C3=A7
tel;cell:+33 7 00 00 00 00
version:2.1
end:vcard
</code></pre>
<h3>Sending vCard to Nokia phone</h3>
<p>If we try to send this file to the Nokia phone, via Bluetooth, it detects it correctly as a vCard file, and ask you to add the contact to the phone.
But, it will do it only for the first contact in the file ("test 1" in our example).</p>
<p>So the first issue is that the Nokia phone does not support multiple contact in one vCard file. We will have to split each contact in its own vCard file.</p>
<p>Also, the second contact has specific characters that are not part of the ASCII table. You can see that exporting a vCard will output the specific character as "quoted-printable" format with some specific code like : "=C3=A9".</p>
<p><a href="https://en.wikipedia.org/wiki/Quoted-printable">Quoted-printable</a> strings are a manner of encoding character which prevent to be incorrectly parsed when exporting (or sending over a wire) data. All escape code are prefixed by a "=" sign followed by the code.</p>
<p>So here : <strong>=C3=A9</strong> is : <strong>\xc3\xa9</strong>. It is UTF-8 encoding for representing the character "é".</p>
<p>But, what happens if we sent the second contact via Bluetooth ?
The Nokia phone do not understand the UTF-8 encoding, and save the contact name as is like:</p>
<pre><code>"test =C3=A9=C3=A0=C3=A9=C3=A7 2"
</code></pre>
<p>If we look at the standard page for Charset specification (<a href="https://tools.ietf.org/html/rfc6350#section-3.1">section #3.1</a>) it says:</p>
<pre><code>« The charset (see [RFC3536] for internationalization terminology) for vCard is UTF-8 as defined in [RFC3629]. »
</code></pre>
<p>And it is quite clear with this sentence:</p>
<pre><code>« There is no way to override this. »
</code></pre>
<p>So the phone tricks the standard.
But how to find the expecting encoding charset for vCard on this Nokia Phone ?
This is simple, we will create a contact on the Nokia phone, "test éçà" and send it via Bluetooth to the computer. (Menu contact, "Send bus. card"-> "via Bluetooth")</p>
<p>Open the VCF file on your computer, and what do you see ? The Nokia phone overrides the encoding information by adding a Charset specification ("N;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:").</p>
<p><a href="https://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO-8859-1</a> is another Charset specification, widely used before UTF-8 encoding for encoding latin specific characters.</p>
<p>So we have 2 issues here:</p>
<ul>
<li>Charset encoding</li>
<li>vCard Split: One VCF file per contact</li>
</ul>
<h4>Issue 1 : Hacking the vCard encoding</h4>
<p>We will need to hack the vCard output to transform quoted-printable in UTF-8 format to be quoted-printable in ISO-8859-1.
Also, to keep easy the formating I decide to remove the field FN from vCard output. (Only N; field will be kept)</p>
<h4>Issue 2 : Splitting the vCard file</h4>
<p>For each vCard contact discovered in the vCard output, we will create a single VCF file with the vCard information.</p>
<h4>Bonus : Filtering interesting contact</h4>
<p>As a Bonus, I will only kept contact with a phone number.</p>
<h3>Python Script</h3>
<p>I made a simple Python script (<strong>2.7</strong>) to address theses two issues.
It takes one argument, a vCard file and re-encode and split all contact in it (only those with a phone number) to the "out/" directory by creating one VCF file per contact.</p>
<p>When done, you will end up with all your vCard files in the "out/" directory. Each vCard file is named by the contact name. To send it to your phone, you must send each separate vCard file via Bluetooth, and for each contact, manually accept it on the phone. (It takes me about 10 minutes for ~ 200 contacts)</p>
<ul>
<li>For vCard parsing I use the module « <a href="http://eventable.github.io/vobject/">vobject</a> » python module.</li>
<li>For quoted-printable decode and re-encode, I use <a href="https://docs.python.org/2/library/quopri.html">quopri</a> python module.</li>
<li>To hack the encoding for <strong>N</strong> field and to remove the <strong>FN</strong> field, I use two simples regular expressions, with Python « <a href="https://docs.python.org/2/library/re.html">re</a> » module.</li>
</ul>
<p>The python script is available on my personal git repository <a href="https://dev.beneth.fr/beneth/vcard_legacy">here</a>.</p>
<p>You can clone it:</p>
<pre><code>$ git clone https://dev.beneth.fr/beneth/vcard_legacy.git
</code></pre>
<p>The script name is <strong>split_vcard.py</strong>.</p>
<p>To run it, you have to give as first argument your vCard export file, like this:</p>
<pre><code>$ ./split_vcard.py vcard_test_file/vcard_test_file_1.vcf
</code></pre>
<p>Do not hesitate to comment and/or improve the script if you use it.</p>
<h3>Links</h3>
<ul>
<li><a href="https://dev.beneth.fr/beneth/vcard_legacy">vCard legacy repository</a> on <a href="https://dev.beneth.fr">dev.beneth.fr</a></li>
<li><a href="https://tools.ietf.org/html/rfc6350">vCard RFC</a></li>
<li><a href="https://en.wikipedia.org/wiki/Quoted-printable">Quoted-printable encoding</a> on Wikipedia</li>
<li><a href="http://eventable.github.io/vobject/">vObject Python module</a></li>
<li><a href="https://docs.python.org/2/library/quopri.html">quopri Python module</a></li>
<li><a href="https://docs.python.org/2/library/re.html">re Python module</a></li>
</ul>
https://blog.beneth.fr/post/2016/11/23/sync-contact-on-old-bluetooth-phones#comment-formhttps://blog.beneth.fr/feed/atom/comments/47