亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Android編程9:藍牙測試

系統 2163 0

Android編程9:藍牙測試


本文博客鏈接: http://blog.csdn.net/jdh99 ,作者:jdh,轉載請注明.


軟件平臺:win7 + eclipse + sdk


設計思路:

配合倒計時定時器實現藍牙打開,可見,掃描三個功能


參考鏈接:

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


源代碼:

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="開啟可見 " 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="設備不可見 "></TextView>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3">
        <Button android:id="@+id/button3" android:text="掃描:OFF" 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="停止掃描 "></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 when the activity is first created. */
    @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);
        //綁定XML中的ListView,作為Item的容器  
        list = (ListView) findViewById(R.id.listView1);  
        
        //獲取藍牙適配器
        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");
        	//退出程序
    		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("掃描:OFF");
        btn_scan.setEnabled(false);  
        
        //判斷藍牙是否已經被打開
        if (mBluetoothAdapter.isEnabled())
        {
        	//打開
        	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( "剩余可見時間" + millisUntilFinished / 1000 + "秒");
        	}          
        	@Override
        	public void onFinish() 
        	{
        		//判斷藍牙是否已經被打開
                if (mBluetoothAdapter.isEnabled())
                {
                	btn_see.setEnabled(true);
                	txt_see.setText( "設備不可見");
                }
        	}
        };
        scan_timer = new CountDownTimer(12000,1000) 
        {
        	@Override
        	public void onTick( long millisUntilFinished) 
        	{
        		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<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 address to an array adapter to show in a ListView
                		mArrayAdapter.add(device.getName() + ":" + device.getAddress());
                	}
                	list.setAdapter(mArrayAdapter);
                 }
            } else 
            {  
                finish();  
            }  
        }  
    }  
}
  
效果圖:

Android編程9:藍牙測試

Android編程9:藍牙測試


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 2021最新国产成人精品免费 | 久久婷婷人人澡人人爱91 | 人人爽天天爽夜夜爽qc | 精品国产第一国产综合精品 | 鲁丝一区二区三区不属 | 国产男女性特黄录像 | 欧美日韩亚洲精品一区 | 天天想天天干 | 中文字幕免费在线视频 | 亚洲一区二区三区精品视频 | 亚洲日本va中文字幕在线不卡 | 国产区综合另类亚洲欧美 | 国产精品福利尤物youwu | 99re6在线视频免费精品 | 欧美日韩中文字幕在线观看 | 国产精品高清免费网站 | 久久99热这里只有精品免费看 | 奇米第八色 | 99视频精品免费99在线 | 久久久久久午夜精品 | 久久精品三级视频 | 久久成人免费视频 | 欧美在线视频一区二区 | 五月婷婷免费视频 | 9久9久女女热精品视频免费观看 | 日本精品在线 | 高清国产一区二区 | 在线观看深夜观看网站免费 | 免费福利影院 | 91免费国产在线观看尤物 | 久久婷婷综合中文字幕 | 国产在线免 | 五月婷婷久久综合 | 亚欧精品一区二区三区 | 国产精品久久二区三区色裕 | 免费一级毛片在线视频观看 | 国产香蕉网| 日本精品久久久久中文字幕8 | 精品久久久久久久一区二区手机版 | er久99久热只有精品国产 | 国产精品久久久久久久久久一区 |