1.什么是蓝牙
  Bluetooth是目前使用的最广泛的无线通讯协议之一
  主要针对短距离设备通讯(10米)
  常用于连接耳机、鼠标和移动通讯设备等

2.发现周围蓝牙设备
  BluetoothAdapter:代表了本地的蓝牙适配器
  BluetoothDevice:代表一个远程的蓝牙设备
  
  扫描已配对的蓝牙设备方法:
  1.在AndroidManifest.xml声明蓝牙权限
  <uses-permission Android:name="android.permission.BLUETOOTH" />
  
  2.获得BluetoothAdapter对象

3.判断当前设备中是否拥有蓝牙设备

4.判断当前设备中蓝牙设备是否已经打开

5.得到已经配对的蓝牙设备对象

  1. BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
  2. if(btAdapter != null){
  3. if(btAdapter.isEnabled()){
  4. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  5. startActivity(intent);
  6. Set<BluetoothDevice> devices = adapter.getBondedDevices();
  7. if(devices.size() > 0){
  8. for(Iterator iterator = devices.iterator(); iterator.hasNext()){
  9. BluetoothDevice device = (BluetoothDevice)iterator.next();
  10. System.out.println(bluetoothDevice.getAddress());
  11. }
  12. }
  13. else System.out.println("没有绑定的蓝牙设备");
  14. }
  15. else System.out.println("蓝牙设备未正常启动");
  16. }
  17. else System.out.println("无蓝牙设备");

3.修改本机蓝牙设备的可见性

  1. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  2. intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); //可见持续300秒,不能超过300秒
  3. startActivity(intent);

4.扫描周围可用的蓝牙设备

  1. //1.注册一个广播,用于接收“发现设备”的广播
  2. IntentFilter intentFilter = IntentFilter(BluetoothAdapter.ACTION_FOUND);
  3. BluetoothReceiver receiver = new BluetoothReceiver();
  4. registerReceiver(receiver, intentFilter);
  5. //2.创建蓝牙适配器,并开始扫描
  6. BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
  7. btAdapter.startDiscovery();
  8. //3.继续广播接收类,处理接收到的广播
  9. private class BluetoothReceiver extends BroadcastReceiver{
  10. @Override
  11. public void onReceive(Context context, Intent intent){
  12. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  13. System.out.println(device.getAddress());
  14. }
  15. }

转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)的更多相关文章

  1. Android学习笔记(第二篇)View中的五大布局

    PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本... 学习内容: 1.用户界面View中的五大布局... i.首先介绍一下view的概念   view是什么呢?我们已经知道一个Act ...

  2. Android学习笔记(第一篇)编写第一个程序Hello World+Activity

    PS:终于开始正式的搞Android了...无人带的一介菜鸟,我还是自己默默的努力吧... 学习内容: 1.编写第一个Hello World程序..   学习Android,那么就需要有一个编译器来集 ...

  3. (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理

    异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...

  4. android学习笔记(入门篇)

    +号只是当你第一次定义一个资源ID的时候需要, 告诉SDK此资源ID需要被创建出来 对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身 ...

  5. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  6. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  7. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  8. 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...

  9. 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...

随机推荐

  1. AC日记——数据流中的算法 51nod 1785

    数据流中的算法 思路: 线段树模拟: 时间刚刚卡在边界上,有时超时一个点,有时能过: 来,上代码: #include <cstdio> #include <cstring> # ...

  2. Rust-HayStack

    src/main.rs extern crate multipart; extern crate iron; extern crate time; //image converter extern c ...

  3. 牛客网 牛客小白月赛1 F.三视图

    F.三视图   链接:https://www.nowcoder.com/acm/contest/85/F来源:牛客网     这个题自己想一下三维的,正视图和左视图中y轴为行数,x轴和z轴是列数,因为 ...

  4. 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 ...

  5. codevs——1044 拦截导弹(序列DP)

    1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 某国为了防御敌国的导弹袭击 ...

  6. 2016集训测试赛(十八)Problem C: 集串雷 既分数规划学习笔记

    Solution 分数规划经典题. 话说我怎么老是忘记分数规划怎么做呀... 所以这里就大概写一下分数规划咯: 分数规划解决的是这样一类问题: 有\(a_1, a_2 ... a_n\)和\(b_1, ...

  7. PyTorch 实现kmax-pooling

    max-pooling有很多种实现方式 (1)kmax-pooling import torch def kmax_pooling(x, dim, k): index = x.topk(k, dim= ...

  8. Docker镜像原理和最佳实践

    https://yq.aliyun.com/articles/68477 https://yq.aliyun.com/articles/57126  DockerCon 2016 深度解读: Dock ...

  9. rapidxml读取包含中文路径的xml解析错误的解决方法

    from http://blog.csdn.net/qinwei4072880/article/details/38865179 1.rapidxml不支持中文路径. 2.rapidxml不支持Uni ...

  10. 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 ...