The Bluetooth test example of Android programming _android

Source: Internet
Author: User
Tags gettext stub

This example describes the Bluetooth test for Android programming. Share to everyone for your reference. The specific analysis is as follows:

First, software platform:

Win7 + eclipse + SDK

Second, design ideas:

With the countdown timer to achieve Bluetooth open, visible, scanning three functions

Third, the source code:

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <textview android:id= "@+id/textview1" android:layout_width= "wrap_content" android:text= "TextView" Android: layout_height= "Wrap_content" ></TextView> <linearlayout android:layout_width= "Match_parent" Android: layout_height= "Wrap_content" android:id= "@+id/linearlayout1" > <button android:id= "@+id/button1" Android:text = "Off" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" ></Button> </ linearlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android : id= "@+id/linearlayout2" > <button android:id= @+id/button2 "android:text=" open Visible "android:layout_width=" wrap_ Content "android:layout_height=" wrap_content "></Button> <textview Android: id= "@+id/textview2" android:layout_width= wrap_content "android:layout_height=" wrap_content "android:text=" Device not visible "></TextView> </LinearLayout> <linearlayout android:layout_width=" Match_parent "Android:layout_ height= "Wrap_content" android:id= "@+id/linearlayout3" > <button android:id= "@+id/button3" android:text= "Scan: O FF "android:layout_width=" wrap_content "android:layout_height=" wrap_content "></Button> <textview Android:id= "@+id/textview3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = "Stop Scan" ></TextView> </LinearLayout> <listview android:id= "@+id/listview1" android:layout_height= "Wrap_content" android:layout_width= "match_parent" ></ListView> </LinearLayout>

Test_bluetooth.java:

