【转】Any way to implement BLE notifications in Android-L preview----不错
原文网址:http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l-preview
This question is not about Android notificatinos, but BLE notifications (as the title may hint)
I have got basic BLE peripheral mode working on Android-L
Is there any way to implement BLE notifications in Android-L preview. I could do some thing like the following to make a charecteritic be able to notify, but trying to listen for
BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(
UUID.fromString(serviceOneCharUuid),
BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );
But in LightBlue app on iOS I cannot subscribe to this characteristic. Apprently there is no API that could be use to respond to the calls when a char is subscribed (like there are in iOS)
Kindly share your code if you have successfully enabled BLE notifications on Android-L
On top of what OP has done:
BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(UUID.fromString(serviceOneCharUuid), BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ )
The next thing is to add a Client Characteristic Configuration descriptor (UUID is the 128 bit version of the 16 bit 0x2902 using the Bluetooth base UUID), so that the connected device can tell yours that it wants notifications (or indications), and then add that descriptor to your characteristic:
BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
firstServiceChar.addDescriptor(gD);
That UUID is from the Bluetooth spec. Apparently a device subscribes to notifications by updating this descriptor, so you've got to handle this in your BluetoothGattServerCallback by overriding onDescriptorWriteRequest:
@Override
public void onDescriptorWriteRequest (BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
btClient = device; // perhaps add to some kind of collection of devices to update?
// now tell the connected device that this was all successfull
btGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
Now update your value and notify the connectee:
firstServiceChar.setValue("HI");
btGattServer.notifyCharacteristicChanged(btClient, firstServiceChar, false);
Hopefully this quick and dirty code will help, because it was OP's code that I used in the first place to even get basic peripheral mode working :)
【转】Any way to implement BLE notifications in Android-L preview----不错的更多相关文章
- Android BLE开发之Android手机搜索iBeacon基站
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 上次讲了Android手机与BLE终端之间的通信,而最常见的BLE终端应该是苹果公司倡导的iBeacon基站. ...
- Android BLE开发之Android手机与BLE终端通信
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...
- BLE简介和Android BLE编程
一.BLE和BT区别 其实我知道许多程序员不太喜欢阅读除了代码以外的文档,因为有时这些过于冗长的文档对编程并没有更多的好处,有了协议,接口,demo差不多很多人就能写出很好质量的代码了.但其实更深入的 ...
- MQTT的学习研究(十五) MQTT 和android整合文章
详细参考: How to Implement Push Notifications for Android http://tokudu.com/2010/how-to-implement-push- ...
- Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key
Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 OCT 6, 2016 Push notifications are ...
- Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)
Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...
- 求android ble 解决方案!
智能医疗的产品,求ble解决方案:整体结构如下: 名词定义: 盒子:基于android4.3或以上版本的硬件,需支持wifi.ble 手机:android/ios 手机,用户使用 服务器:云服务器,盒 ...
- Android 蓝牙4.0 BLE
Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用. BLE是蓝牙4.0的核心Profil ...
- Android notifications通知栏的使用
app发送通知消息到通知栏中的关键代码和点击事件: package com.example.notifications; import android.os.Bundle; import androi ...
随机推荐
- struts2 OGNL 表达式
一.Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言:JSTL(JSP Standard ...
- cogs 2507 零食店
/* cogs 2507 零食店 跪了这题的数据了.... 第一遍Q*m 暴力询问 嗯 以为能的70 但只有40 Q已经到了1e6了 考试的时候 放弃了第三题又打了一遍 这次是Q*(n+logn) 最 ...
- weex APIs
1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...
- 实现Android K的伪沉浸式
在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯.而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的 ...
- Spring通过SchedulerFactoryBean实现调度任务的配置
http://blog.csdn.net/hu_shengyang/article/details/19815201(里面是配置) 介绍SchedulerFactoryBean http://blog ...
- Tomcat下work文件夹的作用
1.打补丁,重启tomcat时要删除work文件夹,有缓存. 2.work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录 jsp,tomcat的工作原理: 当 ...
- wpf 中DataGrid 控件的样式设置及使用
本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </ ...
- sublime text snippet代码片断
$0 代表补全代码后放的位置 0 的权重是最低的 $1 最高 也等于${1:} ${1: name} 1输入点的序号(1权重最高) name 自动补全的默认值 <conte ...
- SGU 171.Sarov zones
简单的贪心.优先weight最大的,优先匹配Q值大的地区 code #include <iostream> #include <algorithm> #include < ...
- PHPCMS V9调用时间标签 |日期时间格式化
PHPCMS V9 如何调用时间标签,下面分享常见的调用时间标签 |日期时间格式化 1.日期时间格式化显示: a标准型:{date('Y-m-d H:i:s', $rs['inputtime'])} ...