歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發教程:藍牙測試

軟件平台: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<String> 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<String>(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 + "秒");  
  102.             }            
  103.             @Override  
  104.             public void onFinish()   
  105.             {  
  106.                 //判斷藍牙是否已經被打開   
  107.                 if (mBluetoothAdapter.isEnabled())  
  108.                 {  
  109.                     btn_scan.setEnabled(true);  
  110.                     //關閉掃描   
  111.                     mBluetoothAdapter.cancelDiscovery();  
  112.                     btn_scan.setText("掃描:OFF");  
  113.                     txt_scan.setText( "停止掃描");  
  114.                 }  
  115.             }  
  116.         };  
  117.     }  
  118.       
  119.     @Override    
  120.     protected void onDestroy() {    
  121.         super.onDestroy();    
  122.         android.os.Process.killProcess(android.os.Process.myPid());    
  123.     }    
  124.       
  125.     @Override  
  126.     public void onClick(View v)   
  127.     {  
  128.         // TODO Auto-generated method stub   
  129.         switch (v.getId())  
  130.         {  
  131.         case R.id.button1:  
  132.             {  
  133.                 String str = btn_switch.getText().toString();  
  134.                 if (str == "OFF")  
  135.                 {  
  136.                     if (!mBluetoothAdapter.isEnabled())   
  137.                     {  
  138.                         //打開藍牙   
  139.                         Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  140.                         startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  141.                         txt.setText("s1");  
  142.                         btn_see.setEnabled(true);    
  143.                         btn_scan.setText("掃描:OFF");  
  144.                         btn_scan.setEnabled(true);  
  145.                     }  
  146.                 }  
  147.                 else  
  148.                 {  
  149.                     //關閉藍牙   
  150.                     mBluetoothAdapter.disable();  
  151.                     btn_switch.setText("OFF");  
  152.                     mArrayAdapter.clear();  
  153.                     list.setAdapter(mArrayAdapter);  
  154.                     btn_see.setEnabled(false);    
  155.                     btn_scan.setEnabled(false);  
  156.                 }  
  157.                   
  158.                 break;  
  159.             }  
  160.         case R.id.button2:  
  161.         {  
  162.             //開啟可見   
  163.             Intent enableBtIntent_See = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  164.             startActivityForResult(enableBtIntent_See, 3);  
  165.             txt.setText("s1");  
  166.             btn_see.setEnabled(false);   
  167.             see_timer.start();  
  168.               
  169.             break;  
  170.         }  
  171.         case R.id.button3:  
  172.         {  
  173.             String str = btn_scan.getText().toString();  
  174.             if (str == "掃描:OFF")  
  175.             {  
  176.                 txt.setText("s5");  
  177.                 if (mBluetoothAdapter.isEnabled())   
  178.                 {  
  179.                     //開始掃描   
  180.                     mBluetoothAdapter.startDiscovery();  
  181.                     txt.setText("s6");  
  182.                     btn_scan.setText("掃描:ON");  
  183.                       
  184.                     // Create a BroadcastReceiver for ACTION_FOUND   
  185.                     final BroadcastReceiver mReceiver = new BroadcastReceiver()   
  186.                     {  
  187.                         @Override  
  188.                         public void onReceive(Context context, Intent intent)   
  189.                         {  
  190.                             // TODO Auto-generated method stub   
  191.                             String action = intent.getAction();  
  192.                             // When discovery finds a device   
  193.                             mArrayAdapter.clear();  
  194.                             if (BluetoothDevice.ACTION_FOUND.equals(action))   
  195.                             {  
  196.                                 // Get the BluetoothDevice object from the Intent   
  197.                                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  198.                                 // Add the name and address to an array adapter to show in a ListView   
  199.                                 mArrayAdapter.add(device.getName() + ":" + device.getAddress());  
  200.                             }  
  201.                             list.setAdapter(mArrayAdapter);  
  202.                         }  
  203.                     };  
  204.                     // Register the BroadcastReceiver   
  205.                     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  206.                     registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy   
  207.                       
  208.                     scan_timer.start();  
  209.                 }  
  210.             }  
  211.             else  
  212.             {  
  213.                 //關閉掃描   
  214.                 mBluetoothAdapter.cancelDiscovery();  
  215.                 btn_scan.setText("掃描:OFF");  
  216.                 scan_timer.cancel();  
  217.                 txt_scan.setText( "停止掃描");  
  218.             }  
  219.               
  220.             break;  
  221.         }  
  222.         default:  
  223.             break;  
  224.         }  
  225.     }  
  226.       
  227.     public void onActivityResult(int requestCode, int resultCode, Intent data)   
  228.     {    
  229.         switch (requestCode)   
  230.         {    
  231.         case REQUEST_ENABLE_BT:    
  232.             // When the request to enable Bluetooth returns     
  233.             if (resultCode == Activity.RESULT_OK)   
  234.             {    
  235.                 // Bluetooth is now enabled, so set up a chat session     
  236.                 btn_switch.setText("ON");  
  237.                 txt.setText("s4");  
  238.                   
  239.                 //獲取藍牙列表   
  240.                 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();  
  241.                 mArrayAdapter.clear();  
  242.                 // If there are paired devices   
  243.                 if (pairedDevices.size() > 0)   
  244.                 {  
  245.                     //txt.setText("s3");   
  246.                       
  247.                     // Loop through paired devices   
  248.                     for (BluetoothDevice device : pairedDevices)   
  249.                     {  
  250.                         // Add the name and address to an array adapter to show in a ListView   
  251.                         mArrayAdapter.add(device.getName() + ":" + device.getAddress());  
  252.                     }  
  253.                     list.setAdapter(mArrayAdapter);  
  254.                  }  
  255.             } else   
  256.             {    
  257.                 finish();    
  258.             }    
  259.         }    
  260.     }    
  261. }  

效果圖:

Copyright © Linux教程網 All Rights Reserved