Tips & Tricks: WiFi Direct

Hi Android dev’s,

We are back in business at least on the website. We still are having some technical problems with our hardware.

Today we would like to share some information on WiFi Direct with Android.

What is WiFi Direct?
WiFi Direct is a direct connection between two WiFi devices, which at least one is WiFi Direct certified, without any access point in between. It can do peer to peer connections over WiFi, because it will start up an soft AP (Software Access Point). When the connection is established it can do similar things as bluetooth: share files, print files and play together. It has a larger range then bluetooth.

How to implement it in Android?
From Android 4.0 (API Level 14) it is possible to use WiFi Direct in applications.
Everything we need to do from within the application is coming from: WifiP2pManager. With WifiP2pManager you can do initialization, discover, connect and more.

To start you first need to add the following permissions to your Android manifest.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

After the right permissions are added you need to add a simple broadcast receiver, so your application will receive the events from the Android system.

 * A BroadcastReceiver that notifies of important Wi-Fi p2p events.
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
    private WifiP2pManager manager;
    private Channel channel;
    private MyWiFiActivity activity;
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
            MyWifiActivity activity) {
        this.manager = manager; = channel;
        this.activity = activity;
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                // Wifi Direct is enabled
            } else {
                // Wi-Fi Direct is not enabled
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // request available peers from the wifi p2p manager. This is an
            // asynchronous call and the calling activity is notified with a
            // callback on PeerListListener.onPeersAvailable()
            if (manager != null) {
                manager.requestPeers(channel, myPeerListListener);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing

Then in your activity add the following:

WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
protected void onCreate(Bundle savedInstanceState){
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    Receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
    mIntentFilter = new IntentFilter();

Don’t forget to register and unregister the receiver in onResume() and onPause().

This is the first part you have to do, after this you can use the WifiP2pManager to discover peers and to connect to them.
This is done with the following code to discover peers:

manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
    public void onSuccess() {
    public void onFailure(int reasonCode) {

And for connecting to peers with the following code:

//obtain a peer from the WifiP2pDeviceList
WifiP2pDevice device;
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
manager.connect(channel, config, new ActionListener() {
    public void onSuccess() {
        //success logic
    public void onFailure(int reason) {
        //failure logic

We hope these tips and tricks will help you to start with WiFi Direct on Android.

Kind regards,
PullesSon – Android

Tips & Tricks: TeamBox

Hi Android-devs,

We hope you started the year good.

This week another tips and tricks from PullesSon, but again not an Android specific one.

Like all start-ups we also started searching for solutions to collaborate and project planning. Of course as start-up time is money and there is not much money available. So we started looking for several open-source and easy to setup project management tools.
We used several like dotProject, ProjectOpen, OpenProj and many others. But all had a lot of options and configurations that made it hard to let them work on a self-hosted server. Then we came by TeamBox.

This collaboration and project management tool is easy to setup and has a small and simple wizard to add your first organization, project and tasks. Adding projects, add people to projects and add tasks is really intuitive and easy. The collaboration part has a nice layout and easy to use and understand.
The calendar part is very useful to have an over view the project planning and easy syncable to Google Calendar.
The files part per project is a cool feature and file can be used in the collaboration conversations also, these file can also link directly to a dropbox.

A simple layout overview:

If you don’t have a server, you can always use one of their plans specially the free plan with maximum 3 projects and 50MB storage.

TeamBox is a must for start-ups and even 1 person companies to quick and easy manage the projects, tasks and planning.

The only things we miss are:
1. Add an estimation when planning a task.
2. An Android app (If TeamBox reads this, feel free to contact us).

For more information look at their website and the user guide.

Kind regards,
PullesSon – Android

Tips & Tricks: Screen sizes

Hi Android developers,

Last week was hectic, we needed to reinstall and reconfigure the backend server of Perka’s File stash.

This week we will take a little twist and we will talk about a topic that is more focused on design: Screen sizes.

When designing Android apps the best practice is to use Density-independent pixel (dp) as measure. A dp is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a “medium” density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160).

Another best practice is to use wrap_content and fill_parent in XML so the layout will scale automagically. Also a RelativeLayout is a good practice, it will keep the distances the same even if the screen is bigger or smaller.

Also keep in mind of using layout and orientation qualifiers, which can be added to the /res/ directory. This will make it easy to make to make a different layout for another screen size and/or orientation.

For images used in for the different screens, you might want to use draw9patch to make image.9.png files. These files are processed by the tool and you can assign how it will be stretched.

Another good practice is to use Fragments, this will allow you to reuse designs in different layouts. You can combine the fragments in a layout or just use 1 fragment as layout.

Extended information can be found at: Supporting Multiple Screen Sizes and Android Training Multiscreen

Kind regards,
PullesSon – Android

Tips & Tricks: BugSense

Hi developers,

Last week there was no tips and tricks from PullesSon – Android, because of the workload.

This time it will be not really about code, but about bugs.

For those who found the errors overview in the Android market console useful, but almost never get errors reported, there is BugSense.

BugSense will catch all the exceptions and send it right away to your BugSense dashboard and if you have it activated, also it will send you notification to your email.

Signing up is easy and it will direct you to add an application. Be sure to remember your API Key, even it’s also in your dashboard available.
After this there has to be made some code change in your application. First you have to download and add the library file to your project, then you need to add the internet permission by adding to the android manifest file. Then in the onCreate() method you have to add: BugSenseHandler.setup(this, YOUR_API_KEY);, where YOUR_API_KEY is your API Key from your dashboard (or the one previously remembered). Also don’t forget to include import com.bugsense.trace.BugSenseHandler; else you will get compile errors.

The dashboard is really intuitive and easy to understand. A quick overview of the errors reported is shown on the first page of your dashboard with exception name, file name, line number, OS version and application version. From here you can click on the error and it will show details.

The details page will show you all that’s included in the quick overview plus the amount of occurrences, a short stacktrace, last error instance time, ip, country, phone model, screen information and communication information.

In the configuration of the app registered in the dashboard you can add project viewers, like developers that want to know the error information. Also there is an option to set the application stage between testing and production.

Besides they have twitter and a chat to talk for support.

We also got an email from BugSense, mentioning if we needed help to implement the code, because there were no errors reported.
Seems we configured all well and we did some good coding, because there were just no errors generated. After provoking an exception we received it in the dashboard.

Good service and support on the BugSense’ side and easy to implement and understand. Keep up the good work.

PullesSon – Android

Tips & Tricks: AsyncTask.

Hi Android developers,

This week in tips and tricks: AsyncTask.

A AsyncTask is a class that provides you an easy interface to create a new thread. It is recommended to use it by background tasks, like downloading through a internet connection.
From Honeycomb (SDK API level 11) it’s even mandatory to use Asynctask’s for internet connection downloads and if it’s not done it will force close the application. So it will be also for Ice Cream Sandwich.
Besides it’s mandatory it’s also really handy. You will take pressure from the UI Thread and it will be processed faster and parallel to the UI Thread.

You have to start with:

private class DownloadFilesTask extends AsyncTask<void, void, void>

There are five methods to implement which are:
The first method called in the AsyncTask, called on the UI thread. In this method you usually prepare the progressbar, if needed.

protected void onPreExecute() {

This is the method where is all the action. This method executes the time consuming tasks and publish the task progress, executed in background thread.

protected String doInBackground(String... params) {
    // perform Long time consuming operation
    return null;

Method that updates the progress of the AsyncTask, run on the UI thread. This is where you update the progressbar.

protected void onProgressUpdate(String... progress) {

The final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask, like removing the progressbar.

protected void onPostExecute(String unused) {
    try {
    } catch (Exception e){
        Log.e("APP_NAME","Unknown exception caught: " + e.getMessage());

This method gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask.

protected void onCancelled() {

To execute it just use:

new DownloadFilesTask().execute(param1, param2);

As you can see the AsyncTask is easy to implement and the methods are really easy to use and understand. It is highly recommended to use AsyncTask for task that doesn’t have to update the UI Thread.

We use AsyncTask in Let Me Android That For You and Perka’s File Stash.

For more information:

PullesSon – Android

Perka’s File Stash – update

Perka’s File Stash is updated with the: Flash zip option to flash CWM zips.
Long press on the zip file -> choose: Flash zip -> Wait untill it’s downloaded -> confirm the flash, if the file is already downloaded, just confirm the flash.