转载 ----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 ...
随机推荐
- AC日记——数据流中的算法 51nod 1785
数据流中的算法 思路: 线段树模拟: 时间刚刚卡在边界上,有时超时一个点,有时能过: 来,上代码: #include <cstdio> #include <cstring> # ...
- Rust-HayStack
src/main.rs extern crate multipart; extern crate iron; extern crate time; //image converter extern c ...
- 牛客网 牛客小白月赛1 F.三视图
F.三视图 链接:https://www.nowcoder.com/acm/contest/85/F来源:牛客网 这个题自己想一下三维的,正视图和左视图中y轴为行数,x轴和z轴是列数,因为 ...
- Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...
- codevs——1044 拦截导弹(序列DP)
1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 某国为了防御敌国的导弹袭击 ...
- 2016集训测试赛(十八)Problem C: 集串雷 既分数规划学习笔记
Solution 分数规划经典题. 话说我怎么老是忘记分数规划怎么做呀... 所以这里就大概写一下分数规划咯: 分数规划解决的是这样一类问题: 有\(a_1, a_2 ... a_n\)和\(b_1, ...
- PyTorch 实现kmax-pooling
max-pooling有很多种实现方式 (1)kmax-pooling import torch def kmax_pooling(x, dim, k): index = x.topk(k, dim= ...
- Docker镜像原理和最佳实践
https://yq.aliyun.com/articles/68477 https://yq.aliyun.com/articles/57126 DockerCon 2016 深度解读: Dock ...
- rapidxml读取包含中文路径的xml解析错误的解决方法
from http://blog.csdn.net/qinwei4072880/article/details/38865179 1.rapidxml不支持中文路径. 2.rapidxml不支持Uni ...
- Working With Push Buttons In Oracle Forms
Managing push buttons at run time in Oracle Forms is very simple and in this tutorial you will learn ...