android.bluetooth.BluetoothGattDescriptor Java Examples

The following examples show how to use android.bluetooth.BluetoothGattDescriptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BleWrapper.java    From BLEService with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setNotificationForCharacteristic(BluetoothGattCharacteristic ch, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) return;
    
    boolean success = mBluetoothGatt.setCharacteristicNotification(ch, enabled);
    if(!success) {
    	Log.e("------", "Seting proper notification status for characteristic failed!");
    }
    
    // This is also sometimes required (e.g. for heart rate monitors) to enable notifications/indications
    // see: https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
    BluetoothGattDescriptor descriptor = ch.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    if(descriptor != null) {
    	byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
     descriptor.setValue(val);
     mBluetoothGatt.writeDescriptor(descriptor);
    }
}
 
Example #2
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyDescriptor(){
    byte[] data = new byte[]{0x01, 0x02, 0x03};
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    BluetoothGattDescriptorCopy result = new GattUtils().copyDescriptor(descriptor);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for descriptor: %s, with data: %s", descriptor.getUuid(), Arrays.toString(descriptor.getValue())));
        return;
    }
    data[2] = 0x04;
    Assert.assertNotEquals(Arrays.hashCode(data), Arrays.hashCode(result.getValue()));
}
 
Example #3
Source File: RxBleConnectionMock.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@NonNull
private Completable setupCharacteristicNotification(
        final UUID bluetoothGattCharacteristicUUID,
        final NotificationSetupMode setupMode,
        final boolean enabled,
        final boolean isIndication
) {
    if (setupMode == NotificationSetupMode.DEFAULT) {
        final byte[] enableValue = isIndication ? ENABLE_INDICATION_VALUE : ENABLE_NOTIFICATION_VALUE;
        return getClientConfigurationDescriptor(bluetoothGattCharacteristicUUID)
                .flatMapCompletable(new Function<BluetoothGattDescriptor, Completable>() {
                    @Override
                    public Completable apply(BluetoothGattDescriptor bluetoothGattDescriptor) {
                        return writeDescriptor(bluetoothGattDescriptor, enabled ? enableValue : DISABLE_NOTIFICATION_VALUE);
                    }
                });
    } else {
        return Completable.complete();
    }

}
 
Example #4
Source File: BTLEDeviceManager.java    From BLEService with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
   * Enables or disables notification on a give characteristic.
   * @param characteristic Characteristic to act on.
   * @param enabled If true, enable notification.  False otherwise.
   * @param immediate setNotification immediately, don't enqueue
   */
  public void setCharacteristicNotification(BTDeviceInfo 					deviceInfo, 
  										  BluetoothGattCharacteristic 	characteristic,
                                            boolean 						enabled) throws DeviceManagerException {
  	Log.d(TAG, "setCharacteristicNotification " + deviceInfo.getDeviceAddress() + " name = " + deviceInfo.getDevice().getName());
      if (mBluetoothAdapter == null) { 
         	throw new DeviceManagerException(mContext.getString(R.string.adapter_uninitialized));
      }
      if (deviceInfo.getGatt() == null) {
          throw new DeviceManagerException(String.format("%s %s", mContext.getString(R.string.no_gatt_info), deviceInfo.getDeviceAddress()));
      }
      
      // if there are no notification requests outstanding, we can issue one right away.
      deviceInfo.enqueueCommand(new BTLECommandSetCharacteristicNotification(deviceInfo, characteristic, true));
      // this is some serious "guess the magic word".  You have to write this characteristic to receive notififcations.
      // setCharacteristicNotification() isn't enough.
      // from http://stackoverflow.com/questions/17910322/android-ble-api-gatt-notification-not-received
      BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(BTUUID.CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID));
      descriptor.setValue(enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
deviceInfo.enqueueCommand(new BTLECommandWriteDescriptor(deviceInfo, descriptor));
  }
 
Example #5
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyDescriptorWithNullValue(){
    byte[] data = null;
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    BluetoothGattDescriptorCopy result = new GattUtils().copyDescriptor(descriptor);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for descriptor: %s, with data: %s", descriptor.getUuid(), Arrays.toString(descriptor.getValue())));
        return;
    }
    Assert.assertNull(result.getValue());
}
 
