Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
来源:互联网 发布:c语言简单的程序 编辑:程序博客网 时间:2024/05/16 19:20
一. 什么是蓝牙(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 得到所有已经配对的蓝牙设备对象
实现代码如下:
MainActivity:
- 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("本机没有蓝牙设备!");
- }
- }
- });
- }
- }
0 0
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备(转)
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备(转)
- Android Bluetooth蓝牙开发:Bluetooth蓝牙设备配对Paired Bluetooth Devices(2)
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- Bluetooth-->蓝牙开发扫描设备,及蓝牙设备类型
- android之蓝牙通信(一)(检测蓝牙、开启蓝牙、扫描附近蓝牙设备)
- Android Bluetooth蓝牙开发:发现Bluetooth蓝牙设备(1)
- iOS蓝牙开发连接系统或其他APP已经连接、配对成功的蓝牙设备
- Android开发之蓝牙连接与配对设备
- Android Bluetooth 学习(3)蓝牙设备之间自动配对
- Android Bluetooth蓝牙开发:Bluetooth蓝牙设备之间的连接建立(3)
- android 打开蓝牙设备 显示已经配对的蓝牙设备 ,并将已配对的蓝牙设备显示在textview中
- Android 蓝牙设备通讯的开发(配对/连接/传输数据)
- Bluetooth蓝牙设备配对 - 常见问题解答
- IOS - 实时显示iOS编写UI代码效果(控件的懒加载和使用Plist文件将数据与代码分离),iosplist
- poj 2813 画家问题(枚举局部)
- The table '#sql-fb5_190a57' is full Operation failed
- 【Linux进阶】Linux防火墙iptables详解
- spark中的SparkContext实例的textFile使用的小技巧
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
- 通过Hash用锁控制并发
- ios layer图层排序
- JAVA web的常见面试
- IOS 的 KVO机制
- php文件只要开始标签而不要结尾标签?
- Activiti工作流引擎使用
- eclipse创建android项目,无法正常预览布局文件
- MYSQL必知必会读书笔记 第十八章 全文本检索