Android编程之蓝牙测试实例
程序员文章站
2022-03-23 14:05:22
本文实例讲述了android编程之蓝牙测试。分享给大家供大家参考。具体分析如下:
一、软件平台:
win7 + eclipse + sdk
二、设计思路:
配合倒计...
本文实例讲述了android编程之蓝牙测试。分享给大家供大家参考。具体分析如下:
一、软件平台:
win7 + eclipse + sdk
二、设计思路:
配合倒计时定时器实现蓝牙打开,可见,扫描三个功能
三、源代码:
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程序设计有所帮助。
上一篇: 支付宝推出学费码 可以按教学进度支付
下一篇: CDR图片怎么添加书页卷边效果?
推荐阅读
-
Android编程之线性布局LinearLayout实例简析
-
Android编程之监听器用法实例分析
-
Android编程之九宫格实现方法实例分析
-
Android编程之控件状态配置文件实例
-
Android编程之杀毒的实现原理及具体实例
-
Android编程之自定义AlertDialog(退出提示框)用法实例
-
Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解
-
Android编程之TabWidget选项卡用法实例分析
-
Android编程之线性布局LinearLayout实例简析
-
Android编程之基于Log演示一个activity生命周期实例详解