标准蓝牙 UUID

0000xxxx-0000-1000-8000-00805F9B34FB

其中 xxxx 代表自定义部分

例如:蓝牙串口服务1101 -- SerialPortServiceClass_UUID1102    -- LANAccessUsingPPPServiceClass_UUID 拨号网络服务1103  -- DialupNetworkingServiceClass_UUID 信息同步服务1104     -- IrMCSyncServiceClass_UUID 文件传输服务1106 -- OBEXFileTransferServiceClass_UUID蓝牙传真服务1111  -- FaxServiceClass_UUID 蓝牙打印服务1126  -- HCRPrintServiceClass_UUID 个人局域网服务1115        -- PANUServiceClass_UUID 人机输入服务1124     -- HumanInterfaceDeviceServiceClass_UUID 1125   -- HardcopyCableReplacementServiceClass_UUID

经典蓝牙的绑定和通信

  1. 蓝牙的可被搜索和扫描
  2. 首先我们要绑定后才能正常通信
  3. 通信我们这里我们是通过socket来传输文件

服务端设置可被搜索/客户端开始扫描

一、服务端设置可被搜索和监听广播

  1. 首先使能蓝牙

     BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();if (!adapter.isEnabled()) {adapter.enable();}
    
  2. 设置可被搜索

    通过反射调动的setScanMode

     public static void setDiscoverableTimeout() {try {Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);setDiscoverableTimeout.setAccessible(true);// Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class);Method setScanMode = BluetoothAdapter.class.getMethod("setScanMode", int.class);setScanMode.setAccessible(true);setDiscoverableTimeout.invoke(adapter, 0);// Object state = setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 300);Object state = setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);Log.d(TAG, "setDiscoverableTimeout setScanMode:" + state);} catch (Exception e) {e.printStackTrace();Log.e(TAG, "setDiscoverableTimeout failure:" + e.getMessage());}}
    
  3. 服务端监听状态变化广播

二、客户端启动扫描和监听广播

  1. 首先也要先打开蓝牙(和上面方法相同)

  2. 开始扫描蓝牙

     if (!mBlueAdapter.isDiscovering()) {Log.d(TAG, "startDiscovery");mBlueAdapter.startDiscovery();}
    
  3. 服务端监听状态变化广播

    主要是为了获取绑定状态,连接状态,扫描返回,绑定请求

     IntentFilter filter = new IntentFilter();filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙开关状态filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//蓝牙开始搜索filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//蓝牙搜索结束filter.addAction(BluetoothDevice.ACTION_FOUND);//蓝牙发现新设备(未配对的设备)filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);//在系统弹出配对框之前(确认/输入配对码)filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//设备配对状态改变filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//最底层连接建立filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//最底层连接断开filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); //BluetoothAdapter连接状态filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); //BluetoothHeadset连接状态filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); //BluetoothA2dp连接状态context.registerReceiver(this, filter);
    

扫描到蓝牙后开始绑定

一、客户创建绑定和确认绑定

  1. 扫描到蓝牙后–创建绑定

     public void onReceive(Context context, Intent intent) {switch (intent.getAction()) {case BluetoothDevice.ACTION_FOUND:// 判断符合自己想要的蓝牙后,创建绑定BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);creatBond(device.getClass(), device);break;}}private void creatBond(Class bluetoothClass, BluetoothDevice device) {try {Method createBondMethod = bluetoothClass.getMethod("createBond");Boolean value = (Boolean) createBondMethod.invoke(device);Log.d(TAG, "creatBond return " + value);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}
    
  2. 配对请求

     public void onReceive(Context context, Intent intent) {switch (intent.getAction()) {case BluetoothDevice.ACTION_PAIRING_REQUEST:BluetoothDevice device_bond= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);if (BluetoothDevice.BOND_NONE == type) {abortBroadcast();//终止配对广播,取消系统配对框// 注意setPairingConfirmation的调用需要系统权限device.setPairingConfirmation(true);device.setPin(intToByteArray(pairingKey));}break;}}
    

二、服务端确认绑定

  1. 服务端收到广播后绑定

     public void onReceive(Context context, Intent intent) {switch (intent.getAction()) {case BluetoothDevice.ACTION_PAIRING_REQUEST:BluetoothDevice device_bond= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);if (BluetoothDevice.BOND_NONE == type) {abortBroadcast();//终止配对广播,取消系统配对框// 注意setPairingConfirmation的调用需要系统权限device.setPairingConfirmation(true);device.setPin(intToByteArray(pairingKey));}break;}}
    

绑定后开始连接

注意 SPP_UUID 为自定义的 uuid,保持客户端和服务端一致就好

public static final UUID SPP_UUID = UUID.fromString("0000ff01-0000-1000-8000-00805F9B34FB");

一、服务端监听

  1. 初始化服务端

     BluetoothServerSocket mSerSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(TAG, SPP_UUID); //明文传输BluetoothSocket socket = mSocket.accept(); // 监听连接,阻塞
    

二、客户端连接

  1. 创建连接

     BluetoothSocket mBlueSocket = mDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID);try {mBlueSocket.connect();dataOut = new DataOutputStream(mBlueSocket.getOutputStream());} catch (IOException e) {e.printStackTrace();}
    
  2. 根据广播收到的连接成功-代表连接成功

数据传输

那么这里就是利用socket传输数据
输出,拿到输出流

 dataOut = new DataOutputStream(mBlueSocket.getOutputStream());

读取,拿到输入流

 dataIn = new DataInputStream(socket.getInputStream());

Android--经典蓝牙(文件传输--socket通信)相关推荐

  1. Android经典蓝牙开发全流程

    一.基本介绍   所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,最初是由爱立信公司公司发明的.技术始于爱立信公司 1994 方案,它是研究在移动电话和其他配件间进行低功耗.低成本无 ...

  2. Android 经典蓝牙开发

    本文主要讲解经典蓝牙的开发,主要包含以下几个知识点: 蓝牙 API 简介 经典蓝牙开发的一般步骤 相信通过以上步骤,您会很快上手一个 Android 经典蓝牙开发的 App. 蓝牙 API 简介 An ...

  3. 蓝牙文件传输服务linux,openwrt蓝牙文件传输

    openwrt支持: USB Supprot------------------------------- kmod-usb-ohci kmod-usb-uhci kmod-usb-storage-e ...

  4. Android java和C的Socket通信demo(可用)

    原址 关于Android应用与Framework的socket通信,相信关心这个问题的朋友们已经看过<android使用socket使底层和framework通信>这篇文章,美中不足的是作 ...

  5. bluez5.50蓝牙文件传输

    蓝牙文件传输主要用到 bluez 下 obex 协议支持,需要在启动 bluetoothd 蓝牙守护进程后再启动 obexd 文件传输服务进程,关于蓝牙协议栈可以参考以下图片: bluez 5.50 ...

  6. Mac系统遇到蓝牙文件传输失败不支持要怎么办

    Mac系统遇到蓝牙文件传输失败不支持的项该怎么办?苹果设备在使用蓝牙传输文件时候,总是失败,该怎么解决这个问题呢?在使用Mac的蓝牙传文件,有些文件无法传输,它提示:文件传输失败不支持的项目或操作,那 ...

  7. 蓝牙文件传输失败不支持的解决方法!

    用苹果的朋友你有没有遇到过这种情况呢:在使用蓝牙传输文件时候,显示失败!那么如何解决呢?来和我一起看看吧! 在使用Mac的蓝牙传文件,有些文件无法传输,它提示:文件传输失败不支持的项目或操作,那么该怎 ...

  8. android蓝牙文件传输的实现

    一.android设备蓝牙通信介绍 1.1配对 两个蓝牙设备在建立通信连接之前需要先彼此感知到对方的存在,这一过程就是配对.使用android蓝牙api进行配对分为以下几步: 1.设备A与B均开始蓝牙 ...

  9. Android经典蓝牙开发简介(Google官网译文)

    公司的项目最近需要用到蓝牙开发的相关内容,因此特地查阅了Google官方文档的内容并进行二次整理,希望能对需要学习该部分的朋友有所帮助. 原文地址:http://developer.android.c ...

最新文章

  1. 022_配置configuration
  2. java中List与Map的使用
  3. mysql怎么对比表结构_mysql查看表结构2种方式对比
  4. mysql blob图片_显示存储在mysql blob中的图像
  5. TypeScript学习(七):类型断言
  6. http请求被挂起 cancled 原因
  7. ISO 27001信息安全管理体系认证
  8. U盘文件夹被隐藏能够解决方法
  9. YGG Pilipinas: 台风奥黛特救灾工作更新
  10. 注册Gmail邮箱需要手机验证
  11. 计算机行业绩效管理,【计算机仿真论文】计算机信息系统在绩效管理中的应用(共4413字)...
  12. Springboot启动报错-类文件具有错误的版本 61.0, 应为 52.0
  13. 【Unity国际版下载地址】
  14. < pre >标签 定义预格式化的文本
  15. SQL审核 | SQLE 二次开发环境搭建
  16. 利用百度poi计算城市能级及评价街道范围便利度、生活宜居情况等
  17. C#读写导入导出Excel表格模板(NPOI)
  18. 分享一下最近数学竞赛的获奖经历
  19. 超详细Shell学习教程第二篇
  20. 一位年薪35W的测试被开除,回怼的一番话,令人沉思

热门文章

  1. BigGAN代码解读(gpt3.5帮助)——生成器部分
  2. oop 编程是什么?
  3. Python求各科成绩差与平均数
  4. 《面试知识,工作可待:集合篇》-java集合面试知识大全
  5. 数据结构排序算法之归并排序(c语言实现)
  6. 解决tensorflow 调用bug Running model failed:Invalid argument: NodeDef mentions attr 'dilations'
  7. npm引入小程序官方WeUI组件库weui-miniprogram
  8. 编程参考 - va_list的定义问题
  9. 转载:D3D9的更新 关于devicelost
  10. 【案例分享】项目施工进度报告 - 树形报表