Android编程之蓝牙测试实例
作者:jdh99 发布时间:2022-06-09 13:59:53
标签:Android,蓝牙
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 本文实例讲述了Java实现的求逆矩阵算法。分享给大家供大家参考,具体如下:package demo;public class MatrixI
- 本文实例讲述了Android编程判断是否连接网络的方法。分享给大家供大家参考,具体如下:判断wifi网络是否链接:public static
- 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....程序设计:public class exp2{ publi
- 效果视频引用描述本示例采用的是非常、非常、非常好用的一款第三方SDK——helloCharts传送门导包第一步 :导入mavenmaven
- 最近再开发中遇到需要将文件上传到Linux服务器上,至此整理代码笔记。此种连接方法中有考虑到并发问题,在进行创建FTP连接的时候
- jwt介绍: JWT是一种用于双方之间传递安全信息的简洁的、URL安全的表述性声明规范
- Java 开发语言中实现HTTP请求的方法主要有两种:一种是JAVA的标准类HttpUrlConnection,比较原生的实现方法;另一种是
- 文件格式分别如下 package txtobject ReadTxt {def readFromTxtBy
- 前言本文给你提供在Spring Boot 应用程序中编写好的单元测试的机制,并且深入技术细节。我们将带你学习如何以可测试的方式创建Sprin
- 之前的一篇博客中,讲的是用栈实现了中缀表达式的简易计算器,对于我们人来讲,中缀表达式是一种比较直观,而且非常好计算的一种形式,但对于计算器来
- 在开发中,我们通常需要将从数据库中查询的集合数据转换成类似文件系统一样的树形集合,比如:省市单位,部门机构,书籍分类等TreeNode对象@
- 前言JavaEE(Java Enterprise Edition),Java企业版,是一个用于企业级web开发平台。最早由Sun公司定制并发
- 最近几天一直在看Hadoop相关的书籍,目前稍微有点感觉,自己就仿照着WordCount程序自己编写了一个统计关联商品。需求描述:根据超市的
- 前言Spring框架对Bean进行装配提供了很灵活的方式,下面归纳一下主要的方式:在XML中进行显示配置在Java中进行显示配置隐式的bea
- 本例子演示如何添加一个简单的单页导航,在此基础上,再演示如何在第2个页面中显示第1个页面中拨打过的所有电话号码。(1)通过该例子理解Andr
- 前言众所周知在spring boot内,设置session过期时间只需在application.properties内添加server.se
- 一、编辑框EditText编辑框用于接收键盘输入的文字,由文本视图派生而来,除了TextView已有的各种属性和方法,EditText还支持
- SpringBoot的主要目的是简化配置文件,通过少量配置即可运行Java程序,其强大的自动配置功能帮助开发者轻松实现配置装配,通过引入Sp
- Caffeine和Spring Boot集成Caffeine是使用Java8对Guava缓存的重写版本,在Spring Boot 2.0中将
- retrofit post请求上传表单和文件最重要的就是就是去构建RequestBody,今天为大家带来一种超级简单的上传方式Body方式: