Professional Documents
Culture Documents
1 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
Installation tutorial
Android Mobile Computing Labware > Module 5. Network > 2. Lab Activity > Lab1: Application layer > 2. Bluetooth >
In this tutorial, we will learn to build a Bluetooth Scanner application. There is only one button on the screen. When students click the button, the application
will scan available Bluetooth Devices that already has been set to be discoverable. When this application is opened, it will turn on the Bluetooth feature
5/27/2015 2:50 PM
2 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
automatically..
Objective
From this labware, students will learn how to implement discover fuction that in Android. Bluetooth API.
Software Requirement
Eclipse IDE
Android SDK
Tutorial
Create a new android project
Project Name: BluetoothScanner
Target Name: Android 2.2.2
Package Name: android.bluetoothscanner
5/27/2015 2:50 PM
3 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView android:id="@+id/textView1" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:text="Scan
available Bluetooth Devices"></TextView>
<Button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="Scan Bluetooth Devices" android:layout_weight="0.05"></Button>
<ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent">
</ListView>
</LinearLayout>
</ScrollView>
5/27/2015 2:50 PM
4 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
package android.bluetoothscanner;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.content.BroadcastReceiver;
android.content.Context;
android.content.Intent;
android.content.IntentFilter;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.ListView;
android.widget.Toast;
5/27/2015 2:50 PM
5 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
});
registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(FoundReceiver);
}
private final BroadcastReceiver FoundReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}};
Save and run the project, you must run this appliciation by a real device. (Emulator doesn't support bluetooth)
When open the app by real device, it will be like this. Android device system will ask you if give permission to the application. click Yes.
5/27/2015 2:50 PM
6 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
Then click the Scan Bluetooth Devices button. Make sure that the other devcie that you want to find was already in discoverable.
5/27/2015 2:50 PM
7 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
Sample QR(Use Barcode Scanner on your Android Phone to scan this image):
5/27/2015 2:50 PM
8 of 8
https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...
Sign in | Recent Site Activity | Report Abuse | Print Page | Powered By Google Sites
5/27/2015 2:50 PM