Example #6
Source File: BleService.java    From bleTester with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
private void getChartacteristicValue(
		BluetoothGattCharacteristic characteristic) {
	// TODO Auto-generated method stub
	List<BluetoothGattDescriptor> des = characteristic.getDescriptors();
	Intent mIntent = new Intent(ACTION_CHAR_READED);
	if (des.size() != 0) {
		mIntent.putExtra("desriptor1", des.get(0).getUuid().toString());
		mIntent.putExtra("desriptor2", des.get(1).getUuid().toString());
	}
	mIntent.putExtra("StringValue", characteristic.getStringValue(0));
	String hexValue = Utils.bytesToHex(characteristic.getValue());
	mIntent.putExtra("HexValue", hexValue.toString());
	mIntent.putExtra("time", DateUtil.getCurrentDatatime());
	sendBroadcast(mIntent);
}
 
Example #7
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyCharacteristicWithDescriptorChild(){
    byte[] data = null;
    BluetoothGattCharacteristic characteristic = mock(BluetoothGattCharacteristic.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(characteristic.getValue()).thenReturn(data);
    when(characteristic.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(characteristic.getPermissions()).thenReturn(BluetoothGattCharacteristic.PERMISSION_READ);
    when(characteristic.getProperties()).thenReturn(BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    characteristic.addDescriptor(descriptor);
    BluetoothGattCharacteristicCopy result = new GattUtils().copyCharacteristic(characteristic);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for characteristic: %s, with data: %s", characteristic.getUuid(), Arrays.toString(characteristic.getValue())));
        return;
    }
    Assert.assertNotEquals(descriptor, characteristic.getDescriptor(descriptor.getUuid()));
}
 
Example #8
Source File: MultipleBleService.java    From BleLib with Apache License 2.0 6 votes vote down vote up
/**
 * Enables or disables notification on a give characteristic.
 *
 * @param address        The address.
 * @param characteristic Characteristic to act on.
 * @param enabled        If true, enable notification.  False otherwise.
 */
public void setCharacteristicNotification(String address,
                                          BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGattMap.get(address) == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGattMap.get(address).setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString(GattAttributes.DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION));
    descriptor.setValue(enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
            BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    mBluetoothGattMap.get(address).writeDescriptor(descriptor);
}
 
Example #9
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}
 
Example #10
Source File: BlePeripheral.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
public void characteristicDisableNotify(@NonNull final BluetoothGattCharacteristic characteristic, CompletionHandler completionHandler) {
    final String identifier = getCharacteristicIdentifier(characteristic);
    BleCommand command = new BleCommand(BleCommand.BLECOMMANDTYPE_SETNOTIFY, kDebugCommands ? identifier : null, completionHandler) {

        @Override
        public void execute() {

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(kClientCharacteristicConfigUUID);
            if (mBluetoothGatt != null && descriptor != null && (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                mNotifyHandlers.remove(identifier);
                descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descriptor);
            } else {
                Log.w(TAG, "disable notify: client config descriptor not found for characteristic: " + characteristic.getUuid().toString());
                finishExecutingCommand(BluetoothGatt.GATT_FAILURE);
            }
        }
    };
    mCommmandQueue.add(command);
}
 
Example #11
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyDisableTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.setNotify(characteristic, false);
    verify(gatt).setCharacteristicNotification(characteristic, false);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);

    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example #12
Source File: DescriptorWriteOperation.java    From tap-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public void onExecute(@NonNull BluetoothGatt gatt) {
    BluetoothGattDescriptor d = extractDescriptor(gatt);
    if (d == null) {
        postOnError(ErrorStrings.NO_DESCRIPTOR);
        return;
    }

    if (!d.setValue(data)) {
        postOnError(ErrorStrings.VALUE_STORE_FAIL);
        return;
    }

    if (!gatt.writeDescriptor(d)) {
        postOnError(ErrorStrings.WRITE_OP_INIT_FAIL);
    }
}
 
Example #13
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyIndicationTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    peripheral.setNotify(characteristic, true);
    verify(gatt).setCharacteristicNotification(characteristic, true);
    assertEquals(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);
    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example #14
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean internalEnableNotifications(final BluetoothGattCharacteristic characteristic) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || characteristic == null)
		return false;

	// Check characteristic property
	final int properties = characteristic.getProperties();
	if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
		return false;

	gatt.setCharacteristicNotification(characteristic, true);
	final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
	if (descriptor != null) {
		descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		return internalWriteDescriptorWorkaround(descriptor);
	}
	return false;
}
 
