当前位置: 首页>>代码示例>>Java>>正文


Java BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE属性代码示例

本文整理汇总了Java中android.bluetooth.BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE属性的典型用法代码示例。如果您正苦于以下问题:Java BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE属性的具体用法?Java BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE怎么用?Java BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在android.bluetooth.BluetoothGattDescriptor的用法示例。


在下文中一共展示了BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setCharacteristicNotification

/**
     * Enables or disables notification on a give characteristic.
     *
     * @param characteristic Characteristic to act on.
     * @param enabled If true, enable notification.  False otherwise.
     */
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        Log.d(TAG,"Notification set for " + characteristic.getUuid().toString() + " : " + String.valueOf(enabled));

        // step 1 - enable notifications locally
        boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        if(!success) {
            Log.e("------", "Seting proper notification status for characteristic failed!");
        }

        //step 2 - write to Clienc Characteristic Config Descriptor (2902) proper values
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        if(descriptor != null) {
//            byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
            byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
            descriptor.setValue(val);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }
 
开发者ID:smartlockpicking,项目名称:hackmelock-android,代码行数:29,代码来源:BluetoothLeService.java

示例2: onDescriptorReadRequest

@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset,
                                    BluetoothGattDescriptor descriptor) {
    if (TimeProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
        Log.d(TAG, "Config descriptor read");
        byte[] returnValue;
        if (mRegisteredDevices.contains(device)) {
            returnValue = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
        } else {
            returnValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
        }
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                returnValue);
    } else {
        Log.w(TAG, "Unknown descriptor read request");
        mBluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_FAILURE,
                0,
                null);
    }
}
 
开发者ID:androidthings,项目名称:sample-bluetooth-le-gattserver,代码行数:25,代码来源:GattServerActivity.java

示例3: start

@Override
protected void start(Connection connection, BluetoothGatt gatt) {
    if (type == Type.UNSUBSCRIBE && connection.getCharacteristicsChangedListenerCount(characteristicUUID) > 0) {
        NeatleLogger.d("Won't unsubscribe on " + characteristicUUID + " since it has registered listeners");
        finish(CommandResult.createEmptySuccess(characteristicUUID));
        return;
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    if (service == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, SERVICE_NOT_FOUND));
        return;
    }

    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
    if (characteristic == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, CHARACTERISTIC_NOT_FOUND));
        return;
    }

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    if (descriptor == null) {
        finish(CommandResult.createErrorResult(characteristicUUID, DESCRIPTOR_NOT_FOUND));
        return;
    }

    boolean turnOn;
    byte[] valueToWrite;
    switch (type) {
        case Type.SUBSCRIBE_INDICATION:
            NeatleLogger.d("Subscribing to indications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
            turnOn = true;
            break;
        case Type.SUBSCRIBE_NOTIFICATION:
            NeatleLogger.d("Subscribing to notifications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
            turnOn = true;
            break;
        case Type.UNSUBSCRIBE:
            NeatleLogger.d("Unsubscribing from notifications/indications on  " + characteristicUUID);
            valueToWrite = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
            turnOn = false;
            break;
        default:
            throw new IllegalStateException();
    }

    byte[] descriptorValue = descriptor.getValue();
    if (Arrays.equals(descriptorValue, valueToWrite)) {
        NeatleLogger.d("No subscription changes needed - is at  " + valueToWrite[0] + " on " + characteristicUUID);
        finish(CommandResult.createEmptySuccess(characteristicUUID));
        return;
    }

    if (!gatt.setCharacteristicNotification(characteristic, turnOn)) {
        NeatleLogger.e("Failed to change characteristics notification flag on " + characteristicUUID);
        finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
        return;
    }

    descriptor.setValue(valueToWrite);
    NeatleLogger.e("Writing descriptor on " + characteristicUUID);
    if (!gatt.writeDescriptor(descriptor)) {
        NeatleLogger.e("Failed to write descriptor on " + characteristicUUID);
        finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
    }
}
 
开发者ID:inovait,项目名称:neatle,代码行数:68,代码来源:SubscribeCommand.java


注:本文中的android.bluetooth.BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。