问题描述
我一直在使用BluetoothLeGatt
应用程序.我正在尝试从 BLE设备-TI CC2541钥匙扣中读取特征.我能够读写某些特征,但不能对某些其他特征进行读写.所有这些服务和特征都在expandableListView
中列出.但是在选择其中一些时,它们的值并不会显示出来.有人可以帮我解决这个问题吗?
I have been working with the BluetoothLeGatt
application. I am trying to read characteristics from a BLE device- TI CC2541 Keyfob. I am able to read and write into some characteristics, but failing to do so to some other characteristics.All these services and characteristics are listed in the expandableListView
. But on selecting some of them, their values and not getting displayed.Can anybody help me out with this problem.
有没有一种使用特征值句柄读取值的方法
Is there a way to read value using Characteristic Value Handle
推荐答案
**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);
这篇关于无法使用BluetoothLeGatt应用程序从BLE设备读取/写入某些特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!