Example #15
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean internalEnableNotifications(@Nullable final BluetoothGattCharacteristic characteristic) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || characteristic == null || !connected)
		return false;

	final BluetoothGattDescriptor descriptor = getCccd(characteristic, BluetoothGattCharacteristic.PROPERTY_NOTIFY);
	if (descriptor != null) {
		log(Log.DEBUG, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
		gatt.setCharacteristicNotification(characteristic, true);

		descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		log(Log.VERBOSE, "Enabling notifications for " + characteristic.getUuid());
		log(Log.DEBUG, "gatt.writeDescriptor(" +
				BleManager.CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
		return internalWriteDescriptorWorkaround(descriptor);
	}
	return false;
}
 
Example #16
Source File: MusicControlActivity.java    From android_wear_for_ios with MIT License 6 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    Log.d(TAG_LOG, " onDescriptorWrite:: " + status);
    // Notification source
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG_LOG, "status: write success ");
        //find music controll service
        Log.d(TAG_LOG, "*+*+*+*+*+*+*+*+*+*+ find music control");
        BluetoothGattService service = bluetooth_gatt.getService(UUID.fromString(service_ams));
        if(service != null) {
            BluetoothGattCharacteristic chara = service.getCharacteristic(UUID.fromString(characteristics_entity_update));
            if(chara != null) {
                chara.setValue(new byte[]{(byte) 0x02, (byte) 0x00, (byte) 0x02});
                bluetooth_gatt.writeCharacteristic(chara);
            }
        }
    }
}
 
Example #17
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will return the copy of the service
 *
 * @param service The gatt service
 * @return a shallow-ish copy of the service
 */

public @Nullable
BluetoothGattServiceCopy copyService(@Nullable BluetoothGattService service) {
    if (null == service || null == service.getUuid()) {
        return null;
    }
    BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(service.getUuid(), service.getType());
    if (!service.getIncludedServices().isEmpty()) {
        for (BluetoothGattService includedService : service.getIncludedServices()) {
            BluetoothGattServiceCopy newGattService = new BluetoothGattServiceCopy(includedService.getUuid(), includedService.getType());
            newService.addService(newGattService);
        }
    }
    if (!service.getCharacteristics().isEmpty()) {
        // why not use the copy characteristic method, it will implicitly link itself to the null service
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            BluetoothGattCharacteristicCopy newCharacteristic = new BluetoothGattCharacteristicCopy(characteristic.getUuid(), characteristic.getProperties(), characteristic.getPermissions());
            if (characteristic.getValue() != null) {
                newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
            }
            // why not use the copy descriptor method?  It will implicitly link itself to the null characteristic
            for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
                if (descriptor.getValue() != null) {
                    newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
                }
                newCharacteristic.addDescriptor(newDescriptor);
            }
            newService.addCharacteristic(newCharacteristic);
        }
    }
    return newService;
}
 
Example #18
Source File: BleUtils.java    From thunderboard-android with Apache License 2.0 5 votes vote down vote up
public static boolean setCharacteristicNotification(BluetoothGatt gatt, UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, boolean enable) {
    if (gatt == null) {
        return false;
    }
    BluetoothGattService service = gatt.getService(serviceUuid);
    if (service == null) {
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);

    if (characteristic == null) {
        Timber.d("could not get characteristic: %s for service: %s", characteristicUuid.toString(), serviceUuid.toString());
        return false;
    }

    if (!gatt.setCharacteristicNotification(characteristic, true)) {
        Timber.d("was not able to setCharacteristicNotification");
        return false;
    }

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
    if (descriptor == null) {
        Timber.d("was not able to getDescriptor");
        return false;
    }

    if (enable) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    } else {
        descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    }
    return gatt.writeDescriptor(descriptor);
}
 
Example #19
Source File: ShareTest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    Log.i(TAG, "Characteristic setting notification");
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    Log.i(TAG, "UUID FOUND: " + characteristic.getUuid());
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG));
    Log.i(TAG, "Descriptor found: " + descriptor.getUuid());
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}
 
Example #20
Source File: DexShareCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) {
    Log.w(TAG, "Characteristic setting indication");
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG));
    Log.w(TAG, "Descriptor found: " + descriptor.getUuid());
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}
 
Example #21
Source File: BLEUtils.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
public static boolean SetNotificationForCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, UUID gattDescriptor, Notifications value) {
    boolean written = false;
    if (characteristic != null) {
        gatt.setCharacteristicNotification(characteristic, value.isEnabled());
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(gattDescriptor);
        if (descriptor != null) {
            //writing this descriptor causes the device to send updates
            descriptor.setValue(value.getDescriptorValue());
            written = gatt.writeDescriptor(descriptor);
        }
        return written;
    }

    return false;
}
 
