set Bluetooth Characteristic Notification - Android Bluetooth

Android examples for Bluetooth:Bluetooth State

Description

set Bluetooth Characteristic Notification

Demo Code

/*/*w w  w  .  j  a v a  2s.c o  m*/
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * version 3 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * Please contact Integreight, Inc. at info@integreight.com or post on our
 * support forums www.1sheeld.com/forum if you need additional information
 * or have any questions.
 */
//package com.java2s;
import android.annotation.TargetApi;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;

import android.os.Build;

import java.util.UUID;

public class Main {
    static final UUID DEVICE_CONFIG_CHARACTERISTIC = UUID
            .fromString("00002902-0000-1000-8000-00805f9b34fb");

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    static boolean setCharacteristicNotification(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic, boolean isEnabled) {
        if (characteristic != null) {
            gatt.setCharacteristicNotification(characteristic, isEnabled);
            BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(DEVICE_CONFIG_CHARACTERISTIC);
            if (descriptor != null) {
                descriptor
                        .setValue(isEnabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                                : new byte[] { 0x00, 0x00 });
                return gatt.writeDescriptor(descriptor);
            } else
                return false;
        } else
            return false;
    }
}

Related Tutorials