You are on page 1of 8

Lab 1.

Bluetooth Scanner - Installation tutorial

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 >

Lab 1. Bluetooth Scanner

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

Lab 1. Bluetooth Scanner - Installation tutorial

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

Java JDK, JRE

Android SDK

Tutorial
Create a new android project
Project Name: BluetoothScanner
Target Name: Android 2.2.2
Package Name: android.bluetoothscanner

Copy and paste following code to the main.xml file.


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

5/27/2015 2:50 PM

Lab 1. Bluetooth Scanner - Installation tutorial

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>

Go to BluetoothScanner->AndroidManifest.xml, copy and paste following code to it.

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.bluetoothscanner"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BluetoothScannerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Finally, add these code to BluetoothScannerActivity.java

5/27/2015 2:50 PM

Lab 1. Bluetooth Scanner - Installation tutorial

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;

public class BluetoothScannerActivity extends Activity {


//private final BroadcastReceiver FoundReceiver = null;
private ArrayAdapter<String> btArrayAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
final Button scanb = (Button)findViewById(R.id.button);
final ListView Deviceslist = (ListView)findViewById(R.id.listView1);
btArrayAdapter = new ArrayAdapter<String>(BluetoothScannerActivity.this,
android.R.layout.simple_list_item_1);
Deviceslist.setAdapter(btArrayAdapter);
//Turn on Bluetooth
if (myBlueToothAdapter==null)
Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth",
Toast.LENGTH_LONG).show();
else if (!myBlueToothAdapter.isEnabled()) {
Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(BtIntent, 0);
Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show();
}
//scan
scanb.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
btArrayAdapter.clear();
myBlueToothAdapter.startDiscovery();
Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show();
}

5/27/2015 2:50 PM

Lab 1. Bluetooth Scanner - Installation tutorial

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

Lab 1. Bluetooth Scanner - Installation tutorial

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

Lab 1. Bluetooth Scanner - Installation tutorial

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

Lab 1. Bluetooth Scanner - Installation tutorial

8 of 8

https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

Manual Download: BluetoothScanner.apk

Sign in | Recent Site Activity | Report Abuse | Print Page | Powered By Google Sites

5/27/2015 2:50 PM

You might also like