Search in sources :

Example 46 with ParcelUuid

use of android.os.ParcelUuid in project android_frameworks_base by crdroidandroid.

the class AdvertiseDataTest method testEmptyServiceData.

@SmallTest
public void testEmptyServiceData() {
    Parcel parcel = Parcel.obtain();
    ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
    byte[] serviceData = new byte[0];
    AdvertiseData data = mAdvertiseDataBuilder.setIncludeDeviceName(true).addServiceData(uuid, serviceData).build();
    data.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    AdvertiseData dataFromParcel = AdvertiseData.CREATOR.createFromParcel(parcel);
    assertEquals(data, dataFromParcel);
}
Also used : ParcelUuid(android.os.ParcelUuid) Parcel(android.os.Parcel) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 47 with ParcelUuid

use of android.os.ParcelUuid in project android_frameworks_base by crdroidandroid.

the class ScanRecordTest method testParser.

@SmallTest
public void testParser() {
    byte[] scanRecord = new byte[] { // advertising flags
    0x02, // advertising flags
    0x01, // advertising flags
    0x1a, // 16 bit service uuids
    0x05, // 16 bit service uuids
    0x02, // 16 bit service uuids
    0x0b, // 16 bit service uuids
    0x11, // 16 bit service uuids
    0x0a, // 16 bit service uuids
    0x11, // name
    0x04, // name
    0x09, // name
    0x50, // name
    0x65, // name
    0x64, // tx power level
    0x02, // tx power level
    0x0A, // tx power level
    (byte) 0xec, // service data
    0x05, // service data
    0x16, // service data
    0x0b, // service data
    0x11, // service data
    0x50, // service data
    0x64, // manufacturer specific data
    0x05, // manufacturer specific data
    (byte) 0xff, // manufacturer specific data
    (byte) 0xe0, // manufacturer specific data
    0x00, // manufacturer specific data
    0x02, // manufacturer specific data
    0x15, // an unknown data type won't cause trouble
    0x03, // an unknown data type won't cause trouble
    0x50, // an unknown data type won't cause trouble
    0x01, // an unknown data type won't cause trouble
    0x02 };
    ScanRecord data = ScanRecord.parseFromBytes(scanRecord);
    assertEquals(0x1a, data.getAdvertiseFlags());
    ParcelUuid uuid1 = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
    ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
    assertTrue(data.getServiceUuids().contains(uuid1));
    assertTrue(data.getServiceUuids().contains(uuid2));
    assertEquals("Ped", data.getDeviceName());
    assertEquals(-20, data.getTxPowerLevel());
    assertTrue(data.getManufacturerSpecificData().get(0x00E0) != null);
    assertArrayEquals(new byte[] { 0x02, 0x15 }, data.getManufacturerSpecificData().get(0x00E0));
    assertTrue(data.getServiceData().containsKey(uuid2));
    assertArrayEquals(new byte[] { 0x50, 0x64 }, data.getServiceData().get(uuid2));
}
Also used : ParcelUuid(android.os.ParcelUuid) ScanRecord(android.bluetooth.le.ScanRecord) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 48 with ParcelUuid

use of android.os.ParcelUuid in project android_frameworks_base by crdroidandroid.

the class AdvertiseDataTest method testServiceUuid.

@SmallTest
public void testServiceUuid() {
    Parcel parcel = Parcel.obtain();
    ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
    ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
    AdvertiseData data = mAdvertiseDataBuilder.setIncludeDeviceName(true).addServiceUuid(uuid).addServiceUuid(uuid2).build();
    data.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    AdvertiseData dataFromParcel = AdvertiseData.CREATOR.createFromParcel(parcel);
    assertEquals(data, dataFromParcel);
}
Also used : ParcelUuid(android.os.ParcelUuid) Parcel(android.os.Parcel) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 49 with ParcelUuid

use of android.os.ParcelUuid in project android_frameworks_base by crdroidandroid.

the class ScanFilterTest method testsetServiceDataFilter.

@SmallTest
public void testsetServiceDataFilter() {
    byte[] setServiceData = new byte[] { 0x50, 0x64 };
    ParcelUuid serviceDataUuid = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
    ScanFilter filter = mFilterBuilder.setServiceData(serviceDataUuid, setServiceData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] emptyData = new byte[0];
    filter = mFilterBuilder.setServiceData(serviceDataUuid, emptyData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] prefixData = new byte[] { 0x50 };
    filter = mFilterBuilder.setServiceData(serviceDataUuid, prefixData).build();
    assertTrue("service data filter fails", filter.matches(mScanResult));
    byte[] nonMatchData = new byte[] { 0x51, 0x64 };
    byte[] mask = new byte[] { (byte) 0x00, (byte) 0xFF };
    filter = mFilterBuilder.setServiceData(serviceDataUuid, nonMatchData, mask).build();
    assertTrue("partial service data filter fails", filter.matches(mScanResult));
    filter = mFilterBuilder.setServiceData(serviceDataUuid, nonMatchData).build();
    assertFalse("service data filter fails", filter.matches(mScanResult));
}
Also used : ParcelUuid(android.os.ParcelUuid) ScanFilter(android.bluetooth.le.ScanFilter) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 50 with ParcelUuid

use of android.os.ParcelUuid in project android_frameworks_base by crdroidandroid.

the class ScanRecord method parseFromBytes.

