Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LocalName AdStructure, remove trailing null characters from data #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.UnsupportedEncodingException;

import static com.neovisionaries.bluetooth.ble.util.Bytes.removeTrailingNulls;


/**
* An AD structure of type "Shortened Local Name" (type = 0x08)
Expand Down Expand Up @@ -71,14 +73,16 @@ public LocalName(int length, int type, byte[] data)

private void parse(byte[] data)
{
if (data == null || data.length < 1)
byte[] dataStr = removeTrailingNulls(data);

if (dataStr == null || dataStr.length < 1)
{
return;
}

try
{
mLocalName = new String(data, "UTF-8");
mLocalName = new String(dataStr, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/neovisionaries/bluetooth/ble/util/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.neovisionaries.bluetooth.ble.util;



/**
* Utility for byte arrays.
*
Expand Down Expand Up @@ -164,4 +165,34 @@ public static byte[] copyOfRange(byte[] source, int from, int to)

return destination;
}

/**
* Remove trailing nulls from a byte array.
*
* @param data
* An input byte array.

* @return
* A byte array with any trailing null bytes removed.
*/
public static byte[] removeTrailingNulls(byte[] data)
{
int length = lengthWithoutNulls(data);
byte[] result = new byte[length];

System.arraycopy(data, 0, result, 0, length);

return result;
}

private static int lengthWithoutNulls(byte[] data) {
if (data == null || data.length == 0)
return 0;

for (int i = 0; i < data.length; i++)
if (data[i] == '\0')
return i;

return data.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.util.List;
import java.util.UUID;

import com.neovisionaries.bluetooth.ble.advertising.ADPayloadParser;
import com.neovisionaries.bluetooth.ble.advertising.ADStructure;
import com.neovisionaries.bluetooth.ble.advertising.LocalName;
import org.junit.Test;

import javax.xml.bind.DatatypeConverter;


public class StandardGattServiceTest
{
Expand All @@ -51,6 +59,18 @@ private void uuidTest(String uuid, StandardGattService expected)
compareInstances(expected, getByUuid(uuid));
}

private String nameFromScanRecord(String hex)
{
byte[] scanRecord = DatatypeConverter.parseHexBinary(hex);
List<ADStructure> structures = ADPayloadParser.getInstance().parse(scanRecord);
for (ADStructure structure : structures) {
if (structure instanceof LocalName) {
LocalName name = (LocalName) structure;
return name.getLocalName();
}
}
return null;
}

private void uuidTest(UUID uuid, StandardGattService expected)
{
Expand Down Expand Up @@ -126,4 +146,12 @@ public void test10()
{
uuidTest(UUID.fromString("0000181F-0000-1000-8000-00805f9b34fb"), CONTINUOUS_GLUCOSE_MONITORING);
}

@Test
public void test11()
{
// test extracting a name with a null terminated string
// "Name" = 4E616D65
assertEquals("Name", nameFromScanRecord("020102020A0C0319800006094E616D6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
}
}