一. 参考

  1. Bluetooth overview
    https://developer.android.com/guide/topics/connectivity/bluetooth

  2. Android BluetoothChat Sample
    https://github.com/googlearchive/android-BluetoothChat

二. 简单概述

  1. Find devices (发现设备)
  2. Connect devices(连接设备)
  3. Manage a connection(进行数据传输)

三. 分步骤介绍

(一) Find devices (发现设备)

1.Query paired devices(查询已配对设备)

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

2 . Discover devices (发现设备)

     // Request discover from BluetoothAdaptermBtAdapter.startDiscovery();
     // Register for broadcasts when a device is discoveredIntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);this.registerReceiver(mReceiver, filter);

(二). Connect devices(连接设备)

1 . Connect as a client (连接作为一个客户端发起者)

         // Get a BluetoothSocket to connect with the given BluetoothDevice.// MY_UUID is the app's UUID string, also used in the server code.BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);// Connect to the remote device through the socket. This call blocks// until it succeeds or throws an exception.mmSocket.connect();

2 . Connect as a server(连接作为一个服务端监听连接)

         // MY_UUID is the app's UUID string, also used by the client code.BluetoothServerSocket mmServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);BluetoothSocket socket = mmServerSocket.accept();

(三).Manage a connection(进行数据传输)

