1.权限 使用蓝牙设备需要先在Manifest中开放权限,位置如下。 [html] view plaincopy

...

// 使用蓝牙设备的权限

// 管理蓝牙设备的权限

2.打开蓝牙 获得蓝牙适配器(android.bluetooth.BluetoothAdapter),检查该设备是否支持蓝牙,如果支持,就打开蓝牙。 [java] view plaincopy // 检查设备是否支持蓝牙

adapter = BluetoothAdapter.getDefaultAdapter();

if (adapter == null)

{

// 设备不支持蓝牙

}

// 打开蓝牙

if (!adapter.isEnabled())

{

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

// 设置蓝牙可见性,最多300秒

intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

context.startActivity(intent);

}

3.获取已配对的蓝牙设备(android.bluetooth.BluetoothDevice) 首次连接某蓝牙设备需要先配对,一旦配对成功,该设备的信息会被保存,以后连接时无需再配对,所以已配对的设备不一定是能连接的。 [java] view plaincopy BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

Set devices = adapter.getBondedDevices();

for(int i=0; i

{

BluetoothDevice device = (BluetoothDevice) devices.iterator().next();

System.out.println(device.getName());

}

4.搜索周围的蓝牙设备 适配器搜索蓝牙设备后将结果以广播形式传出去,所以需要自定义一个继承广播的类,在onReceive方法中获得并处理蓝牙设备的搜索结果。 [java] view plaincopy // 设置广播信息过滤

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(BluetoothDevice.ACTION_FOUND);

intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

// 注册广播接收器,接收并处理搜索结果

context.registerReceiver(receiver, intentFilter);

// 寻找蓝牙设备,android会将查找到的设备以广播形式发出去

adapter.startDiscovery();

自定义广播类 [java] view plaincopy private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

}

}

}

5.蓝牙设备的配对和状态监视 [java] view plaincopy private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

// 获取查找到的蓝牙设备

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out.println(device.getName());

// 如果查找到的设备符合要连接的设备,处理

if (device.getName().equalsIgnoreCase(name)) {

// 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索

adapter.cancelDiscovery();

// 获取蓝牙设备的连接状态

connectState = device.getBondState();

switch (connectState) {

// 未配对

case BluetoothDevice.BOND_NONE:

// 配对

try {

Method createBondMethod = BluetoothDevice.class.getMethod("createBond");

createBondMethod.invoke(device);

} catch (Exception e) {

e.printStackTrace();

}

break;

// 已配对

case BluetoothDevice.BOND_BONDED:

try {

// 连接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

} else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {

// 状态改变的广播

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (device.getName().equalsIgnoreCase(name)) {

connectState = device.getBondState();

switch (connectState) {

case BluetoothDevice.BOND_NONE:

break;

case BluetoothDevice.BOND_BONDING:

break;

case BluetoothDevice.BOND_BONDED:

try {

// 连接

connect(device);

} catch (IOException e) {

e.printStackTrace();

}

break;

}

}

}

}

}

6.蓝牙设备的连接 [java] view plaincopy private void connect(BluetoothDevice device) throws IOException {

// 固定的UUID

final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

UUID uuid = UUID.fromString(SPP_UUID);

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);

socket.connect();

}

Android中发现蓝牙设备的广播是,Android 蓝牙设备的查找和连接相关推荐

  1. android中编译和使用luajit开发应用,Android 嵌入 LuaJIT 的曲折道路

    相关链接:Windows 下编译 LuaJIT 懒人与伸手党可以直接看最底部. 为什么使用 LuaJIT Lua 官方版的编译嵌入相对简单,但是为什么要用 LuaJIT 呢?我所了解到的优势有: 更高 ...

  2. Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

    Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题 这是API ...

  3. android中调用fft函数,J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data)...

    J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data) 我一直在玩这个游戏已经有一段时间了,我无法弄清楚我在这里要做的事情. ...

  4. Android中mesure过程详解 (结合Android 4.0.4 最新源码)

    如何遍历并绘制View树?之前的文章Android中invalidate() 函数详解(结合Android 4.0.4 最新源码)中提到invalidate()最后会发起一个View树遍历的请求,并通 ...

  5. Android中layout过程详解 (结合Android 4.0.4 最新源码)

    上一篇文章Android中mesure过程详解 (结合Android 4.0.4 最新源码)介绍了View树的measure过程,相对与measure过程,本文介绍的layout过程要简单多了,正如l ...

  6. android中如何新建一个activity,《Android Activity》活动的介绍和创建

    Activity 是什么 Android 有四大组件:Activity.Service.Broadcast Receiver 和 Content Provider. Activity,活动,即用来承载 ...

  7. android中的 listview控件,聊聊Android中的ListView控件

    软硬件环境 Macbook Pro MGX 72 Android Studio 1.3.2 坚果手机 前言 ListView是Android系统中使用非常广泛的一种控件,几乎所有的App都会用到它.它 ...

  8. Android中显示输入的隐藏密码/Android多语系支持

    1.我们常常会看到我们输入的密码都是以小黑点的形式出现,这在Android中实现是很简单的,只需要设置一个属性即可. 需要设置EditText的inputType属性,设置如下: android:in ...

  9. android中gradle的作用,Gradle 之 Android 中的应用

    在上一篇文章中 Gradle 之语言基础 Groovy 主要介绍了 Groovy 的基础语法(如果没有 Groovy 的基础,建议先看看上篇文章,如果可以动手敲一下里面的示例代码就更好不过了),也是为 ...

  10. android中prop配置参数名,21.Android系统属性build.prop文件(笔记)

    一.概念 在Android设备shell终端可以看到/system目录下的build.prop文件,Android的build.prop文件是在Android编译时刻收集的各种property,编译完 ...

最新文章

  1. double java 坑,Java中四则运算的那些坑
  2. Struts2 配置文件手册
  3. JavaScript学习总结(二)数组和对象部分
  4. 如何做好OA流程审批?
  5. Nginx安全说:一剑封喉
  6. mysql5.6错误代码
  7. 教学思路SQL之入门习题《学员成绩》 三、多表复杂子查询
  8. Unable to establish a connection to Redis Cluster at [RedisURI
  9. Android上图片文字识别
  10. 深圳福田区特殊住房申请学位需要哪些材料 具体材料汇总
  11. offline RL介绍
  12. 华三防火墙应用二层和三层的配置实例
  13. 在Linux服务器上安装SQL Server
  14. 三代基因组测序技术原理简介
  15. win10查看笔记本电脑电池健康度
  16. 嵌入式C++开发详解
  17. 技术人向顾问/管理者转型的推荐经典书35本
  18. 国人独有(写诗、对对联)--国学中的深度学习项目
  19. 深度学习:Concatenate的理解
  20. ORB-SLAM3中遇到的坑

热门文章

  1. 代码投毒、删库跑路,开源生态链安全该如何保证?
  2. 云原生是什么?它从哪里来?又到哪里去?
  3. 两大方案,只为写出更安全的代码!
  4. Python写出一个字节,一个YouTube,我用Python怎么了!
  5. 为信息产业自主化而奋斗,第一本龙芯平台的Linux内核书来了!
  6. Python 3.9 正式版要来了,会有哪些新特性?
  7. 珍稀干货!阿里 Web 音视频开发趟坑指南
  8. 互联网大厂春节礼盒鄙视链
  9. 实话!为什么2019年,我劝你别再闷头学Python!
  10. 小米上市 365 天:雷军的坚守与败退