"Go" any-to-implement BLE notifications in Android-l preview----good

Source: Internet
Author: User

Original URL: Http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l-preview

This question are not on Android Notificatinos, but BLE notifications (as the title may hint)

I have got basic BLE peripheral mode working on Android-l

Is there an any-to-implement BLE notifications in Android-l preview. I could do some thing like the following to make a charecteritic is able to notify, but trying to listen for

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(        UUID.fromString(serviceOneCharUuid),                BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );

But the LightBlue app on IOS I cannot subscribe to this characteristic. apprently There is no API this could be use for respond to the calls when a char was subscribed (like there am in IOS)

Kindly share your code if you had successfully enabled BLE notifications on Android-l

On top of the What OP have 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-the-bit version of the-the-bit 0x290 2 using the Bluetooth base UUID), so, the connected device can tell yours it wants notifications (or indications) , and then add this 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 was from the Bluetooth spec. Apparently a device subscribes to notifications by updating this descriptor ve got to handle the your bluetoothgattservercallback by overriding Ondescriptorwriterequest:

@Overridepublic 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 would help, because it is OP's code that I used in the first place to even get basic p Eripheral mode working:)

"Go" any-to-implement BLE notifications in Android-l preview----good

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.