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

最新文章

  1. [LeetCode] Remove K Digits 去掉K位数字
  2. 浅谈Android样式开发之shape
  3. c++中的重名问题
  4. Excel替换应用
  5. Unsupported configuration attributes: [FILE_UPLOAD]
  6. HDU 1372 Knight Moves (bfs)
  7. 算法实践——Twitter算法面试题(积水问题)的线性时间解法
  8. asp.net Linq 实现分组查询
  9. 面试时,问哪些问题能试出一个Android应用开发者真正的水平?
  10. wxsqlite3的加密模块单独编译
  11. Thinkphp的页面判断
  12. windows 下mysql的安装于使用(启动、关闭)
  13. ASP.NET MVC应用程序处理并发
  14. MYSQL:基础—存储过程
  15. HTML &lt;select&gt;标签
  16. hdu 3974 线段树 将树弄到区间上
  17. Ubuntu: Windows Help Tools For Ubuntu
  18. Samba文件共享系统
  19. C#泛型约束where T : class 解释
  20. 【ASP.Net MVC3 】使用Unity 实现依赖注入

热门文章

  1. LeetCode总结【转】
  2. javascript 之 className属性
  3. golang xorm reverse 自动生成数据库实体文件
  4. 静态变量(static)的特点
  5. 怎样录制简单GIF动图
  6. 基于WPF系统框架设计(7)-TextBox/PasswordBox在ViewModel中支持回车命令
  7. 关于Sending build context to Docker daemon 数据很大的问题
  8. 使用LeakCanary遇到的问题 就是不弹出来
  9. Aliyun-CentOS7.3 Init
  10. Tomcat Manager用户名和密码