1.什么是蓝牙
Bluetooth是目前使用的最广泛的无线通讯协议之一
主要针对短距离设备通讯(10米)
常用于连接耳机、鼠标和移动通讯设备等
2.发现周围蓝牙设备
BluetoothAdapter:代表了本地的蓝牙适配器
BluetoothDevice:代表一个远程的蓝牙设备
扫描已配对的蓝牙设备方法:
1.在AndroidManifest.xml声明蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH" />
2.获得BluetoothAdapter对象
3.判断当前设备中是否拥有蓝牙设备
4.判断当前设备中蓝牙设备是否已经打开
5.得到已经配对的蓝牙设备对象
- BluetoothAdapterbtAdapter=BluetoothAdapter.getDefaultAdapter();
- if(btAdapter!=null){
- if(btAdapter.isEnabled()){
- Intentintent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivity(intent);
- Set<BluetoothDevice>devices=adapter.getBondedDevices();
- if(devices.size()>0){
- for(Iteratoriterator=devices.iterator();iterator.hasNext()){
- BluetoothDevicedevice=(BluetoothDevice)iterator.next();
- System.out.println(bluetoothDevice.getAddress());
- }
- }
- elseSystem.out.println("没有绑定的蓝牙设备");
- }
- elseSystem.out.println("蓝牙设备未正常启动");
- }
- elseSystem.out.println("无蓝牙设备");
3.修改本机蓝牙设备的可见性
- Intentintent=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
- startActivity(intent);
4.扫描周围可用的蓝牙设备
- IntentFilterintentFilter=IntentFilter(BluetoothAdapter.ACTION_FOUND);
- BluetoothReceiverreceiver=newBluetoothReceiver();
- registerReceiver(receiver,intentFilter);
- BluetoothAdapterbtAdapter=BluetoothAdapter.getDefaultAdapter();
- btAdapter.startDiscovery();
- privateclassBluetoothReceiverextendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- System.out.println(device.getAddress());
- }
- }
分享到:
相关推荐
Android学习笔记-Activity篇 Contents Android学习笔记-Activity篇 1 一、activity创建: 1 二、在manifest.xml中声明activity: 1 三、启动activity 1 3.1 启动不带返回结果的Activity 1 3.2 启动带有返回结果的...
android学习笔记-clip.pdf
android学习笔记--clip.pdf
android学习笔记--clip参照.pdf
http://blog.csdn.net/loveqRc/article/details/54169666 Android学习笔记--Gson笔记
Android学习笔记--通过wifi向服务器端发送数据
Android学习笔记-SQLite介绍-以及使用Sqlite-进行数据库的创建-完成数据.pdf
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
黑马程序员之android学习笔记--用户界面 View(一).docx
Android学习笔记-采用ListView实现数据列表显示-以及各种适配器使用-和如何写.pdf
Android学习笔记-service[参考].pdf
python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python ...
android 的学习笔记 菜单时android的重要用户接口之一
JDBC学习笔记--JDBC学习笔记JDBC学习笔记--JDBC学习笔记JDBC学习笔记--JDBC学习笔记JDBC学习笔记--JDBC学习笔记
android的binder机制提供一种进程间通信的方法,使不同一个进程可以以类似远程过程调用的形式调用另一个进程所提供的功能。binder机制在Java环境和C/C++环境都有提供。
Android开发笔记---ListView
LaTex教程--latex排版学习笔记---入门提高.pdf
zookeeper云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-...