xref: /btstack/port/mtk/docs/ruggear-gettingstarted.tex (revision 8caefee39d444df6d8908a96a844825f10fbdaa4)
1\documentclass[a4paper,titlepage,oneside,12pt]{amsart} %amsart
2\usepackage{graphicx}
3\usepackage{hyperref}
4%\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
5\usepackage[margin=1.3in]{geometry}
6\geometry{a4paper} % or letter or a5paper or ... etc
7% \geometry{landscape} % rotated page geometry
8\usepackage{ifthen}
9
10% See the ``Article customise'' template for come common customisations
11\usepackage[usenames,dvipsnames]{color}
12
13\definecolor{lightgray}{RGB}{245, 245, 245}
14\definecolor{bkblue}{RGB}{18, 47, 76}
15\definecolor{bklightblue}{RGB}{102, 131, 158}
16\definecolor{mygreen}{rgb}{0,0.6,0}
17\definecolor{orange}{RGB}{255,153,0}
18
19\usepackage{opensans}
20\usepackage{setspace}
21\usepackage{booktabs} \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
22
23\usepackage{color}
24\newcommand{\todo}[1]{\colorbox{yellow}{#1}}
25\newcommand{\toread}[1]{{\color{bklightblue} #1}}
26
27\usepackage{listings}
28\lstset{ %
29language=C,                % choose the language of the code
30basicstyle=\footnotesize,       % the size of the fonts that are used for the code
31%numbers=left,                   % where to put the line-numbers
32%numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
33%stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
34%numbersep=5pt,                  % how far the line-numbers are from the code
35backgroundcolor=\color{lightgray},  % choose the background color. You must add \usepackage{color}
36showspaces=false,               % show spaces adding particular underscores
37showstringspaces=false,         % underline spaces within strings
38showtabs=false,                 % show tabs within strings adding particular underscores
39frame=single,           % adds a frame around the code
40framerule=0.2pt,
41tabsize=2,          % sets default tabsize to 2 spaces
42captionpos=b,           % sets the caption-position to bottom
43breaklines=true,        % sets automatic line breaking
44breakatwhitespace=false,    % sets if automatic breaks should only happen at whitespace
45escapeinside={\%*}{*)},          % if you want to add a comment within your code
46belowcaptionskip=5em,
47belowskip=1em,
48aboveskip=1.8em,
49commentstyle=\itshape\color{mygreen},
50keywordstyle=\bfseries\color{black},
51identifierstyle=\color{black},
52stringstyle=\color{blue},
53morekeywords={*, }
54}
55
56\newcommand{\versionNr}{0.1}
57\newcommand{\authorMila}{Dr. sc. Milanka Ringwald}
58\newcommand{\authorMatthias}{Dr. sc. Matthias Ringwald}
59\newcommand{\bkContact}{\href{[email protected]}{[email protected]}}
60\newcommand{\barWidth}{0.3cm}
61
62\makeatletter
63\renewcommand{\maketitle}{
64  \begin{titlepage}
65    \fosfamily
66    \begin{center}
67    \begin{minipage}[b]{\textwidth}
68        \begin{minipage}[b]{.1\textwidth}
69            \color{bkblue}\rule{\barWidth{}}{22cm}
70        \end{minipage}
71        \hfill\begin{minipage}[b]{.8\textwidth}\begin{flushright}
72            {\color{bkblue}VERSION \versionNr{} \\
73            \today \\}
74            \vspace*{7.5cm}
75            \hfill\includegraphics[width=0.95\textwidth]{../../../docs/manual/picts/bklogo.pdf}
76            \vspace*{1.5cm}
77            \begin{spacing}{2}
78                {\huge \color{bkblue} \@title} \\
79                {\Large \color{bklightblue} Getting Started}
80            \end{spacing}
81            \vspace*{1.5cm}
82            {\color{bkblue}\large \authorMila \\
83            \large \authorMatthias \\
84            \large \bkContact\\ }
85        \end{flushright}\end{minipage}
86        \vfill
87        \begin{minipage}[b]{\textwidth}
88            \color{bklightblue}\rule{\barWidth{}}{\barWidth{}}
89        \end{minipage}
90    \end{minipage}
91
92
93    \end{center}
94  \end{titlepage}
95}
96\makeatother
97
98\title[] {BTstack for RugGear/MediaTek Chipsets}
99\author{Copyright \copyright  2014 BlueKitchen GmbH}
100
101%%% BEGIN DOCUMENT
102\begin{document}
103
104\maketitle
105
106\tableofcontents
107\pagebreak
108
109\newcommand{\urlfoot}[2]{\href{#1}{{\color{blue} #2}}\footnote{#1}}
110\newcommand{\BTstack}{\urlfoot{http://bluekitchen-gmbh.com/btstack}{BTstack}{}}
111\newcommand{\CydiaImpactor}{\urlfoot{http://www.cydiaimpactor.com}{Cydia Impactor}{}}
112\newcommand{\CydiaImpactorURL}{\href{http://www.cydiaimpactor.com}{\color{blue} Cydia Impactor}}
113
114\newcommand{\ADT}{\urlfoot{http://developer.android.com/tools/index.html}{Android Developer Tools}{}}
115\newcommand{\nio}{\urlfoot{https://www.bluenio.com/products/accessories/niotag}{nio Tag}{}}
116\newcommand{\PacketLogger}{\urlfoot{http://adcdownload.apple.com/Developer\_Tools/hardware\_io\_tools\_for\_xcode\_\_june\_2014/hardwareiotools\_june\_2014.dmg}{PacketLogger}{}}
117\newcommand{\Wireshark}{\urlfoot{http://www.wireshark.org}{Wireshark}{}}
118\newcommand{\Cygwin}{\urlfoot{https://www.cygwin.com}{Cygwin}{}}
119\newcommand{\MSYS}{\urlfoot{www.mingw.org/wiki/msys}{MSYS}{}}
120\newcommand{\adb}{\urlfoot{http://developer.android.com/tools/help/adb.html}{Android Debug Bridge}{}}
121
122This documents describes how \BTstack{} can be installed and used on RugGear devices with MediaTek chipset. It also presents measurements of the reception of Advertising reports from a remote devices, e.g. beacons or peripherals, as this is the crucial step for discovering and connecting to them.
123
124\section{Hardware Setup}
125To install BTstack on a RugGear mobile phone, connect the RugGear device to a Mac or Linux system using a micro USB cable. The installation might also work on Windows with \Cygwin{} and/or \MSYS{} installed. The RugGear device is connected to an USB port during setup and development.
126
127\section{General Tools}
128
129\begin{itemize}
130\item \CydiaImpactor{} to get root access.
131\item Google's \ADT{} (ADT) to develop an Android LE Client.
132\item The \adb{} (adb) to communicate with a connected Android device via a command line. It comes as a part of the Android Developer Tools.
133\item Apple's \PacketLogger{} (available to the registered developers as part of the Hardware IO Tools for Xcode download) or \Wireshark{} to look and analyze Bluetooth packet logs.
134\item Some LE devices that send Advertising reports to test your LE Client. Here, we used the \nio{} and a BTstack-based LE Peripheral on a desktop machine using the BCM20702A0 module.
135\end{itemize}
136
137\section{Rooting the RugGear Device}
138There are various ways to root an Android device. We recommend the \CydiaImpactorURL{} tool. It is available for all major platforms, it works with most Android devices and it comes from a trustworthy source\footnote{It was created by Jay Freeman, who has supporting the use iOS devices outside of Apple's walled garden since the device came out}.
139
140To root the device, start Impactor, and press the "Start" button as shown in Fig.\ref{fig:cydiaimpactor}
141
142\begin{figure}[htbp]
143\begin{center}
144 \includegraphics[width=\textwidth]{picts/cydiaimpactor.png}
145\caption{Cydia Impactor running on OS X.}
146\label{fig:cydiaimpactor}
147\end{center}
148\end{figure}
149
150\section{Installing BTstack on RugGear devices with MediaTek Chipsets}
151
152First, extract the provided \path{btstack-android-mtk-VERSION.tar.gz} archive. Please make sure that the path to the extracted archive does not contain spaces as the installer may fail in this case. Then, start the provided \path{./installer.sh} in the \path{mtk} folder. This should look similar to this listing.
153
154\begin{lstlisting}
155$ mkdir btstack
156$ tar -zxvf btstack-android-mtk.tar.gz -C btstack
157$ ls btstack
158java	mtk
159$ cd btstack/mtk
160$ ./installer.sh
161BTstack Installer for RugGear/Mediatek devices
162from: .
163- /system mounted as read/write
164- stopping Bluetooth daemon
165- transfer files to device
1669192 KB/s (279736 bytes in 0.029s)
1672949 KB/s (6188 bytes in 0.002s)
1687753 KB/s (62360 bytes in 0.007s)
1694184 KB/s (11316 bytes in 0.002s)
1703201 KB/s (6592 bytes in 0.002s)
1715348 KB/s (11720 bytes in 0.002s)
172- put files in place
173- start BTstack daemon
174DONE
175\end{lstlisting}
176
177If BTstack was installed properly, we can have a look at its packet log.
178
179\begin{lstlisting}
180$ make hci_dump
181killall PacketLogger
182adb shell su root chmod 666 /data/btstack/hci_dump.pklg
183adb pull /data/btstack/hci_dump.pklg 2> /dev/null
184open hci_dump.pklg
185\end{lstlisting}
186
187The \path{make} \path{hci_dump} command assumes that the underlying system is OS X and that PacketLogger is already running. The two\path{adb} commands, shown in the following Listing, are used to fetch the packet log (hci\_dump.pklg) and it can be run on any platform. Wireshark can then be used to open the packet log.
188
189\begin{lstlisting}
190$ adb shell su root chmod 666 /data/btstack/hci_dump.pklg
191$ adb pull /data/btstack/hci_dump.pklg 2> /dev/null
192\end{lstlisting}
193
194The initial packet log should look like in Figure \ref{fig:firstLog}.
195
196\begin{figure}[htbp]
197\begin{center}
198 \includegraphics[width=\textwidth]{picts/ruggear_initial_hci_dump.pdf}
199\caption{hci\_dump.pklg right after BTstack daemon was installed.}
200\label{fig:firstLog}
201\end{center}
202\end{figure}
203
204\section{Running the First Example}
205
206As first, make sure that BTstack was installed properly and it is running, i.e. by checking the packet log as explained in the previous section. Now, let's do an LE Scan using a test program written in C against libBTstack.
207
208\begin{lstlisting}
209$ adb shell
210$ le_scan
211le_scan started
212- connecting to BTstack Daemon
213- connected
214- send power on
215- btstack state 1
216- btstack state 2
217- start LE scan
218- ADV: 3E 0F 02 01 00 01 F2 01 8F 45 16 66 03 02 01 1A B3
219\end{lstlisting}
220
221The \path{le\_scan} just dumps the data of each received advertisement. It uses the default parameters used by iOS, which are scan window 30 ms, scan interval 300 ms. More on these parameters is in Section \ref{ss:measurements}. The packet log looks like in Figure \ref{fig:leScan}.
222
223\begin{figure}[htbp]
224\begin{center}
225 \includegraphics[width=\textwidth]{picts/scan_parameters_hci_dump.png}
226\caption{HCI dump of the le\_scan program.}
227\label{fig:leScan}
228\end{center}
229\end{figure}
230
231\section{BTstack Java API}
232
233BTstack on RugGear/MediaTek provides its own Bluetooth stack and its own interface for using it. While BTstack has been certified for Classic SPP and LE Peripheral, it does not provide a complete Java API for this. At the moment, the functionality to turn Bluetooth on/off, scan for LE devices, connect, and make use of Services and Characteristics is provided. This covers the use of BTstack as LE Central. Please note that the Security Manager is not implemented yet. Use of the Security Manager is necessary when devices require explicit pairing before some services can be used.
234
235 The Java classes that make up the BTstack API  are split into three folders. Please add all of them to your Android Java project:
236
237 \begin{enumerate}
238\item \path{java/src} - main code for BTstack client, i.e., socket communication with BTstack server that runs as a daemon.
239\item \path{java/android} - Android specific Client/Server communication.
240\item \path{java/gen} -  code for available commands and events, auto-generated from the BTstack C source.
241\end{enumerate}
242
243\subsection{BTstack GATT Client for Android example}
244More documentation on the Java API is needed. For now, please have a look at the Anrdoid example in \path{java/example/com/bluekitchen/GATTClientTest.java}. It connects to BTstack Server and turns Bluetooth on. On success, it starts an LE Scan for devices. When it finds a device, it connects to it and queries the available Services. Then, it requests the list of Characteristics for the first service. Finally, it performs some read/write operations on the found Characteristics.
245
246BTstack server runs as a daemon. In the event of a crash, the Java client will get notified and can restart the daemon, to provide a continuous use of the Bluetooth services.
247
248\section{BCM20702A0 and RugGear RG500 Reception of Advertisements}
249\label{ss:measurements}
250
251During the first tests of BTstack on RugGear/MediaTek, we have seen that it receives less Advertisements than other devices.
252
253Advertisements serve three main purposes:
254\begin{enumerate}
255\item They provide information that a particular device is in range and turned on.
256\item They can provide some information without connecting to a particular device (e.g. its name, provided services, or even manufacturer specific data).
257\item They allow to connect to a particular device, i.e., an LE device listens for incoming connection requests only a short moment after sending an Advertisement (to save energy).
258\end{enumerate}
259
260A setup with one device that sends Advertisements and one device that receives them, can be completely described by three parameters:
261\begin{enumerate}
262\item the \emph{Advertisement Interval} - the period between two consecutive Advertisements being sent.
263\item the \emph{Scan Interval} - it determines how frequent the receiving device listens for Advertisements.
264\item the \emph{Scan Window} - the time window in which the receiving device listens for Advertisements.
265\end{enumerate}
266
267\subsection{Setup} To evaluate the MediaTek Bluetooth chipsets used in the RugGear RG500, we compared them to a common Bluetooth USB Dual-Mode Dongle that uses the Broadcom BCM20702A0. We used two LE Peripherals. As the first LE Peripheral, we setup another device with the BCM20702A0 and configured it to advertise every 30 ms. This is very often, but it helps to establish how good Advertisements are received, if they occur frequently. As a second more real-world example, we used the nio Tag without further configuration. Based on the measured results, the nio Tag advertises roughly every second.
268
269We measured two cases:
270\begin{enumerate}
271\item continuous scanning (Scan Window 30ms, Scan Interval 30ms),
272\item intermittent scanning (Scan Window 30ms, Scan Interval 300ms) as performed by iOS.
273\end{enumerate}
274
275\subsection{Process}
276For the continuous and intermittent scanning measurements on the RugGear, we used the \path{le_scan} test. We let it run for 15 minutes in continuous, and then 15 min in intermittent scanning setup. For each of the two measurement, we retrieved the packet logs from the device.  We processed it an analyzed it using two Python scripts \path{process_scan.py} and \path{plot_scan.py}. The measurements with the BCM module have been carried out with the GATT Browser example of BTstack on OS. Hence, we have up to four experiments for each setup:
277\begin{itemize}
278\item BCM/BCM - BCM20702A0 receives Advertisements from second BCM 20702A0 module
279\item BCM/nio - BCM20702A0 receives Advertisements from nio tag
280\item RugGear/BCM - RugGear RG500 receives Advertisements from BCM 20702A0 module
281\item RugGear/nio - RugGear RG500 receives Advertisements from nio tag
282\end{itemize}
283
284\subsection{Measurements with continuous scanning}
285For this experiment with set the Scan Interval to 30 ms and the Scan Windows to 30 ms, which translates to continuous scanning.
286
287The plot in Figure \ref{fig:accumulatedAds} shows how many Advertisements have been received for both receivers from the two senders. In the BCM/BCM combination, 27 out of max 33 (1s /30 ms) Advertisements have been received. On the RugGear device, only about 1 in 15 Advertisements have been received.
288
289Figure \ref{fig:delayHist} shows the distribution of the time between two received Advertisements over the full range.
290
291All setups but the RugGear vs. nio Tag, received an Advertisement every second, which is sufficient for most interactive applications. We further analyzed the RugGear vs. nio Tag setup in more detail. For this setup, we calculated the expected delay between the start of a scan and the first received Advertisement based on the previous measurements as shown in Figure \ref{fig:expectedDelay}. An alternative and easier to read representation of the same is given in Figure \ref{fig:cdfExpectedDelay}. From this Figure we can, for example, see that we have 20 percent probability of receiving an Advertisement with less then 1 second.
292
293\subsection{Measurements with normal scanning}
294For this experiment with set the Scan Interval to 300 ms and the Scan Windows to 30 ms, which translates to scanning for 1/10 of the time. As the scanning time is reduced by a factor of 10, we also expect the number of received Advertisements to be lower by 1/10. We didn't had the nio Tag around this time.
295
296The plot in Figure \ref{fig:accumulatedNormalAds} shows the number of Advertisements received for both receivers. The BCM/BCM received 2.9 Advertisements per second (adv/s) which is roughly 1/10 of the 27 adv/s received in the continuous scanning experiment. For the RugGear/BCM combination, we received 0.6 adv/s, which is roughly 1/3 of the 1.8 adv/s received in the continuous scanning experiment and is unexpected.
297
298We conclude from this data, that the MediaTek chipset didn't do continuous scanning in the first experiment. Instead, it looks like it was scanning for only 1/3 of the time, potentially due to internal resource limitations or scheduling problems. In the intermittent scanning setup, the RugGear device received 1/5 of the Advertisements received by the BCM chipset.
299
300
301		\begin{figure}[htbp]
302		\begin{center}
303		 \includegraphics[width=0.9\textwidth]{picts_experiments/nio_acc_number_of_advertisements_continuous_scanning.pdf}
304		\caption{Advertising reports accumulated over time, continuous scanning.}
305		\label{fig:accumulatedAds}
306		\end{center}
307		\end{figure}
308
309		\begin{figure}[htbp]
310		\begin{center}
311		 \includegraphics[width=0.9\textwidth]{picts_experiments/nio_histogram_advertisements_time_delay.pdf}
312		\caption{Time delay histogram,  continuous scanning.}
313		\label{fig:delayHist}
314		\end{center}
315		\end{figure}
316
317		\begin{figure}[htbp]
318		\begin{center}
319		 \includegraphics[width=0.9\textwidth]{picts_experiments/nio_ruggear_expected_scan_response.pdf}
320		\caption{Expected time until first Advertisement, continuous scanning.}
321		\label{fig:expectedDelay}
322		\end{center}
323		\end{figure}
324
325		\begin{figure}[htbp]
326		\begin{center}
327		 \includegraphics[width=0.9\textwidth]{picts_experiments/nio_ruggear_cdf.pdf}
328		\caption{Cumulative distribution of expected time until first Advertisement, continuous scanning.}
329		\label{fig:cdfExpectedDelay}
330		\end{center}
331		\end{figure}
332
333		\begin{figure}[htbp]
334		\begin{center}
335		 \includegraphics[width=0.9\textwidth]{picts_experiments/nio_acc_number_of_advertisements_normal_scanning.pdf}
336		\caption{Advertising reports accumulated over time, normal scanning.}
337		\label{fig:accumulatedNormalAds}
338		\end{center}
339		\end{figure}
340
341%\end{minipage}
342
343
344\end{document}