zoukankan      html  css  js  c++  java
  • Android编程9:蓝牙测试

    转自http://blog.csdn.net/jdh99/article/details/6871070

    Android编程9:蓝牙测试


    本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


    软件平台:win7 + eclipse + sdk


    设计思路:

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


    参考链接:

    http://blog.csdn.net/pwei007/article/details/6015907


    源代码:

    main.xml:

    [html] view plaincopy
    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:

    [java] view plaincopy
    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. }  
    效果图:


  • 相关阅读:
    关于获取路径
    今天最好的生日礼物就是重新找到目标
    Fedora与Ubuntu安装g++的命令
    CMPXCHG8B 比较并交换 8 字节
    关于 WIN32_LEAN_AND_MEAN
    i386和i686
    Intrinsic function
    VC9: LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
    Linux内核中的Min和Max函数
    linux重定向命令应用及语法
  • 原文地址:https://www.cnblogs.com/walccott/p/4957585.html
Copyright © 2011-2022 走看看