Go Down

Topic: Creating Android BLE app (Read 1 time) previous topic - next topic

kathyelecfreaks

If you are an electronics enthusiast, and also fond of programming, I would be very glad to share with you an Android BLE application development method, so you can integrate the phone and MCU to do some more interesting things. Right now we begin to explain how to develop an Android BLE application, and you can also refer to the official Google tutorial. This guide is a packed tutorial, which enables you to build your Android BLE application more easily and more quickly. Of course, I'll upload my source code for everyone to share it and you can also refer to the official sample sdk/samples/android-18/legacy/BluetoothLeGatt.

1. Create a new Android project
Open Eclipse,File->New->Android Application Project,and then fill the Application name in the Application Name edit box, for example, BleExample,or others. Minimum Required SDK selects API18:Android 4.3,and Target SDK also selects API18:Android 4.3,as buletooth 4.0 must be with Android 4.3edition or above. Others default unchanged and please continue clicking the Next button until Finish button appears,and then click the Finish button.

2. Add the permissions and services
Add the below code in the manifest file:
Code: [Select]
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<service android:name="com.elecfreaks.ble.BluetoothLeService" android:enabled="true"/>


3. Create the ListView item layout file
Aiming to display each content of ListView, here we use customization(defining by yourself), so that each ListView can show more content,item_list.xml is demonstrated as below:
Code: [Select]
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   
    >
    <TextView android:id="@+id/textViewDevName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24dp"/>
    <TextView android:id="@+id/textViewDevAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12dp"/>
</LinearLayout>

Copy the source code of BleExample /com.elecfreaks.ble to your project src directory,and then open the file with error prompt, pressing the shift+ctrl+O keys.

4. Modify activity_main.xml, increasing scanButton and bleDeviceListView
Increased contents are shown as below:
Code: [Select]
<Button
        android:id="@+id/scanButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="scanOnClick"
        android:text="scan" />

    <ListView
        android:id="@+id/bleDeviceListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/scanButton"
        android:layout_below="@+id/scanButton"
        android:layout_above="@+id/sendButton"
         >
    </ListView>


5. In MainActivity.java, add scanButton mothod of responding to events
Code: [Select]
(onClick="scanOnClick")
public void scanOnClick(final View v){

}


Complete instructions and more details can be found at www.elecfreaks.com/7906.html

Go Up