Android編程之藍(lán)牙測(cè)試實(shí)例
本文實(shí)例講述了Android編程之藍(lán)牙測(cè)試。分享給大家供大家參考。具體分析如下:
一、軟件平臺(tái):
win7 + eclipse + sdk
二、設(shè)計(jì)思路:
配合倒計(jì)時(shí)定時(shí)器實(shí)現(xiàn)藍(lán)牙打開(kāi),可見(jiàn),掃描三個(gè)功能
三、源代碼:
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="開(kāi)啟可見(jiàn) " 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="設(shè)備不可見(jiàn) "></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);
//獲取藍(lán)牙適配器
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);
//判斷藍(lán)牙是否已經(jīng)被打開(kāi)
if (mBluetoothAdapter.isEnabled())
{
//打開(kāi)
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( "剩余可見(jiàn)時(shí)間" + millisUntilFinished / 1000 + "秒");
}
@Override
public void onFinish()
{
//判斷藍(lán)牙是否已經(jīng)被打開(kāi)
if (mBluetoothAdapter.isEnabled())
{
btn_see.setEnabled(true);
txt_see.setText( "設(shè)備不可見(jiàn)");
}
}
};
scan_timer = new CountDownTimer(12000,1000)
{
@Override
public void onTick( long millisUntilFinished)
{
txt_scan.setText( "剩余掃描時(shí)間" + millisUntilFinished / 1000 + "秒");
}
@Override
public void onFinish()
{
//判斷藍(lán)牙是否已經(jīng)被打開(kāi)
if (mBluetoothAdapter.isEnabled())
{
btn_scan.setEnabled(true);
//關(guān)閉掃描
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())
{
//打開(kāi)藍(lán)牙
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
{
//關(guān)閉藍(lán)牙
mBluetoothAdapter.disable();
btn_switch.setText("OFF");
mArrayAdapter.clear();
list.setAdapter(mArrayAdapter);
btn_see.setEnabled(false);
btn_scan.setEnabled(false);
}
break;
}
case R.id.button2:
{
//開(kāi)啟可見(jiàn)
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())
{
//開(kāi)始掃描
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
{
//關(guān)閉掃描
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");
//獲取藍(lán)牙列表
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();
}
}
}
}
效果圖如下:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android代碼實(shí)現(xiàn)新年賀卡動(dòng)畫(huà)示例詳解
這篇文章主要為大家介紹了Android代碼實(shí)現(xiàn)新年賀卡動(dòng)畫(huà)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android中EditText如何去除邊框添加下劃線(xiàn)
這篇文章主要介紹了Android中EditText如何去除邊框添加下劃線(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-02-02
解決RecycleView分割線(xiàn)不居中的三種方法
這篇文章主要為大家分享了解決RecycleView分割線(xiàn)不居中的三種方法,感興趣的小伙伴們可以參考一下2016-05-05
Kotlin協(xié)程之Flow基礎(chǔ)原理示例解析
這篇文章主要為大家介紹了Kotlin協(xié)程之Flow基礎(chǔ)原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android模擬實(shí)現(xiàn)支付寶螞蟻森林效果
這篇文章主要為大家詳細(xì)介紹了如何利用Android模擬實(shí)現(xiàn)支付寶中螞蟻森林的動(dòng)畫(huà)效果,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09
Android簡(jiǎn)單實(shí)用的可拖拽GridView組件分享
在我們?nèi)粘i_(kāi)發(fā)中,使用?GridView?這種網(wǎng)格視圖的場(chǎng)合還是不少的,本篇我們來(lái)介紹一個(gè)支持拖拽的?GridView?組件,可以輕松搞定網(wǎng)格視圖的拖拽排序,需要的可以參考一下2023-06-06
Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果
這篇文章主要介紹了Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果的方法,借助Fragment可以使Activity的內(nèi)部管理邏輯更加清晰,需要的朋友可以參考下2016-03-03