Example #22
Source File: BluetoothLeService.java    From SensorTag-CC2650 with Apache License 2.0 5 votes vote down vote up
public boolean isNotificationEnabled(
    BluetoothGattCharacteristic characteristic) {
       if (characteristic == null) {
           return false;
       }
	if (!checkGatt())
		return false;

	BluetoothGattDescriptor clientConfig = characteristic
	    .getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);
	if (clientConfig == null)
		return false;

	return clientConfig.getValue() == BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
}
 
Example #23
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean internalReadDescriptor(@Nullable final BluetoothGattDescriptor descriptor) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || descriptor == null || !connected)
		return false;

	log(Log.VERBOSE, "Reading descriptor " + descriptor.getUuid());
	log(Log.DEBUG, "gatt.readDescriptor(" + descriptor.getUuid() + ")");
	return gatt.readDescriptor(descriptor);
}
 
Example #24
Source File: GattClient.java    From bean-sdk-android with MIT License 5 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status != BluetoothGatt.GATT_SUCCESS) {
        disconnect();
        return;
    }
    fireDescriptorWrite(descriptor);
    executeNextOperation();
}
 
Example #25
Source File: G5CollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public void getSensorData() {
    android.util.Log.i(TAG, "Request Sensor Data");
    mGatt.setCharacteristicNotification(controlCharacteristic, true);
    BluetoothGattDescriptor descriptor = controlCharacteristic.getDescriptor(BluetoothServices.CharacteristicUpdateNotification);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    SensorTxMessage sensorTx = new SensorTxMessage();
    controlCharacteristic.setValue(sensorTx.byteSequence);
    mGatt.writeDescriptor(descriptor);
}
 
Example #26
Source File: UartService.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enable Notification on TX characteristic
 *
 * @return 
 */
public void enableTXNotification()
{ 

	if (mBluetoothGatt == null) {
		showMessage("mBluetoothGatt null: " + mBluetoothGatt);
		broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
		return;
	}
	BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
	if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
	BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        showMessage("Tx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar,true);
    
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
	
}
 
Example #27
Source File: BluetoothConnectInterface.java    From AndroidBleManager with Apache License 2.0 5 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);
    Logger.i("onDescriptorWrite status=" + GattError.parseConnectionError(status));
    mOpratorQueue.nextOperator();
    if (getBluetoothGattCallback() != null) getBluetoothGattCallback().onDescriptorWrite(gatt, descriptor, status);
}
 
Example #28
Source File: BaseBleService.java    From bleYan with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
		BluetoothGattDescriptor descriptor, int status) {
	BleLog.i(TAG, "onDescriptorWrite: " + BleUtils.getGattStatus(status));
	UUID uuid = descriptor.getUuid();

	sendBleMessage(BleConstants.MSG_BLE_ID_DESCRIPTOR_WRITE, status, uuid);
	onNextWrite();
}
 
Example #29
Source File: AndroidBle.java    From GizwitsBLE with Apache License 2.0 5 votes vote down vote up
public void onDescriptorWrite(BluetoothGatt gatt,
		BluetoothGattDescriptor descriptor, int status) {
	String address = gatt.getDevice().getAddress();
	Log.d(TAG, "onDescriptorWrite " + address + " status " + status);
	BleRequest request = mService.getCurrentRequest();
	if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION
			|| request.type == RequestType.CHARACTERISTIC_INDICATION
			|| request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) {
		if (status != BluetoothGatt.GATT_SUCCESS) {
			mService.requestProcessed(address,
					RequestType.CHARACTERISTIC_NOTIFICATION, false);
			return;
		}
		if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) {
			mService.bleCharacteristicNotification(address, descriptor
					.getCharacteristic().getUuid().toString(), true,
					status);
		} else if (request.type == RequestType.CHARACTERISTIC_INDICATION) {
			mService.bleCharacteristicIndication(address, descriptor
					.getCharacteristic().getUuid().toString(), status);
		} else {
			mService.bleCharacteristicNotification(address, descriptor
					.getCharacteristic().getUuid().toString(), false,
					status);
		}
		return;
	}
}
 
Example #30
Source File: BLEServicePeripheral.java    From unity-bluetooth with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BLEServicePeripheral(final Activity activity) {
    super(activity);

    mBtAdvertiser = mBtAdapter.getBluetoothLeAdvertiser();
    mBtGattCharacteristic = new BluetoothGattCharacteristic(
            UUID.fromString(CHARACTERISTIC_UUID),
            BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE
            , BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ);
    BluetoothGattDescriptor dataDescriptor = new BluetoothGattDescriptor(
            UUID.fromString(CHARACTERISTIC_CONFIG_UUID),
            BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
    mBtGattCharacteristic.addDescriptor(dataDescriptor);
}