蓝牙(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 ...
随机推荐
- PHP 魔术方法 __get __set (二)
慢慢长寻夜,明月高空挂 __get() - 读取不可访问属性的值时,__get() 会被调用 __set() - 在给不可访问属性赋值时,__set() 会被调用 <?php /** * 清晰的 ...
- myeclipse 8.5 破解
步骤: (1)新建一个java project项目 (2)在src目录下新建一个名为MyEclipseGen的Java文件(MyEclipseGen.java) (3)MyEclipseGen.jav ...
- TweenMax动画库学习(五)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- IIS上的错误与解决方案
1.未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案:在IIS上打开该网站应用程序池-->高级设置-->启动32位程序-->选True
- laravel 框架 开源的cms推荐
laravel 框架写的开源的cms系统 TypiCMS系统 多语言和模块化的CMS Laravel 5.2框架 下载地址:https://github.com/TypiCMS/Base Bootst ...
- 简单风格 在线音乐播放器(支持wav,MP3等)
找了两天终于找到了,支持wav,MP3,其他格式没有测试. 1.修复了jQuery判断ie的bug, 2.修复播放循环 下载地址: http://pan.baidu.com/s/1o6upwHs
- JavaScript的语法要点 4 - 面向对象的基础
在传统的面向对象语言如C++.C#.Java中有类.对象.继承等概念.在JavaScript中又如何表示呢?JavaScript中没有class关键字,JavaScript中的类.对象.继承的概念是通 ...
- javascript实现URL不缓存的方法
<script> document.write("<s"+"cript type='text/javascript' src='/js/test.js? ...
- PHP获取搜索引擎关键字来源(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)
<?php //获取来自搜索引擎入站时的关键词 function get_keyword($url,$kw_start) { $start=stripos($url,$kw_start); $u ...
- ubuntu下安装ffmpeg和X264
第一步:安装必要的库 $:-dev libtheora-dev libx11-dev zlib1g-dev 第二步:安装SDL(否则可能编译不出ffplay) $:-dev $:-dev libsdl ...