蓝牙(2)用BluetoothAdapter搜索蓝牙设备示例
注意在搜索之前要先打开蓝牙设备
package com.e.search.bluetooth.device; 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.view.View;
import android.view.Window;
import android.widget.TextView; public class Main extends Activity {
//搜索蓝牙设备第1步,在manifest.xml中加入权限
/*
* <uses-permission android:name="android.permission.BLUETOOTH" />
* <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
*/ //搜索蓝牙设备第2步,准备BluetoothAdapter
private BluetoothAdapter mBluetoothAdapter;
private TextView tvDevices; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main); tvDevices = (TextView) findViewById(R.id.tvDevices);
//搜索蓝牙设备第3步,获取或创建BluetoothAdapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //搜索蓝牙设备第4步,得到已经配对的蓝牙设备.
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > ) {
//枚举已经配对的设备的名字和地址.
for (BluetoothDevice device : pairedDevices) {
tvDevices.append(device.getName() + ":" + device.getAddress());
}
} //搜索蓝牙设备第5步,用广播接收器得到蓝牙找到设备和搜索完成广播消息.
//注册找到蓝牙设备广播接收器,用filter注册
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(receiver, filter); //注册搜索蓝牙设备已完成广播接收器,用filter注册
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(receiver, filter); } /*
* 开始搜索按钮
*/
public void onClick_Search(View view) {
//显示进度条
setProgressBarIndeterminateVisibility(true);
setTitle("正在搜索...");
//搜索蓝牙设备第6步,开始搜索蓝牙设备.
if (mBluetoothAdapter.isDiscovering()) {//如果正在搜索,那么先取消.
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();//异步的开始搜索.
}
//搜索蓝牙设备第7步,处理找到蓝牙设备和搜索完成广播消息.
private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {//找到蓝牙设备 //搜索蓝牙设备第8步,搜到蓝牙设备.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//搜索蓝牙设备第9步,把设备添加到已找到列表中,这里只是显示它的信息.
tvDevices.append(device.getName() + ":"+ device.getAddress() + "\n");
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.//搜索完成
equals(action)) {
//搜索蓝牙设备第10步,搜索完毕.
setProgressBarVisibility(false);
setTitle("已搜素完成");
}
}
};
}
蓝牙(2)用BluetoothAdapter搜索蓝牙设备示例的更多相关文章
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...
- SharePoint 2013 企业搜索架构示例
博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,我们来看一下企业中对于不同规模SharePoint搜索的场的架构是什么样的. 对于搜索场的规模, ...
- Android开发之蓝牙--扫描已经配对的蓝牙设备
一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) 1.3 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
- android蓝牙通讯开发(详细)
新建一个工程之后,我们可以先看到界面左边的项目栏,我们可以看到,除了app目录以外,大多数的文件和目录都是自动生成的,我们也不需要对他们进行修改,而app目录之下的文件才是我们工作的重点.下面,我先对 ...
- 【转】Android4.4(MT8685)源码蓝牙解析--BLE搜索
原文网址:http://blog.csdn.net/u013467735/article/details/41962075 BLE:全称为Bluetooth Low Energy.蓝牙规范4.0最重要 ...
- 【转】android蓝牙开发 蓝牙设备的查找和连接
1. 首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 // 管理蓝牙设备的权限 <uses-permissionandroid:name="android. ...
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...
- android -- 蓝牙 bluetooth (三)搜索蓝牙
接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...
- ZT android -- 蓝牙 bluetooth (三)搜索蓝牙
android -- 蓝牙 bluetooth (三)搜索蓝牙 分类: Android的原生应用分析 2013-05-31 22:03 2192人阅读 评论(8) 收藏 举报 bluetooth蓝牙s ...
随机推荐
- 正则表达式模块re
正则表达式通过特定的式子匹配某种类型的字符串.对字符串进行匹配验证或者从大量的字符串里提取出特定的信息. 正则表达式包含普通字符和特殊字符.大部分的普通字符如‘A’或‘0’可以简单的来匹配自己.如te ...
- Python学习_算数运算函数
记录以grades列表为例,分别定义输出.求和.平均值.方差和标准差函数,并输出相应的值 grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90 ...
- Speech Patterns (string)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- Can’t create handler inside thread that has not called Looper.prepare()
1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...
- Arcgis 10.1 求面里面所包含的点的平均值
空间链接的作用:将面上的所有点的值加起来取平均值。赋值给面属性。(我们可以定义右击——定义合并规则 连接要素的字段映射参数中指定的合并规则仅适用于连接要素中的属性,且仅适用于多个要素与目标要素匹配 ...
- mysqlsla慢查询分析工具教程
mysqlsla是一款帮助语句分析.过滤.和排序的功能,能够处理MySQL慢查询日志.二进制日志等.整体来说, 功能非常强大. 能制作SQL查询数据报表,分析包括执行频率, 数据量, 查询消耗等. 且 ...
- SQL学习中(序)
从校园中走出来已经两年多了,从最初的企业建站到现在的BPM流程开发,深深的感觉到自己底子很差,因此在园子里记录一下学过的知识,以此自勉. 废话到此结束,今天就先来看一下CREATE CHEMA 语句. ...
- Mac 启用http-dav功能(WebDAV服务器)
启用Mac的WebDAV可以实现文件的上传以及Windows.Linux和Mac之间的数据互传. 客户端使用:windows下使用网上邻居 --> 添加一个网上邻居 --> 输入uplo ...
- 5.0:Spring-bean的加载
内容来自<Spring深度解析>,之后的不一一复述! 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎 ...
- jquery图片放大器插件
将鼠标移动到一张图片上来的时候,放大该图片的某些细节. <html> <head> <script src="../js/jquery-1.6.js" ...