zoukankan      html  css  js  c++  java
  • 【转】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 :)

  • 相关阅读:
    杭电oj2032、2040、2042、2054、2055
    杭电oj2022-2030
    杭电oj2012-2021
    杭电oj2000-2011
    值得思考的几句话,放在这看看
    CEO 是一家创业公司的天花板
    致敬骄傲的产品人
    【新业务搭建】竞争情报业务规划及体系构建的思考——By Team
    微软威胁情报中心总经理的十句话——From John Lambert——太精辟了.......
    【调研与分析】标杆学习、知识管理和竞争情报的关系——From Team
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4473383.html
Copyright © 2011-2022 走看看