我是开发BLE的新手,我正在创建一个显示电池状态和百分比的演示。它工作正常,但有时显示0%,这是不正确的。 这是我的代码:
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
final int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, " format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, " UINT8.");
}
int batterylevel = characteristic.getIntValue(format, 0);
Intent in = new Intent(getApplicationContext(), HomeActivity.class);
in.putExtra("battery_status", String.valueOf(batterylevel));
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
finish();
}
答案 0 :(得分:0)
尝试获取batterylevel
,如下所示。
characteristic.getValue()[0]