博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
阅读量:4677 次
发布时间:2019-06-09

本文共 2086 字,大约阅读时间需要 6 分钟。

一. 什么是蓝牙(Bluetooth)?

 

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

 

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

 

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

 

二. 与蓝牙相关的API

 

2.1 BluetoothAdapter:

代表了本地的蓝牙适配器

 

2.2 BluetoothDevice

代表了一个远程的Bluetooth设备

 

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

 

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

 

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

 

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

 

配对蓝牙需要手动操作:

 

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

 

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

 

 

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

 

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

 

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

 

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

 

4.1 获得BluetoothAdapter对象

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

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

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

 

 

实现代码如下:

MainActivity:

 

[java]   
 
  1. import java.util.Iterator;  
  2. import java.util.Set;  
  3.   
  4. import android.app.Activity;  
  5. import android.bluetooth.BluetoothAdapter;  
  6. import android.bluetooth.BluetoothDevice;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private Button button = null;  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.         button = (Button)findViewById(R.id.buttonId);  
  22.         button.setOnClickListener(new OnClickListener(){  
  23.   
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 //获得BluetoothAdapter对象,该API是android 2.0开始支持的  
  27.                 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  
  28.                 //adapter不等于null,说明本机有蓝牙设备  
  29.                 if(adapter != null){  
  30.                     System.out.println("本机有蓝牙设备!");  
  31.                     //如果蓝牙设备未开启  
  32.                     if(!adapter.isEnabled()){  
  33.                         Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  34.                         //请求开启蓝牙设备  
  35.                         startActivity(intent);  
  36.                     }  
  37.                     //获得已配对的远程蓝牙设备的集合  
  38.                     Set<BluetoothDevice> devices = adapter.getBondedDevices();  
  39.                     if(devices.size()>0){  
  40.                         for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){  
  41.                             BluetoothDevice device = (BluetoothDevice)it.next();  
  42.                             //打印出远程蓝牙设备的物理地址  
  43.                             System.out.println(device.getAddress());  
  44.                         }  
  45.                     }else{  
  46.                         System.out.println("还没有已配对的远程蓝牙设备!");  
  47.                     }  
  48.                 }else{  
  49.                     System.out.println("本机没有蓝牙设备!");  
  50.                 }  
  51.             }  
  52.         });  
  53.     }  
  54. }  

转载于:https://www.cnblogs.com/Free-Thinker/p/6798529.html

你可能感兴趣的文章
数据结构化与保存
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
大型网站高并发的架构演变图-摘自网络
查看>>
8丶运行及总结
查看>>
Design Pattern --- Strategy
查看>>
mui列表跳转到详情页优化方案
查看>>
一些简单有用的方法合集
查看>>
Neutron 架构 - 每天5分钟玩转 OpenStack(67)
查看>>
详解JS设计模式
查看>>
CPSR寄存器
查看>>
Java基础50题test7—处理字符串
查看>>
保险行业电话外呼型呼叫中心方案
查看>>
自建型呼叫中心
查看>>