lab 1. bluetooth scanner - installation tutorial

8
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 Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1... 1 of 8 5/27/2015 2:50 PM

Upload: dianz-nacghbt

Post on 12-Sep-2015

219 views

Category:

Documents


0 download

DESCRIPTION

tutorial

TRANSCRIPT

  • 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 applicationwill scan available Bluetooth Devices that already has been set to be discoverable. When this application is opened, it will turn on the Bluetooth feature

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    1 of 8 5/27/2015 2:50 PM

  • automatically..

    ObjectiveFrom this labware, students will learn how to implement discover fuction that in Android. Bluetooth API.

    Software Requirement

    Eclipse IDE

    Java JDK, JRE

    Android SDK

    TutorialCreate a new android project

    Project Name: BluetoothScannerTarget Name: Android 2.2.2Package Name: android.bluetoothscanner

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

  • android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1">

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

    Finally, add these code to BluetoothScannerActivity.java

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    3 of 8 5/27/2015 2:50 PM

  • package android.bluetoothscanner;

    import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;

    public class BluetoothScannerActivity extends Activity { //private final BroadcastReceiver FoundReceiver = null; private ArrayAdapter 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(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(); }

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    4 of 8 5/27/2015 2:50 PM

  • }); 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.

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    5 of 8 5/27/2015 2:50 PM

  • Then click the Scan Bluetooth Devices button. Make sure that the other devcie that you want to find was already in discoverable.

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    6 of 8 5/27/2015 2:50 PM

  • Sample QR(Use Barcode Scanner on your Android Phone to scan this image):

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    7 of 8 5/27/2015 2:50 PM

  • Manual Download: BluetoothScanner.apk

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

    Lab 1. Bluetooth Scanner - Installation tutorial https://sites.google.com/site/installationtutorial/computinglabware/module-5/2-lab-activity/lab1...

    8 of 8 5/27/2015 2:50 PM