热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android编程之蓝牙测试实例

这篇文章主要介绍了Android编程之蓝牙测试,较为详细的分析了Android蓝牙测试的相关运行环境与调试技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了Android编程之蓝牙测试。分享给大家供大家参考。具体分析如下:

一、软件平台:

win7 + eclipse + sdk

二、设计思路:

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

三、源代码:

main.xml:

<&#63;xml version="1.0" encoding="utf-8"&#63;> 

  
  
   
  
  
   
   
  
  
   
   
  
  

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 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(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 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程序设计有所帮助。


推荐阅读
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文是关于自学Android的笔记,包括查看类的源码的方法,活动注册的必要性以及布局练习的重要性。通过学习本文,读者可以了解到在自学Android过程中的一些关键点和注意事项。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文介绍了一种图片处理应用,通过固定容器来实现缩略图的功能。该方法可以实现等比例缩略、扩容填充和裁剪等操作。详细的实现步骤和代码示例在正文中给出。 ... [详细]
  • Hibernate延迟加载深入分析-集合属性的延迟加载策略
    本文深入分析了Hibernate延迟加载的机制,特别是集合属性的延迟加载策略。通过延迟加载,可以降低系统的内存开销,提高Hibernate的运行性能。对于集合属性,推荐使用延迟加载策略,即在系统需要使用集合属性时才从数据库装载关联的数据,避免一次加载所有集合属性导致性能下降。 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
  • 本文介绍了在Ubuntu 11.10 x64环境下安装Android开发环境的步骤,并提供了解决常见问题的方法。其中包括安装Eclipse的ADT插件、解决缺少GEF插件的问题以及解决无法找到'userdata.img'文件的问题。此外,还提供了相关插件和系统镜像的下载链接。 ... [详细]
  • 项目运行环境配置及可行性分析
    本文介绍了项目运行环境配置的要求,包括Jdk1.8、Tomcat7.0、Mysql、HBuilderX等工具的使用。同时对项目的技术可行性、操作可行性、经济可行性、时间可行性和法律可行性进行了分析。通过对数据库的设计和功能模块的设计,确保系统的完整性和安全性。在系统登录、系统功能模块、管理员功能模块等方面进行了详细的介绍和展示。最后提供了JAVA毕设帮助、指导、源码分享和调试部署的服务。 ... [详细]
  • 本文介绍了常用的编辑器快捷键,包括快速转换编辑器、浏览选项卡、提取本地变量和方法、编辑器窗口最大化等功能。通过使用这些快捷键,可以提高编辑器的使用效率,减少复杂度,并提升代码的可测试性。 ... [详细]
author-avatar
妞妞盼寒假_197
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有