/*** This thread runs during a connection with a remote device.* It handles all incoming and outgoing transmissions.*/private class ConnectedThread extends Thread {private final BluetoothSocket mmSocket;private final InputStream mmInStream;private final OutputStream mmOutStream;public ConnectedThread(BluetoothSocket socket, String socketType) {Log.d(TAG, "create ConnectedThread: " + socketType);mmSocket = socket;InputStream tmpIn = null;OutputStream tmpOut = null;// Get the BluetoothSocket input and output streamstry {tmpIn = socket.getInputStream();tmpOut = socket.getOutputStream();} catch (IOException e) {Log.e(TAG, "temp sockets not created", e);}mmInStream = tmpIn;mmOutStream = tmpOut;mState = STATE_CONNECTED;}public void run() {Log.i(TAG, "BEGIN mConnectedThread");byte[] buffer = new byte[1024];int bytes;// Keep listening to the InputStream while connectedwhile (mState == STATE_CONNECTED) {try {// Read from the InputStreambytes = mmInStream.read(buffer);// Send the obtained bytes to the UI ActivitymHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer).sendToTarget();} catch (IOException e) {Log.e(TAG, "disconnected", e);connectionLost();break;}}}/*** Write to the connected OutStream.** @param buffer The bytes to write*/public void write(byte[] buffer) {try {mmOutStream.write(buffer);// Share the sent message back to the UI ActivitymHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();} catch (IOException e) {Log.e(TAG, "Exception during write", e);}}public void cancel() {try {mmSocket.close();} catch (IOException e) {Log.e(TAG, "close() of connect socket failed", e);}}}

蓝牙(Android使用)基本原理相关推荐

  1. android8.1获取蓝牙地址,[蓝牙] Android 8.1 获取蓝牙设备地址无效;02:00:00:00:00:00

    序言: 对于这个问题, 谷歌是这样说的: 为了向用户提供更好的数据保护,从此版本开始,Android删除了使用Wi-Fi和蓝牙API对应用程序的设备本地硬件标识符的编程访问. ok.至此我们已经知道为 ...

  2. delphi android 蓝牙,Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)

    [实例简介] 2.保证无毒 3.简单,方便,实用 4.实例可以自行改用 5.如有非法,本人无法律责任,由改动代码人负责! 6.需要更多本人作品,查找标签"朱建强" 7.请下载,杀毒 ...

  3. 车载bc8android蓝牙,android蓝牙接收单片机数据并绘制波形

    [实例简介] android蓝牙接收单片机数据并绘制波形 [实例截图] [核心代码] BluetoothActivity └── BluetoothActivity ├── AndroidManife ...

  4. 基于51设计的宠物防丢系统(蓝牙+Android上位机)

    一.环境介绍 单片机型号:  STC89C52 编程软件: keil5 编程语言:  C语言 手机APP:  采用QT设计,程序支持跨平台编译运行(Android.IOS.Windows.Linux都 ...

  5. android扫描不到手机蓝牙,Android上的蓝牙:StartDiscovery无法正常工作。无法扫描设备...

    我是新来的android和我正在与蓝牙功能的应用程序.我能够设置蓝牙适配器,获取我自己的设备信息,但我无法使用startdiscovery发现蓝牙设备.当我开始扫描时,它什么都不做.Android上的 ...

  6. android双模蓝牙,Android和双模蓝牙设备:与BR / EDR(经典)配对时未找到自定义BLE服务...

    我使用Silicon Labs的BT 121构建了双模蓝牙设备 . 该设备通过经典连接实现SPP . 经典节点的设备名称是"XYZ Classic" . 它还在BLE中实现自定义服 ...

  7. android开发蓝牙传输图片,如何发送/接收文本和图片通过蓝牙android到另一个android手机...

    我假设你已经在开发Android的蓝牙应用的经验. Android蓝牙API不直接支持OBEX,意味着我无法将任何文件直接推送到任何设备. Android蓝牙API提供诸如发现,连接和使用流的数据传输 ...

  8. 雕刻机蓝牙android上位机控制器设计

    博主在某宝购买了一台雕刻机,发现特别好用,切割胶木板,PCB,木材,塑料,以及激光雕刻文字等等都非常方便,但是新问题出现了,发现要么USB连接,要么内存卡,是不是非常不方便? 于是研发了Android ...

  9. android ble 经典蓝牙,Android 经典蓝牙(Classic Bluetooth)和低功耗蓝牙(BLE)

    [实例简介] 从蓝牙4.0开始包含两个蓝牙芯片模块:传统/经典蓝牙模块(Classic Bluetooth,简称BT)和低功耗蓝牙(Bluetooth Low Energy,简称BLE) 经典蓝牙是在 ...

  10. ipados 蓝牙 android,iPadOS 体验:走出 iOS,iPad 终于能取代 PC 了?

    2015 年 9 月,苹果发布了第一代 iPad Pro.在初代 iPad 上市四年半之后,苹果希望用这款屏幕更大.性能更强.支持外接键盘和 Apple Pencil 的加强版平板电脑为 iPad 赋 ...

最新文章

  1. 书多嚼不烂,看书的方法
  2. retinaface训练笔记
  3. TS对象类型 -- 接口(interface)
  4. oracle 数据库启动停止小结
  5. 极赋设计感的几何字体素材
  6. 发现Tensorflow
  7. 在Macbook M1上安装Parallels Desktop 17虚拟机和Originlab 2021
  8. linux .net环境变量,Linux编程 系统环境变量位置, 环境变量持久化
  9. [Java] java打飞机小游戏
  10. 2017计算机组装视频,【2017年整理】计算机组装维修教程11.ppt
  11. 篮球中的PG,SG,SF,PF 英文全称分别是什么
  12. 【vue】webapp移动端模板
  13. “我在网红书店,从来不看书”
  14. 神舟战神笔记本安装Ubuntu16.04 系统
  15. scrapy源码2:scheduler的源码分析
  16. C. Edgy Trees(并查集+细节)
  17. 解决Raid模式下重装系统无法识别固态硬盘的问题
  18. 绎维软件F-One获得B轮融资,华创资本领投,齐银基金跟投
  19. 安全运营四要素之资产、脆弱性、威胁和事件
  20. Markdown如何画时序图,一篇就够了

热门文章

  1. bat转换成exe文件:bat2exe
  2. 编译原理 上下文无关文法
  3. 如何修复苹果Mac中的快速视频播放错误
  4. 怎样修改管家婆服务器密码,管家婆软件肿么修改密码
  5. MATLAB符号运算
  6. 软件测试员工自述,软件测试人员的述职报告.docx
  7. php阿里云短信验证码
  8. RocketMQ(四)Linux搭建RocketMQ集群
  9. CISP可以自己报名考试吗?
  10. Java 常量池详解(一)字符串常量池