Android开发教程:蓝牙测试

长平狐 发布于 2012/09/03 17:24
阅读 393
收藏 0

软件平台:Windows 7 + Eclipse + SDK

设计思路:

配合倒计时定时器实现蓝牙打开,可见,扫描三个功能

源代码:

main.xml:

  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:orientation="vertical">  
  5.     <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content">TextView>  
  6.     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1">  
  7.         <Button android:id="@+id/button1" android:text="OFF" android:layout_width="wrap_content" android:layout_height="wrap_content">Button>  
  8.     LinearLayout>  
  9.     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">  
  10.         <Button android:id="@+id/button2" android:text="开启可见 " android:layout_width="wrap_content" android:layout_height="wrap_content">Button>  
  11.         <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设备不可见 ">TextView>  
  12.     LinearLayout>  
  13.     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3">  
  14.         <Button android:id="@+id/button3" android:text="扫描:OFF" android:layout_width="wrap_content" android:layout_height="wrap_content">Button>  
  15.         <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止扫描 ">TextView>  
  16.     LinearLayout>  
  17.     <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent">ListView>  
  18. LinearLayout>  

test_bluetooth.java:

  1. package com.test_bluetooth;  
  2.   
  3. import java.util.Set;  
  4.   
  5. import Android.app.Activity;  
  6. import android.bluetooth.BluetoothAdapter;  
  7. import android.bluetooth.BluetoothDevice;  
  8. import android.content.BroadcastReceiver;  
  9. import android.content.Context;  
  10. import android.content.Intent;  
  11. import android.content.IntentFilter;  
  12. import android.os.Bundle;  
  13. import android.os.CountDownTimer;  
  14. import android.view.View;  
  15. import android.widget.ArrayAdapter;  
  16. import android.widget.Button;  
  17. import android.widget.ListView;  
  18. import android.widget.TextView;  
  19.   
  20. public class test_bluetooth extends Activity implements View.OnClickListener  
  21. {  
  22.     private static final int REQUEST_ENABLE_BT = 2;  
  23.     TextView txt;  
  24.     TextView txt_see;  
  25.     TextView txt_scan;  
  26.     BluetoothAdapter mBluetoothAdapter;  
  27.     ArrayAdapter mArrayAdapter;  
  28.     Button btn_switch;  
  29.     Button btn_see;  
  30.     Button btn_scan;  
  31.     ListView list;  
  32.     CountDownTimer see_timer;  
  33.     CountDownTimer scan_timer;  
  34.       
  35.     /** Called when the activity is first created. */  
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState)   
  38.     {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.main);  
  41.           
  42.         txt = (TextView)findViewById(R.id.textView1);  
  43.         txt_see = (TextView)findViewById(R.id.textView2);  
  44.         txt_scan = (TextView)findViewById(R.id.textView3);  
  45.         //绑定XML中的ListView,作为Item的容器     
  46.         list = (ListView) findViewById(R.id.listView1);    
  47.           
  48.         //获取蓝牙适配器   
  49.         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  50.         mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);  
  51.         if (mBluetoothAdapter == null)   
  52.         {  
  53.             // Device does not support Bluetooth   
  54.             txt.setText("fail");  
  55.             //退出程序   
  56.             test_bluetooth.this.finish();  
  57.         }  
  58.           
  59.         btn_switch = (Button)findViewById(R.id.button1);  
  60.         btn_switch.setOnClickListener(this);  
  61.         btn_see = (Button)findViewById(R.id.button2);  
  62.         btn_see.setOnClickListener(this);  
  63.         btn_see.setEnabled(false);     
  64.         btn_scan = (Button)findViewById(R.id.button3);  
  65.         btn_scan.setOnClickListener(this);  
  66.         btn_scan.setText("扫描:OFF");  
  67.         btn_scan.setEnabled(false);    
  68.           
  69.         //判断蓝牙是否已经被打开   
  70.         if (mBluetoothAdapter.isEnabled())  
  71.         {  
  72.             //打开   
  73.             btn_switch.setText("ON");  
  74.             btn_see.setEnabled(true);    
  75.             btn_scan.setEnabled(true);  
  76.         }  
  77.   
  78.         see_timer = new CountDownTimer(120000,1000)   
  79.         {  
  80.             @Override  
  81.             public void onTick( long millisUntilFinished)   
  82.             {  
  83.                 txt_see.setText( "剩余可见时间" + millisUntilFinished / 1000 + "秒");  
  84.             }            
  85.             @Override  
  86.             public void onFinish()   
  87.             {  
  88.                 //判断蓝牙是否已经被打开   
  89.                 if (mBluetoothAdapter.isEnabled())  
  90.                 {  
  91.                     btn_see.setEnabled(true);  
  92.                     txt_see.setText( "设备不可见");  
  93.                 }  
  94.             }  
  95.         };  
  96.         scan_timer = new CountDownTimer(12000,1000)   
  97.         {  
  98.             @Override  
  99.             public void onTick( long millisUntilFinished)   
  100.             {  
  101.                 txt_scan.setText( "剩余扫描时间" + millisUntilFinished /
加入 1000  +  "秒" );  
  •             }            
  •             @Override  
  •             public void onFinish()   
  •             {  
  •                 //判断蓝牙是否已经被打开   
  •                 if (mBluetoothAdapter.isEnabled())  
  •                 {  
  •                     btn_scan.setEnabled(true);  
  •                     //关闭扫描   
  •                     mBluetoothAdapter.cancelDiscovery();  
  •                     btn_scan.setText("扫描:OFF");  
  •                     txt_scan.setText( "停止扫描");  
  •                 }  
  •             }  
  •         };  
  •     }  
  •       
  •     @Override    
  •     protected void onDestroy() {    
  •         super.onDestroy();    
  •         android.os.Process.killProcess(android.os.Process.myPid());    
  •     }    
  •       
  •     @Override  
  •     public void onClick(View v)   
  •     {  
  •         // TODO Auto-generated method stub   
  •         switch (v.getId())  
  •         {  
  •         case R.id.button1:  
  •             {  
  •                 String str = btn_switch.getText().toString();  
  •                 if (str == "OFF")  
  •                 {  
  •                     if (!mBluetoothAdapter.isEnabled())   
  •                     {  
  •                         //打开蓝牙   
  •                         Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  •                         startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  •                         txt.setText("s1");  
  •                         btn_see.setEnabled(true);    
  •                         btn_scan.setText("扫描:OFF");  
  •                         btn_scan.setEnabled(true);  
  •                     }  
  •                 }  
  •                 else  
  •                 {  
  •                     //关闭蓝牙   
  •                     mBluetoothAdapter.disable();  
  •                     btn_switch.setText("OFF");  
  •                     mArrayAdapter.clear();  
  •                     list.setAdapter(mArrayAdapter);  
  •                     btn_see.setEnabled(false);    
  •                     btn_scan.setEnabled(false);  
  •                 }  
  •                   
  •                 break;  
  •             }  
  •         case R.id.button2:  
  •         {  
  •             //开启可见   
  •             Intent enableBtIntent_See = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  •             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 == "扫描:OFF")  
  •             {  
  •                 txt.setText("s5");  
  •                 if (mBluetoothAdapter.isEnabled())   
  •                 {  
  •                     //开始扫描   
  •                     mBluetoothAdapter.startDiscovery();  
  •                     txt.setText("s6");  
  •                     btn_scan.setText("扫描:ON");  
  •                       
  •                     // Create a BroadcastReceiver for ACTION_FOUND   
  •                     final BroadcastReceiver mReceiver = new BroadcastReceiver()   
  •                     {  
  •                         @Override  
  •                         public void onReceive(Context context, Intent intent)   
  •                         {  
  •                             // TODO Auto-generated method stub   
  •                             String action = intent.getAction();  
  •                             // When discovery finds a device   
  •                             mArrayAdapter.clear();  
  •                             if (BluetoothDevice.ACTION_FOUND.equals(action))   
  •                             {  
  •                                 // Get the BluetoothDevice object from the Intent   
  •                                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  •                                 // Add the name and address to an array adapter to show in a ListView   
  •                                 mArrayAdapter.add(device.getName() + ":" + 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  
  •             {  
  •                 //关闭扫描   
  •                 mBluetoothAdapter.cancelDiscovery();  
  •                 btn_scan.setText("扫描:OFF");  
  •                 scan_timer.cancel();  
  •                 txt_scan.setText( "停止扫描");  
  •             }  
  •               
  •             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.RESULT_OK)   
  •             {    
  •                 // Bluetooth is now enabled, so set up a chat session     
  •                 btn_switch.setText("ON");  
  •                 txt.setText("s4");  
  •                   
  •                 //获取蓝牙列表   
  •                 Set 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 address to an array adapter to show in a ListView   
  •                         mArrayAdapter.add(device.getName() + ":" + device.getAddress());  
  •                     }  
  •                     list.setAdapter(mArrayAdapter);  
  •                  }  
  •             } else   
  •             {    
  •                 finish();    
  •             }    
  •         }    
  •     }    
  • }  
  • 效果图:

     




    原文链接:http://blog.csdn.net/tody_guo/article/details/6875180
    加载中
    返回顶部
    顶部