注意在搜索之前要先打开蓝牙设备

 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搜索蓝牙设备示例的更多相关文章

  1. Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表

    Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...

  2. SharePoint 2013 企业搜索架构示例

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,我们来看一下企业中对于不同规模SharePoint搜索的场的架构是什么样的. 对于搜索场的规模, ...

  3. Android开发之蓝牙--扫描已经配对的蓝牙设备

    一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等. 二. ...

  4. android蓝牙通讯开发(详细)

    新建一个工程之后,我们可以先看到界面左边的项目栏,我们可以看到,除了app目录以外,大多数的文件和目录都是自动生成的,我们也不需要对他们进行修改,而app目录之下的文件才是我们工作的重点.下面,我先对 ...

  5. 【转】Android4.4(MT8685)源码蓝牙解析--BLE搜索

    原文网址:http://blog.csdn.net/u013467735/article/details/41962075 BLE:全称为Bluetooth Low Energy.蓝牙规范4.0最重要 ...

  6. 【转】android蓝牙开发 蓝牙设备的查找和连接

    1.  首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 // 管理蓝牙设备的权限 <uses-permissionandroid:name="android. ...

  7. Android 中文API (69) —— BluetoothAdapter[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...

  8. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  9. ZT android -- 蓝牙 bluetooth (三)搜索蓝牙

    android -- 蓝牙 bluetooth (三)搜索蓝牙 分类: Android的原生应用分析 2013-05-31 22:03 2192人阅读 评论(8) 收藏 举报 bluetooth蓝牙s ...

随机推荐

  1. This bison version is not supported for regeneration of the Zend/PHP parsers

    在 mac 中编译 php-src.git 报错: configure: WARNING: This bison version is not supported , excluded: ). con ...

  2. Stimulsoft Reports报表工具

    关于第三方的报表工具,网上的种类有很多,一些专门做报表工具的公司,还针对不同平台语言做了分别处理.总之功能都很强大,比较流行和使用广泛的貌似还是国外的产品,版本收费和中文资料匮乏,这都是不可避免的问题 ...

  3. php获取客户端ip地址

    本文介绍一个,php获取客户端的IP地址的实例代码,有需要的朋友参考下吧. 获取客户端IP地址的代码,如下: 复制代码代码示例: <?php//取得客户端IP的函数function get_cl ...

  4. 35 个必须有的Bootstrap工具和生成器

    Bootstraptor If you think that bootstrap templates are not enough for you, you should go with bootst ...

  5. (转载)异构数据库之间完全可以用SQL语句导数据

    <来源网址:http://www.delphifans.com/infoview/Article_398.html>异构数据库之间完全可以用SQL语句导数据 告诉你一个最快的方法,用SQL ...

  6. iOS7.1Https企业证书发布方法

    openssl使用的是macos系统自带的版本,关键点是不同直接使用ios设备打开https的链接,需要将证书发到系统的mail里,安装到设备, 如果命令执行不成功,用sudo执行. 1.生成服务器的 ...

  7. Delaunay三角剖分

    Bowyer-Watson算法:1.假设已经生成了连接若干个顶点的Delaunay三角网格:2.加入一个新的节点,找出所有外接圆包含新加入节点的三角形,并将这些三角形删除形成一个空洞:3.空洞的节点与 ...

  8. 1185: [HNOI2007]最小矩形覆盖 - BZOJ

      就是一道凸包(枚举凸包的边作为矩形的一条边)的裸题,只是不太好打,所以犹豫到今天才打 不说了,说起AC都是泪啊,因为没有精度判断,没有判重(算距离时除0了)错了好久 拍了好久都和标称是一样的,因为 ...

  9. 2038: [2009国家集训队]小Z的袜子(hose) - BZOJ

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只 ...

  10. Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯

    Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯ The Otto Grou ...