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


猜你喜欢
- mapper.xml中if标签test判断的用法1. 字符串等于条件的两种写法① 将双引号和单引号的位置互换<if test='
- Android从网络中获得一张图片并显示在屏幕上的实例详解看下实现效果图:1:androidmanifest.xml的内容<?xml
- 我们都知道Android Studio用起来很棒,其中布局预览更棒。我们在调UI的时候基本是需要实时预览来看效果的,在Android Stu
- 目前为止我们已经了解了如何通过编程创建 CompletableFuture 对象以及如何获取返回值,虽然看起来这些操作已经比较方便,但还有进
- 展示图: 对接的完整流程如下首先是配置gzh.appid=公众号appidwxPay.mchId=商户号wxPay.key=支付密
- 相比于直线检测,直线拟合的最大特点是将所有数据只拟合出一条直线void fitLine( InputArray points, Output
- 1、右值1.1 简介首先区分一下左右值:左值是指存储在内存中、有明确存储地址(可取地址)的数据;右值是指可以提供数据值的数据(不可取地址)如
- centos下搭建GitLab+Jenkins持续集成环境,供大家参考,具体内容如下1、安装JDKyum install -y java2、
- 算法的主题思想:1.优秀的算法因为能够解决实际问题而变得更为重要;2.高效算法的代码也可以很简单;3.理解某个实现的性能特点是一个挑战;4.
- 1.概览该教程中,我将向你展示:如何在测试时设置spring boot 日志级别。虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测
- 1. 异常1.1 try…catch异常处理try catch的异常处理的格式写法 :try{ &nbs
- 一个简单的照相功能,拍照之后在另一个activit中显示出拍照的图片。首先是布局文件:<?xml version="1.0&
- 本文主要介绍Android实现拍照、录像、录音代码的资料,这里整理了详细的代码,有需要的小伙伴可以参考下。RecordActivity.ja
- 前言学习了关于集合类的知识,我们可以做一个小项目来加深对集合类知识的学习!一、项目要求代码实现,一副扑克牌(不包括大小王)的购买、打乱、发牌
- 一、回顾Stream管道流操作通过前面章节的学习,我们应该明白了Stream管道流的基本操作。我们来回顾一下:源操作:可以将数组、集合类、行
- IO的本质IO的作用就是从外部系统读取数据到java程序中,或者把java程序中输出的数据写回到外部系统。这里的外部系统可能是磁盘,网络流等
- 智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delet
- 一、lateinit延迟初始化关键字Kotlin中很多语法特性,如变量不可变,变量不可为空,等等 这些特性都是为了尽可能地保证程序安全而设计
- 1.static静态变量1.静态变量被同一个类的所有对象共享2.static类变量在类加载的时候就生成使用static保存在class实例的
- 一、引言在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文