lördag 7 december 2013

Android : Working with Bluetooth

I've been doing some research on using Android API for Bluetooth as i wanted to have a sync function in my app by pairing the devices.

I started at Googles Android Bluetooth guide located at http://developer.android.com/guide/topics/connectivity/bluetooth.html. It explains the basics and with some helpful code snippets. Easy to make an device discoverable but the connection handshake didn't really work well for me in my play project using AsyncTask instead of spawning threads.

So i've compiled the BluetoothChat example that comes with the SDK to see how they did it.
Simple and easy, example can be found online here.

Messages was really easy to send between the devices but i wanted to send a file.

Excerpt from BluetoothChatService.java:
 public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothChatService.this.start();
                    break;
                }
            }
        }

As you can see it reads the buffer than send the bytes back to activity. I found a nice little Bluetooth library for sending files located at https://github.com/simonguest/android-btxfr/tree/master/src/com/simonguest/btxfr.
Locate DataTransferThread.java to see his elegant solution.

With help from that i implemented the same thing thing he did but in BluetoothChatService. Send filesize, run until data received send it back to the activity. Read it, import it then re-send data to the device that clicked 'Sync'.

Hope this will help those of you that don't want to use the native Bluetooth intent!


onsdag 20 november 2013

Android : What does your dev. station look like ?

Currently renovating my new 'computer room'.

Just need to do the electrical bit and then done. 2xCAT-6 for whatever reasons. Room will probably become a baby room in the distant future.
Enough about that, my current setup looks like this

Computer build to be silent, the monitor needs to be updated with two new ones someday, like to see if it you gain some effectiveness. Must be nice to see the code while your reading up on something. Included a picture of my Berserk collection, #37 finally on the way! Hopefully Kentaro Miura can finish it, no end in sight and one volume a year.

On to the software side.

  •  IntelliJ IDEA 12.1 - I keep nagging about IDEA but it's my favorite IDE for Android, i tried Android Studio when it got released but need to wait until it gets a bit more mature.
  • Notepad++ - Switched over from regular Notepad because it doesn't understand the LF char. The config files included in Android tend to only have that.
  • Take Command - If you've used 4DOS/4NT in the old days this is it. Nice to have tab autocompletion in the command prompt, easily scriptable. Mostly started when i'm using 'adb shell', 
  • Spotify - Music in the background, when typing this, 'Of Monsters and Men'.

That's the programs i use to for my Android development. (Android SDK i didn't count) You might notice there's no external source control, i backup and date my project directory and use the local history in the IDE. Gonna start using one though, tips ? Tips for other big or small utility applications are welcome!

http://www.glitchthegame.com/ is all over the news, they've released all their game assets as public domain. Never heard of the game earlier but that's a really nice gesture, that's some nice art to start from or use.
I have a feeling their art will pop up in some form in more than a few games, which i guess is the case based on their headline, Glitch is Dead, Long Live Glitch!

måndag 20 maj 2013

Android Studio v0.1 - Yay, Google is co-operating with Jetbrains!

This is great news for all of us that prefer IntelliJ over Eclipse!

Jetbrains posted a little blogpost here about Android Studio and that it's based on the IntelliJ platform.
Just installed Android Studio and it looks exacly like IntelliJ IDEA. (Their Darcula theme is included.) Haven't played much with it yet as i had to write this to express my joy!

Opening Android projects created in IntelliJ worked without a hassle, had to re-specify the Android SDK but that's all.

The xml editor have gotten a little preview facelift, and ListViews now displays rows so you can see how it behaves, it also imports your "@string/"'s automatically in the XML, got fooled at first, thought i didn't hardcode that text.



It's nice to have the latest Android support in what probably will become my favorite IDE for Android! No more external linking to monitor.bat when they changed it in the SDK, etc.


Device selection forked, needs a bit more width, time to play around with it!

Download Android Studio v0.1 - HERE !

Presentation video:

onsdag 17 april 2013

Android : Managing shared libraries / resources with IntelliJ

A quick tutorial for getting you up and running with sharing libraries & resources across your Android projects with IntelliJ.

Lets start with an example, lets use https://github.com/SimonVT/android-calendarview. It's the new CalendarView which is backported for older Android versions.

Download and place it somewhere:
C:\Android\shared-libs\android-calendarview-master\

Now, rename the library directory inside the folder to something more descriptive, like 'calendarview'.
C:\Android\shared-libs\android-calendarview-master\calendarview

IntelliJ uses modules so we can't use the Eclipse structure.

Step 1: Import the Project.

Choose 'Import Project...' in IntelliJ.

Create project from existing sources. It should auto-detect it's an Android project, so click 'Next' until the project is opened. Next we need to make it a library.


Go to 'File -> Project Structure', choose 'Facets' and make sure 'Library Module' is ticked.

I'd recommened to create a README.TXT or the like if you need to alter your styles in the project we are going to use it in. 

Android 4.0 CalendarView backported to 2.2 Probably has to be built against API level 15

To use this library, it's required that the 1 attribute is added to your theme. 

<resources>

    <style name="SampleTheme" parent="@android:style/Theme">
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
    </style>

    <style name="SampleTheme.Light" parent="@android:style/Theme.Light">
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
    </style>
</resources>

Okay, the module is ready to imported and used.

Create a new Android application, after it's been created go to 'Project Structure'.
Choose 'Modules', click the green '+' icon and choose 'Import module', choose the .IML file.

Next switch to our own module, called 'TestApp', hit the right green '+'-icon and click on 'Module Dependency...' and choose 'calendarview'.

Now to see if everything works, create a themes.xml in /values

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme.Light" parent="@android:style/Theme.Light">
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
    </style>
</resources>

Be sure to add your custom theme to the activity in the AndroidManifest.xml.

Now we'll just add the custom view in our layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
    
    <net.simonvt.calendarview.CalendarView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            />
</LinearLayout>

The module works, ready to be used and re-used in other projects! When creating a module as a 'Library' you can skip the hassle with copying the correct resources, values, etc to your project.



fredag 8 februari 2013

Android : Fragments (a reminder)

Everyone is propably used to fragments by now, this is a quick reminder what methods to override for different purposes. Google has two helpful pages on fragments, the reference class page for Fragment, can be found here http://developer.android.com/reference/android/app/Fragment.html and their general for Fragments, http://developer.android.com/guide/components/fragments.html.

This will be a shortpacked for most methods i usually override as a reminder for myself!

Keyboard shortcuts to open the generate code dialog is  ALT + Insert for IntelliJ IDEA

ALT + Shift + S for Eclipse users.

Screenshot from IntelliJ IDEA.




src/MyFragment.java
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



/**
 *  MyFragment - Does Exactly Nothing! :)
 */
public class MyFragment extends Fragment {

    private IUpdate myListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // We have any listeners that need to communicate
        // with our activity, lets attach them here.
        myListener = (IUpdate) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // A custom xml view involved
        // Lets inflate it here!
        return inflater.inflate(R.layout.secret_layout, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Manage & find all the containers/widgets
        // in our layout.
        View myView = getView();
    }

    @Override
    public void onResume() {
        super.onResume();
        // We have something that might have changed while coming back
        // from another view? Settings ? Lets check & then update.
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // If our activity gets destroyed.
        // I.e we changed from portrait to landscape.
        // Anything we need to remember ?
        //
        // Remember, this do not get called just by hiding your view!
        // It's the activity it depends on.
    }
}

This should be methods i tend to override depending on what i'm doing when i'm creating a new Fragment.
A small skeleton fragment to keep me on track, hopefully it's helpful!

Now, family time!