1
Project logs / Creating Android BLE app
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"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.
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>
4. Modify activity_main.xml, increasing scanButton and bleDeviceListView
Increased contents are shown as below:
Code: [Select]
<Button5. In MainActivity.java, add scanButton mothod of responding to events
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>
Code: [Select]
public void scanOnClick(final View v){
}6. Add member for MainActivity
Code: [Select]
private Button scanButton;
private ListView bleDeviceListView;
private BLEDeviceListAdapter listViewAdapter;
private BluetoothHandler bluetoothHandler;
private boolean isConnected;
Complete instructions and more details can be found at: http://www.elecfreaks.com/7906.html



