最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE。
Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了,
连接成功后在 onServicesDiscovered 中拿到 Service Characteristic Descriptor 就可以了。
不过还是遇到了几个小坑
第一: setCharacteristicNotification 时 onCharacteristicChanged 中收不到回调 原因是 BLE 不能连续执行写操作,必须等前一个
写完了再写一下个, 所以应该维护一个队列等前一个写完了再写下一个。

启用通知

BluetoothGattCharacteristic rxCharacteristic = uartService
.getCharacteristic(UUID.fromString(BleConst.UUID_RX_CHARACTERISTIC));
if (rxCharacteristic != null) {
BluetoothGattDescriptor rxCharacteristicDescriptor = rxCharacteristic
.getDescriptor(UUID.fromString(BleConst.UUID_RX_CHARACTERISTIC_DESCRIPTOR));
// 设置 Characteristic 可以接收通知
gatt.setCharacteristicNotification(rxCharacteristic, true);
if (rxCharacteristicDescriptor != null) {
// enable descriptor notification
rxCharacteristicDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(rxCharacteristicDescriptor);
}
}

第二:蓝牙自动配对
自动配对就是通过反射去调用 BluetoothDevice 的 createBond 方法

Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(btDevice);

第三:蓝牙自动连接
配对和连接是不一样的,自动配对后还要连接上蓝牙, stackoverflow 查了一些下面的方法可以自动连接 A2DP 和 HFP 两个 profile
监听 BluetoothDevice.ACTION_BOND_STATE_CHANGED 广播,蓝牙配对成功后调用下面的方法连接上两个 profile 就行了。

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
try {
Log.d(TAG, "a2dp onServiceConnected");
Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.invoke(proxy, device);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} @Override
public void onServiceDisconnected(int profile) {
Log.d(TAG, "a2dp onServiceDisconnected");
}
}, BluetoothProfile.A2DP); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
try {
Log.d(TAG, "hfp onServiceConnected");
Method connect = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.invoke(proxy, device);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} @Override
public void onServiceDisconnected(int profile) {
Log.d(TAG, "hfp onServiceDisconnected");
}
}, BluetoothProfile.HEADSET);

目前头戴所有按键走的都是蓝牙自定义协议,本来Android是可以用 HID 标准键值的, 但由于 IOS 不支持 HID 所已也走自定义协议。

Android 蓝牙 BLE 开发笔记的更多相关文章

  1. Android蓝牙BLE开发,扫描、连接、发送和读取信息;

    1.BLE开发权限 Android蓝牙BLE开发须打开蓝牙权限和6.0位置权限: <uses-permission android:name="android.permission.B ...

  2. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

  3. Android蓝牙——HID开发

    代码地址如下:http://www.demodashi.com/demo/13891.html 原文地址: https://blog.csdn.net/VNanyesheshou/article/de ...

  4. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...

  5. Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...

  6. Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例

    引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...

  7. Android移动APP开发笔记——最新版Cordova 5.3.1(PhoneGap)搭建开发环境

    引言 简单介绍一下Cordova的来历,Cordova的前身叫PhoneGap,自被Adobe收购后交由Apache管理,并将其核心功能开源改名为Cordova.它能让你使用HTML5轻松调用本地AP ...

  8. Android蓝牙BLE低功耗相关简单总结

    在看Android4.42的源代码时看到有加入对BLE设备的处理.看的一头雾水,多方百度,最终有种柳暗花明的感觉. 本文总结来源于百度多篇文章,欢迎转载.分享交流 BLE蓝牙概念 BLE:Blueto ...

  9. Qt on Android 蓝牙通信开发

    版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...

随机推荐

  1. Unity 3D学习之 Prime31 Game Center插件用法

    http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...

  2. 华为2013校招之哈工大威海 上机试题之一:报数问题:设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去直 到所有的人都出圈为止。现要打印出出圈次序。

    1.  报数游戏 问题描述: 设有N 个人围坐一圈并按顺时针方向从1 到N 编号,从第S个人开始进行1 到M报数,报 数到第 M个人时,此人出圈,再从他的下一个人重新开始1 到 M的报数,如此进行下去 ...

  3. meanshift和camshift

    参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/15/2398769.html 照着这位大神的代码运行了一下,发现meanshift的跟踪效 ...

  4. DICOM:DICOM3.0网络通信协议(续)

    转载:http://blog.csdn.net/zssureqh/article/details/44278693 题记: 近一年来一直坚持周末写博客,整理工作和闲暇之余的点点滴滴.对于新知识点.新技 ...

  5. InnoDB主键设计

    InnoDB是clustered-index table,因此对于InnoDB而言,主键具有特殊意义. 可以通过主键直接定位到对应的某一数据行记录的物理位置,主键索引指向对应行记录,其他索引则都指向主 ...

  6. Android 中PendingIntent---附带解决AlarmManager重复加入问题

    最近在程序中使用到了notification功能,自然,就涉及到了PendingIntent,下面总结下. 1 什么是PendingIntent A description of an Intent ...

  7. 在cmd命令行中弹出Windows对话框

    有时候用bat写一些小脚本最后会弹出对话框提示操作成功,可以用mshta.exe来实现,它是Windows系统的相关程序,用来执行.HTA文件,一般计算机上面都有这个程序,实现如下: mshta vb ...

  8. Java for LeetCode 046 Permutations

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...

  9. 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists

      Merge two sorted linked lists and return it as a new list. The new list should be made by splicing ...

  10. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...