【转】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 :)

时间: 2024-11-12 13:11:49

【转】Any way to implement BLE notifications in Android-L preview----不错的相关文章

低功耗蓝牙(BLE)在 Android APP 中的应用

低功耗蓝牙(BLE)在 Android APP 中的应用 前言 最近公司接了一个新项目,用户可以把自己的乐器跟Phone或Pad连接起来,当弹奏乐器的时候,会把演奏情况同步反馈到设备上,方便用户练习,有点类似于之前玩过的一款叫[ 吉他英雄 ]的游戏.不过这次不用插线,直接蓝牙无线连接就可以了. 那么问题来了,因为弹奏的时候数据传输一直在进行,但是如果要一直打开蓝牙的话是很费电的,也许没几首曲子下来设备的电量就耗掉了不少,这当然是无法接受的.那有没有什么好的解决方案呢? 运气真好,Android在

android L BLE Central(Scanner)牛刀小试

转载请表明作者:http://blog.csdn.net/lansefeiyang08/article/details/46482073 昨天写了android L BLE Peripheral的简单使用,今天讲一下BLE Central的更新. 搞过android4.4的人肯定对BluetoothAdapter的startLeScan函数不会陌生,但是在android L已经弃用此接口,但是为了兼容以前的版本,这个接口还是可以使用的.但是谷歌已经单独拿出来android.bluetooth.l

BLE蓝牙在Android开发中的应用

最近一段时间在写支持BLE蓝牙的Android应用.是时候总结一下了. 1.什么是BLE.(总得先知道BLE是什么吧~~~) Bluetooth Low Energy(低功耗蓝牙),缩写为Bluetooth LE,或BLE,作为蓝牙4.0 (有时称为蓝牙智能)规范的一部分,并针对上述的这些具体问题而被引入.就提高电池寿命而言,许多制造商声称一些传感器能维持数月甚至数年的时间(我必须承认我有点怀疑制造商的估计一般是基于最好的情况下,而不涉及实际的使用情况).Google在Android 4.3(A

android L BLE Peripheral牛刀小试

知道Android L对蓝牙对了一些改进,包括添加A2dp sink.HFP client.BLE Peripheral功能等等. 我花了一天多时间对Android L BLE Peripheral SDK进行了研究,网上的资料很少,有一个介绍的还不够清晰,所以就自己写了一个测试应用,希望可以对理解BLE Peripheral有一定的帮助. 此贴主要以讲解代码为主,我会把项目代码也传到CSDN中,帮助大家测试. 首先说明一点,并不是Android L的系统就可以支持BLE Peripheral,

【转】Android BLE开发之Android手机与BLE终端通信

今天看了一下android 蓝牙BLE 记录一下 原创作者连接: http://blog.csdn.net/hellogv/ 最近穿戴设备发展得很火,把相关技术也带旺了,其中一项是BLE(Bluetooth Low Energy).BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备.Android 4.3才开始支持BLE API,所以请各位客官把本文代码运行在蓝牙4.0和Androi

Android BLE开发之Android手机搜索iBeacon基站

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 上次讲了Android手机与BLE终端之间的通信,而最常见的BLE终端应该是苹果公司倡导的iBeacon基站.iBeacon技术基于BLE,它的特点是通过广播对外发送消息,手机不需要连上iBeacon基站也能获取它的信息,目前主要用来做室内定位和营销信息推送,在BLE发出的广播里带上带上特定的信息从而被识别为iBeacon.在iOS里面使用iBeacon要经过passbook注册iBeacon的UUID和对

BLE简介和Android BLE编程

一.BLE和BT区别 其实我知道许多程序员不太喜欢阅读除了代码以外的文档,因为有时这些过于冗长的文档对编程并没有更多的好处,有了协议,接口,demo差不多很多人就能写出很好质量的代码了.但其实更深入的编程是少了阅读的,阅读文档可以解决很多编程中遇到的困难,比如在大数据大流量情况下,很多正常的程序会表现出不可靠的一面,这已经不是够编程能解决的了,硬件的配置,服务器带宽,使用的数据库,调用的接口都有可能是瓶颈.比如BLE,同样的蓝牙,但却有着本质区别,一个表现就是不是所有蓝牙设备都支持BLE,编程如

MQTT的学习研究(十五) MQTT 和android整合文章

详细参考:  How to Implement Push Notifications for Android http://tokudu.com/2010/how-to-implement-push-notifications-for-android/ Push notifications for mobile apps http://dalelane.co.uk/blog/?p=938 源代码地址: https://github.com/tokudu/AndroidPushNotificati

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 a great way to ensure your users re-engage with your app every once in a while, but implementing them on iOS can be challenging, especially with all o