/**
     * Parse scan record bytes to {@link ScanRecord}.
     * <p>
     * The format is defined in Bluetooth 4.1 specification, Volume 3, Part C, Section 11 and 18.
     * <p>
     * All numerical multi-byte entities and values shall use little-endian <strong>byte</strong>
     * order.
     *
     * @param scanRecord The scan record of Bluetooth LE advertisement and/or scan response.
     * @hide
     */
public static ScanRecord parseFromBytes(byte[] scanRecord) {
    if (scanRecord == null) {
        return null;
    }
    int currentPos = 0;
    int advertiseFlag = -1;
    List<ParcelUuid> serviceUuids = new ArrayList<ParcelUuid>();
    String localName = null;
    int txPowerLevel = Integer.MIN_VALUE;
    SparseArray<byte[]> manufacturerData = new SparseArray<byte[]>();
    Map<ParcelUuid, byte[]> serviceData = new ArrayMap<ParcelUuid, byte[]>();
    try {
        while (currentPos < scanRecord.length) {
            // length is unsigned int.
            int length = scanRecord[currentPos++] & 0xFF;
            if (length == 0) {
                break;
            }
            // Note the length includes the length of the field type itself.
            int dataLength = length - 1;
            // fieldType is unsigned int.
            int fieldType = scanRecord[currentPos++] & 0xFF;
            switch(fieldType) {
                case DATA_TYPE_FLAGS:
                    advertiseFlag = scanRecord[currentPos] & 0xFF;
                    break;
                case DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL:
                case DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE:
                    parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_16_BIT, serviceUuids);
                    break;
                case DATA_TYPE_SERVICE_UUIDS_32_BIT_PARTIAL:
                case DATA_TYPE_SERVICE_UUIDS_32_BIT_COMPLETE:
                    parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_32_BIT, serviceUuids);
                    break;
                case DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL:
                case DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE:
                    parseServiceUuid(scanRecord, currentPos, dataLength, BluetoothUuid.UUID_BYTES_128_BIT, serviceUuids);
                    break;
                case DATA_TYPE_LOCAL_NAME_SHORT:
                case DATA_TYPE_LOCAL_NAME_COMPLETE:
                    localName = new String(extractBytes(scanRecord, currentPos, dataLength));
                    break;
                case DATA_TYPE_TX_POWER_LEVEL:
                    txPowerLevel = scanRecord[currentPos];
                    break;
                case DATA_TYPE_SERVICE_DATA:
                    // The first two bytes of the service data are service data UUID in little
                    // endian. The rest bytes are service data.
                    int serviceUuidLength = BluetoothUuid.UUID_BYTES_16_BIT;
                    byte[] serviceDataUuidBytes = extractBytes(scanRecord, currentPos, serviceUuidLength);
                    ParcelUuid serviceDataUuid = BluetoothUuid.parseUuidFrom(serviceDataUuidBytes);
                    byte[] serviceDataArray = extractBytes(scanRecord, currentPos + serviceUuidLength, dataLength - serviceUuidLength);
                    serviceData.put(serviceDataUuid, serviceDataArray);
                    break;
                case DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
                    // The first two bytes of the manufacturer specific data are
                    // manufacturer ids in little endian.
                    int manufacturerId = ((scanRecord[currentPos + 1] & 0xFF) << 8) + (scanRecord[currentPos] & 0xFF);
                    byte[] manufacturerDataBytes = extractBytes(scanRecord, currentPos + 2, dataLength - 2);
                    manufacturerData.put(manufacturerId, manufacturerDataBytes);
                    break;
                default:
                    // Just ignore, we don't handle such data type.
                    break;
            }
            currentPos += dataLength;
        }
        if (serviceUuids.isEmpty()) {
            serviceUuids = null;
        }
        return new ScanRecord(serviceUuids, manufacturerData, serviceData, advertiseFlag, txPowerLevel, localName, scanRecord);
    } catch (Exception e) {
        Log.e(TAG, "unable to parse scan record: " + Arrays.toString(scanRecord));
        // and return an empty record with raw scanRecord bytes in results
        return new ScanRecord(null, null, null, -1, Integer.MIN_VALUE, null, scanRecord);
    }
}
Also used : ParcelUuid(android.os.ParcelUuid) SparseArray(android.util.SparseArray) ArrayList(java.util.ArrayList) ArrayMap(android.util.ArrayMap)

Aggregations

ParcelUuid (android.os.ParcelUuid)147 SmallTest (android.test.suitebuilder.annotation.SmallTest)43 RemoteException (android.os.RemoteException)29 UUID (java.util.UUID)24 ArrayList (java.util.ArrayList)23 GenericSoundModel (android.hardware.soundtrigger.SoundTrigger.GenericSoundModel)20 Parcel (android.os.Parcel)20 ScanFilter (android.bluetooth.le.ScanFilter)19 RecognitionConfig (android.hardware.soundtrigger.SoundTrigger.RecognitionConfig)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 ScanRecord (android.bluetooth.le.ScanRecord)10 BluetoothDevice (android.bluetooth.BluetoothDevice)9 Socket (java.net.Socket)9 DataOutputStream (java.io.DataOutputStream)8 ByteBuffer (java.nio.ByteBuffer)8 SparseArray (android.util.SparseArray)6 RequiresPermission (android.annotation.RequiresPermission)5 BluetoothClass (android.bluetooth.BluetoothClass)5 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)5 ScanCallback (android.bluetooth.le.ScanCallback)5