一. 什么是蓝牙(Bluetooth)?

1.1  BuleTooth是目前使用最广泛的无线通信协议

1.2  主要针对短距离设备通讯(10m)

1.3  常用于连接耳机,鼠标和移动通讯设备等.

二. 与蓝牙相关的API

2.1 BluetoothAdapter:

代表了本地的蓝牙适配器

2.2 BluetoothDevice

代表了一个远程的Bluetooth设备

三. 扫描已经配对的蓝牙设备(1)

注:必须部署在真实手机上,模拟器无法实现

首先需要在AndroidManifest.xml 声明蓝牙权限

<user-permission android:name="android.permission.BLUETOOTH" />

配对蓝牙需要手动操作:

1. 打开设置--> 无线网络 --> 蓝牙 勾选开启

2. 打开蓝牙设置  扫描周围已经开启的蓝牙设备(可以与自己的笔记本电脑进行配对),点击进行配对

电脑上会弹出提示窗口: 添加设备

显示计算与设备之间的配对码,要求确认是否配对

手机上也会显示类似的提示.

四. 扫描已经配对的蓝牙设备(2)

4.1 获得BluetoothAdapter对象

4.2 判断当前移动设备中是否拥有蓝牙

4.3 判断当前移动设备中蓝牙是否已经打开

4.4 得到所有已经配对的蓝牙设备对象

import java.util.Iterator;
import java.util.Set; import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
private Button button = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
//获得BluetoothAdapter对象,该API是android 2.0开始支持的
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
//adapter不等于null,说明本机有蓝牙设备
if(adapter != null){
System.out.println("本机有蓝牙设备!");
//如果蓝牙设备未开启
if(!adapter.isEnabled()){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//请求开启蓝牙设备
startActivity(intent);
}
//获得已配对的远程蓝牙设备的集合
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if(devices.size()>0){
for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){
BluetoothDevice device = (BluetoothDevice)it.next();
//打印出远程蓝牙设备的物理地址
System.out.println(device.getAddress());
}
}else{
System.out.println("还没有已配对的远程蓝牙设备!");
}
}else{
System.out.println("本机没有蓝牙设备!");
}
}
});
}
}

  

Android开发之蓝牙--扫描已经配对的蓝牙设备的更多相关文章

  1. Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) ...

  2. 【视频】零基础学Android开发:蓝牙聊天室APP(四)

    零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...

  3. 【视频】零基础学Android开发:蓝牙聊天室APP(二)

    零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...

  4. 【视频】零基础学Android开发:蓝牙聊天室APP(三)

    零基础学Android开发:蓝牙聊天室APP第三讲 3.1 ImageView.ImageButton控件具体解释 3.2 GridView控件具体解释 3.3 SimpleAdapter适配器具体解 ...

  5. 【视频】零基础学Android开发:蓝牙聊天室APP(一)

    零基础学Android开发:蓝牙聊天室APP第一讲 1. Android介绍与环境搭建:史上最高效Android入门学习 1.1 Google的大小战略 1.2 物联网与云计算 1.3 智能XX设备 ...

  6. Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备

    一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 一.  清单文件AdroidManifest.xml: <uses-permission android:name="an ...

  7. Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 Eg: 一.  清单文件AdroidManifest.xml: <?xml v ...

  8. android开发之蓝牙配对连接的方法

    最近在做蓝牙开锁的小项目,手机去连接单片机总是出现问题,和手机的连接也不稳定,看了不少蓝牙方面的文档,做了个关于蓝牙连接的小结. 在做android蓝牙串口连接的时候一般会使用 ? 1 2 3 4 5 ...

  9. Android开发之蓝牙Socket

    蓝牙Server端就是通过线程来注册一个具有名称和唯一识别的UUID号的BluetoothServerSocket, 然后就一直监听Client端(BluetoothSocket)的请求,并对这些请求 ...

随机推荐

  1. css标签选择器的优先级

    1.行内样式>嵌入式>外部样式(就近原者) 当外部样式在嵌入式的前面时就是外部样式为主 2.重复样式p{color:red}p{color:green}<p>是green< ...

  2. JSON和js对象之间的相互转化

     jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 http://www. ...

  3. IOS-synthesize和dynamic的异同(转)

    一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...

  4. delphi平方开方

    用delphi自带的sqr和sqrt函数 procedure TForm2.SpeedButton3Click(Sender: TObject); begin ShowMessage(Sqr(6).T ...

  5. sql 过了试用期不能启动的,修改时间启动后还原。

    @echo off    set nowtime=%date%    echo 2014-12-01|date    sc start MSSQLSERVER    ping -n 5 127.1&g ...

  6. Unix时间戳与C# DateTime时间类型互换

    Unix时间戳最小单位是秒,开始时间为格林威治标准时间1970-01-01 00:00:00ConvertIntDateTime方法的基本思路是通过获取本地时区表示Unixk开始时间,加上Unix时间 ...

  7. 虚拟机Ubuntu14/15启用root用户登录

    1. 为root用户设置密码 sudo passwd root 需要先输入一次当前用户的登陆密码,然后输入两次root用户的密码(自己设置). 2. 切换至root用户 sudo -s 3. 编辑登陆 ...

  8. ASP.NET Web API 2 中的特性路由

    ASP.NET MVC 5.1 开始已经支持基于特性的路由(http://attributerouting.net),ASP.NET WEB API 2 同时也支持了这一特性. 启用特性路 由只需要在 ...

  9. 使用Let's Encrypt手动创建https证书

    Let's Encrypt是一个开源免费的SSL证书项目,是由 Mozilla.思科.Akamai.IdenTrust 和 EFF 等组织发起的,现由Linux基金会托管. 这篇博文分享的是使用let ...

  10. [C++] 将 mp3 等音乐资源以资源形式嵌入 exe 文件中

    引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...