【转】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 ...
随机推荐
- json 序列化的两种方式
JavaScriptSerializer Serializer = new JavaScriptSerializer(); ResultData<EUserData> resultMode ...
- android 高德地图API 之 java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLibrary returned null错误
错误场景: 运行android app时,在运行到调用高德地图API时,出现 “java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLi ...
- Eclipse代码自动填充.
在默认情况下,Eclipse只在程序员输入“.”并用ALT+/组合键强制调用编码提示功能 我们可以通过少量配置,让Eclipse更聪明,实现完全自动编码提示:1.在你的“工作空间”下找到下在文件.me ...
- 关于webservice不支持方法重载的解决办法
今天在写WebService时,出现了这样的错误: Count(Int32, Int32) 和 Count(Int32) 同时使用消息名称“Count”.使用 WebMethod 自定义特性的 Mes ...
- PDF在线预览
1.所需插件jquery.media.js或者pdfobject.js 代码: <html> <head> <style type="text/css" ...
- iOS英文 汉化,如调用相册,相机改“cancel”,“photos”为“取消”,“相机”
调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文,纠结半天,最终在info.plist设置解决问题. 只需要改三个地方: 1.plist文件中: 2. ...
- HTML页面中启用360浏览器极速模式
今天做页面突然遇到浏览器一直在兼容模式下运行,体验不好,通过查询文档,在<head>中加入<meta name="renderer" content=" ...
- 【POJ2985】【Treap + 并查集】The k-th Largest Group
Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...
- javascript——函数内部属性
<script type="text/javascript"> //在函数内部有两个特殊的属性:arguments 和 this.arguments是一个类数组对象,包 ...
- Web::Scraper 页面提取分析
一组用来提取HTML文档中元素内容的工具集,它能够理解HTML和CSS选择器以及XPath表达式. 语法 use URI; use Web::Scraper; # First, create your ...