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. hdu 3986(最短路变形好题)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. 牛客网 牛客练习赛13 A.幸运数字Ⅰ

    A.幸运数字Ⅰ 链接:https://www.nowcoder.com/acm/contest/70/A来源:牛客网     水题.   代码: #include<iostream> #i ...

  3. Excel Sheet Column Title - LeetCode

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  4. Word Pattern - LeetCode

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. 【转】java:多网卡环境下获取MAC地址

    http://blog.csdn.net/10km/article/details/78569962 JDK6以后 java.net.NetworkInterface提供了完整的方法用于获取网络设备信 ...

  6. Autolayout 02

    Working with Auto Layout Programmatically 如果你在运行阶段添加或者移除views你就需要通过代码来添加约束来保证你的interface能正确适应size或者o ...

  7. Autolayout 01

    Auto Layout Concepts auto layout的基本概念是constraint(约束).表示了你interface中的layout元素.例如,你可以创建一个约束来指定元素的宽度或者距 ...

  8. 安全 --- CSRF攻击

    什么是CSRF CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSRF/X ...

  9. Android 监听双卡信号强度(附完整代码)

    Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...

  10. ThinkPHP 中M方法和D方法的具体区别

    M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...