**For Write characterstics Steps :
1. First you have to do indicate
2. After executing indication onDecriptor write sucessfull is executed.Here you have to start write characterstics.**
// for indicate the follwing code is in BluetoothLEService
public void indicateCharacteristic(UUID serviceUUID, UUID characteristicUuid, boolean isIndicate) {
try {
UUID serviceuid = serviceUUID;
if (serviceuid != null && mBluetoothGatt != null) {
BluetoothGattService service = mBluetoothGatt.getService(serviceuid);
UUID characteristicuid = characteristicUuid;
BluetoothGattCharacteristic characteristic = null;
if (service != null) {
characteristic = service.getCharacteristic(characteristicuid);
//Enable local notifications
if (mBluetoothGatt != null) {
mBluetoothGatt.setCharacteristicNotification(characteristic, isIndicate);
ArrayList<BluetoothGattDescriptor> gattdescriptor = (ArrayList<BluetoothGattDescriptor>) characteristic
.getDescriptors();
for (int k = 0; k < gattdescriptor.size(); k++) {
BluetoothGattDescriptor descriptor = gattdescriptor.get(k);
if (descriptor.getUuid().toString().equalsIgnoreCase(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG))
writeGattDescriptorForIndication(descriptor, isIndicate);
}
}
}
}
} catch (Exception e) {
Log.d("device", "not found");
}
}
// Write gatt descriptor
public void writeGattDescriptorForIndication(BluetoothGattDescriptor d, boolean isIndicate) {
boolean test;
if (isIndicate) {
d.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
d.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else {
d.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
// test = mBluetoothGatt.readDescriptor(d); // return value = true
// Log.d("test",""+test);
test = mBluetoothGatt.writeDescriptor(d);
Log.d("test", "" + test);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (characteristic.getUuid().toString().equals(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID))
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
writeStartCommand();
Log.d(TAG, "volume Descriptor writing successful");
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
// this is where the tricky part comes
if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
Log.e(TAG, "Bonding required!!!");
} else {
// this situation happens when you try to connect for the
// second time to already bonded device
// it should never happen, in my opinion
Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
// I don't know what to do here
// This error was found on Nexus 7 with KRT16S build of
// Andorid 4.4. It does not appear on Samsung S4 with
// Andorid 4.3.
}
} else {
Log.e(TAG, "Error writing descriptor, status: " + status);
}
}
};
public void writeStartCommand() {
int val = 0x55;
doWrite(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.VOLUME_START_SERVICE_UUID), val);
waitSometime(100);
}
public synchronized void doWrite(UUID gatservice_uuid, UUID char_uuid, int value) {
try {
byte[] value1 = new byte[1];
value1[0] = (byte) (Integer.valueOf(value) & 0xFF);
BluetoothGattService mSVC = mBluetoothGatt.getService(gatservice_uuid);
BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(char_uuid);
mCH.setValue(value1);
Log.d("write val", "" + value1);
// mCH.setValue(value, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
// mCH.setWriteType(BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);
if (mBluetoothGatt.writeCharacteristic(mCH)) {
waitSometime(100);
Log.d("sucess", "write characteristics successfully stored-" + char_uuid);
} else {
Log.d("fail", "write characteristics failed-" + char_uuid);
/* if(char_uuid.toString().equalsIgnoreCase(GattAttributes.CALIBRATION_START_COMMAND_UUID)){
Thread.sleep(100);
isWriting
writeStopCommand();
}else {
Toast.makeText(CalibrationLeService.this, "Write Characteristics failed"+char_uuid.toString(), Toast.LENGTH_SHORT).show();
}*/
//sendBroadcast(new Intent(BluetoothLeForegroundService.ACTION_FINISH));
}
} catch (Exception e) {
Log.d("characteristic id is", "discoverd services not available");
}
}
public synchronized void waitSometime(int seconds) {
try {
Thread.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// And then finally in your activity just call
public static String BATTERY_LEVEL_SERVICE_UUID = "5956efdd-7272-4bfe-937a-f17c70e86b55"; // Volume level service UUID
public static String BATTERY_LEVEL_CHARACTERSTIC_UUID = "a5bd1e6a-db71-4da5-9b42-a59800e4538b";
mBluetoothLeForegroundService.indicateCharacteristic(UUID.fromString(GattAttributes.BATTERY_LEVEL_SERVICE_UUID), UUID.fromString(GattAttributes.BATTERY_LEVEL_CHARACTERSTIC_UUID), true);