Package Com.test_bluetooth; 
Import Java.util.Set; 
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.os.CountDownTimer; 
Import Android.view.View; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.Button; 
Import Android.widget.ListView; 
Import Android.widget.TextView; public class Test_bluetooth extends activity implements View.onclicklistener {private static final int request_enable_ 
 BT = 2; 
 TextView txt; 
 TextView Txt_see; 
 TextView Txt_scan; 
 Bluetoothadapter Mbluetoothadapter; 
 Arrayadapter<string> Marrayadapter; 
 Button Btn_switch; 
 Button Btn_see; 
 Button Btn_scan; 
 ListView list; 
 Countdowntimer See_timer; 
 Countdowntimer Scan_timer; /** called the activity is a. * * @OverrIDE public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.main); 
  txt = (TextView) Findviewbyid (R.ID.TEXTVIEW1); 
  Txt_see = (TextView) Findviewbyid (R.ID.TEXTVIEW2); 
  Txt_scan = (TextView) Findviewbyid (R.ID.TEXTVIEW3); 
  Bind the ListView in the XML as the container list = (ListView) Findviewbyid (r.id.listview1) of the item; 
  Get Bluetooth Adapter Mbluetoothadapter = Bluetoothadapter.getdefaultadapter (); Marrayadapter = new Arrayadapter<string> (this,android.
  R.layout.simple_list_item_1); 
   if (Mbluetoothadapter = = null) {//Device does not support Bluetooth txt.settext ("fail"); 
  Exit program Test_bluetooth.this.finish (); 
  } Btn_switch = (Button) Findviewbyid (R.id.button1); 
  Btn_switch.setonclicklistener (this); 
  Btn_see = (Button) Findviewbyid (R.id.button2); 
  Btn_see.setonclicklistener (this);  
  Btn_see.setenabled (FALSE); 
  Btn_scan = (Button) Findviewbyid (R.id.button3); 
  Btn_scan.setonclicklistener (this); Btn_scan.sEttext ("Scan: Off"); 
  Btn_scan.setenabled (FALSE); 
   Determine if Bluetooth has been opened if (mbluetoothadapter.isenabled ()) {//Open Btn_switch.settext ("on"); 
   Btn_see.setenabled (TRUE); 
  Btn_scan.setenabled (TRUE); 
   See_timer = new Countdowntimer (120000,1000) {@Override public void Ontick (long millisuntilfinished) 
   {Txt_see.settext ("remaining visible time" + millisuntilfinished/1000 + "seconds"); 
     @Override public void OnFinish () {//Determine if Bluetooth is already open if (mbluetoothadapter.isenabled ()) { 
     Btn_see.setenabled (TRUE); 
    Txt_see.settext ("Equipment not visible"); 
  } 
   } 
  }; 
    Scan_timer = new Countdowntimer (12000,1000) {@Override public void Ontick (long millisuntilfinished) { 
   Txt_scan.settext ("Remaining scan time" + millisuntilfinished/1000 + "seconds"); 
     @Override public void OnFinish () {//Determine if Bluetooth is already open if (mbluetoothadapter.isenabled ()) { 
     Btn_scan.setenabled (TRUE); Turn off scan Mbluetoothadapter.canceldisCovery (); 
     Btn_scan.settext ("Scan: Off"); 
    Txt_scan.settext ("Stop scanning"); 
 } 
   } 
  }; 
  } @Override protected void OnDestroy () {Super.ondestroy (); 
 Android.os.Process.killProcess (Android.os.Process.myPid ()); @Override public void OnClick (View v) {//TODO auto-generated a stub switch (V.getid ()) {case R 
    . Id.button1: {String str = Btn_switch.gettext (). toString (); if (str = "Off") {if (!mbluetoothadapter.isenabled ()) {//open bluetooth Intent enablebtintent = NE 
      W Intent (bluetoothadapter.action_request_enable); 
      Startactivityforresult (Enablebtintent, REQUEST_ENABLE_BT); 
      Txt.settext ("S1"); 
      Btn_see.setenabled (TRUE); 
      Btn_scan.settext ("Scan: Off"); 
     Btn_scan.setenabled (TRUE); 
     } else {//Turn off Bluetooth mbluetoothadapter.disable (); 
     Btn_switch.settext ("Off"); 
     Marrayadapter.clear (); 
     List.setadapter (Marrayadapter); Btn_see.setenabled (FALSE); 
    Btn_scan.setenabled (FALSE); 
   } break; Case R.id.button2: {//Open visible Intent enablebtintent_see = new Intent (bluetoothadapter.action_request_discove 
   rable); 
   Startactivityforresult (Enablebtintent_see, 3); 
   Txt.settext ("S1"); 
   Btn_see.setenabled (FALSE); 
   See_timer.start (); 
  Break 
   Case R.ID.BUTTON3: {String str = Btn_scan.gettext (). toString (); 
    if (str = = "Scan: Off") {Txt.settext ("S5"); 
     if (mbluetoothadapter.isenabled ()) {//Start scanning mbluetoothadapter.startdiscovery (); 
     Txt.settext ("S6"); 
     Btn_scan.settext ("Scan: On"); 
      Create a broadcastreceiver for action_found final broadcastreceiver mreceiver = new Broadcastreceiver () { @Override public void OnReceive (context context, Intent Intent) {//TODO auto-generated Metho 
       D stub String action = Intent.getaction (); When Discovery finds a device marrayadapter.clear ();
       if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) {//Get the Bluetoothdevice object from the Int 
        ENT bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device); ADD the name and address to a ListView Marrayadapter.add (device.getname () + "adapter" + 
       Device.getaddress ()); 
      } list.setadapter (Marrayadapter); 
     } 
     }; 
     Register the broadcastreceiver intentfilter filter = new Intentfilter (bluetoothdevice.action_found); Registerreceiver (mreceiver, filter); 
    Don ' t forget to unregister during OnDestroy Scan_timer.start (); 
    } else {//close scan mbluetoothadapter.canceldiscovery (); 
    Btn_scan.settext ("Scan: Off"); 
    Scan_timer.cancel (); 
   Txt_scan.settext ("Stop scanning"); 
  } break; 
  } Default:break; } public void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (requestcode{Case REQUEST_ENABLE_BT://When the REQUEST to ENABLE Bluetooth returns if (ResultCode = = Activity.resul 
    T_OK) {//Bluetooth is-enabled, so set up a chat session btn_switch.settext (' on '); 
    Txt.settext ("S4"); 
    Get bluetooth list set<bluetoothdevice> paireddevices = Mbluetoothadapter.getbondeddevices (); 
    Marrayadapter.clear (); 
     If there are paired devices if (Paireddevices.size () > 0) {//txt.settext ("S3"); Loop through paired devices for (Bluetoothdevice device:paireddevices) {//ADD the name and Addre 
     SS to the array adapter to show in a ListView Marrayadapter.add (device.getname () + ":" + device.getaddress ()); 
     } list.setadapter (Marrayadapter); 
   } else {finish (); } 
  } 
 } 
}

The effect chart is as follows:

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.