转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)
1.什么是蓝牙
Bluetooth是目前使用的最广泛的无线通讯协议之一
主要针对短距离设备通讯(10米)
常用于连接耳机、鼠标和移动通讯设备等
2.发现周围蓝牙设备
BluetoothAdapter:代表了本地的蓝牙适配器
BluetoothDevice:代表一个远程的蓝牙设备
扫描已配对的蓝牙设备方法:
1.在AndroidManifest.xml声明蓝牙权限
<uses-permission Android:name="android.permission.BLUETOOTH" />
2.获得BluetoothAdapter对象
3.判断当前设备中是否拥有蓝牙设备
4.判断当前设备中蓝牙设备是否已经打开
5.得到已经配对的蓝牙设备对象
- BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
- if(btAdapter != null){
- if(btAdapter.isEnabled()){
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivity(intent);
- Set<BluetoothDevice> devices = adapter.getBondedDevices();
- if(devices.size() > 0){
- for(Iterator iterator = devices.iterator(); iterator.hasNext()){
- BluetoothDevice device = (BluetoothDevice)iterator.next();
- System.out.println(bluetoothDevice.getAddress());
- }
- }
- else System.out.println("没有绑定的蓝牙设备");
- }
- else System.out.println("蓝牙设备未正常启动");
- }
- else System.out.println("无蓝牙设备");
3.修改本机蓝牙设备的可见性
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //可见持续300秒,不能超过300秒
- startActivity(intent);
4.扫描周围可用的蓝牙设备
- //1.注册一个广播,用于接收“发现设备”的广播
- IntentFilter intentFilter = IntentFilter(BluetoothAdapter.ACTION_FOUND);
- BluetoothReceiver receiver = new BluetoothReceiver();
- registerReceiver(receiver, intentFilter);
- //2.创建蓝牙适配器,并开始扫描
- BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
- btAdapter.startDiscovery();
- //3.继续广播接收类,处理接收到的广播
- private class BluetoothReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent){
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- System.out.println(device.getAddress());
- }
- }
转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)的更多相关文章
- Android学习笔记(第二篇)View中的五大布局
PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本... 学习内容: 1.用户界面View中的五大布局... i.首先介绍一下view的概念 view是什么呢?我们已经知道一个Act ...
- Android学习笔记(第一篇)编写第一个程序Hello World+Activity
PS:终于开始正式的搞Android了...无人带的一介菜鸟,我还是自己默默的努力吧... 学习内容: 1.编写第一个Hello World程序.. 学习Android,那么就需要有一个编译器来集 ...
- (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理
异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...
- android学习笔记(入门篇)
+号只是当你第一次定义一个资源ID的时候需要, 告诉SDK此资源ID需要被创建出来 对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身 ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- ActionBarSherlock学习笔记 第一篇——部署
ActionBarSherlock学习笔记 第一篇--部署 ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...
- 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...
随机推荐
- 转载——C# 6.0可能的新特性及C#发展历程
据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...
- Data structure basics - Java Implementation
Stack & Queue Implementations FixedCapacityQueue package cn.edu.tsinghua.stat.mid_term; import j ...
- java 相关软件使用趋势
http://www.baeldung.com/java-in-2017 https://mp.weixin.qq.com/s?__biz=MzI4NjYwMjcxOQ==&mid=224 ...
- weblogic的集群与配置图文
一.Weblogic的集群 还记得我们在第五天教程中讲到的关于Tomcat的集群吗? 两个tomcat做node即tomcat1, tomcat2,使用Apache HttpServer做请求派发 ...
- Hadoop Mapreduce分区、分组、二次排序过程详解
转载:http://blog.tianya.cn/m/post.jsp?postId=53271442 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2)定制了 ...
- Intellij IDEA 拷贝的项目变为红色名字
Intellij IDEA 拷贝的项目变为红色名字 学习了:https://blog.csdn.net/lishaoran369/article/details/72991805 settings & ...
- 模拟利器Mockito
16.3.1 模拟测试概述 目前支持Java语言的Mock测试工具有EasyMock.JMock.Mockito.MockCreator.Mockrunner.MockMaker等,Mockito是 ...
- NYOJ92 图像实用区域 【BFS】
碰到了一个曾经从未见过的奇怪问题:先上截图: 执行号 用户 题目 结果 时间 内存 语言 提交时间 895360 userid=%E9%95%BF%E6%9C%A8" style=" ...
- 一波三折ST-Link
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- vuex 中关于 mapState 的作用
辅助函数 Vuex 除了提供我们 Store 对象外,还对外提供了一系列的辅助函数,方便我们在代码中使用 Vuex,提供了操作 store 的各种属性的一系列语法糖,下面我们来一起看一下: mapSt ...