下面列出了android.os.ParcelUuid#getUuid ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean matchesServiceUuids(ParcelUuid uuid, ParcelUuid parcelUuidMask,
List<ParcelUuid> uuids) {
if (uuid == null) {
return true;
}
if (uuids == null) {
return false;
}
for (ParcelUuid parcelUuid : uuids) {
UUID uuidMask = parcelUuidMask == null ? null : parcelUuidMask.getUuid();
if (matchesServiceUuid(uuid.getUuid(), uuidMask, parcelUuid.getUuid())) {
return true;
}
}
return false;
}
private boolean matchesServiceUuids(ParcelUuid uuid, ParcelUuid parcelUuidMask,
List<ParcelUuid> uuids) {
if (uuid == null) {
return true;
}
if (uuids == null) {
return false;
}
for (ParcelUuid parcelUuid : uuids) {
UUID uuidMask = parcelUuidMask == null ? null : parcelUuidMask.getUuid();
if (matchesServiceUuid(uuid.getUuid(), uuidMask, parcelUuid.getUuid())) {
return true;
}
}
return false;
}
/**
* Check if the uuid pattern is contained in a list of parcel uuids.
*/
private static boolean matchesServiceUuids(@Nullable final ParcelUuid uuid,
@Nullable final ParcelUuid parcelUuidMask,
@Nullable final List<ParcelUuid> uuids) {
if (uuid == null) {
return true;
}
if (uuids == null) {
return false;
}
for (final ParcelUuid parcelUuid : uuids) {
final UUID uuidMask = parcelUuidMask == null ? null : parcelUuidMask.getUuid();
if (matchesServiceUuid(uuid.getUuid(), uuidMask, parcelUuid.getUuid())) {
return true;
}
}
return false;
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*/
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
}
/**
* Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
*/
public static boolean is32BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
if (is16BitUuid(parcelUuid)) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
}
/**
* Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
*/
public static boolean is32BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
if (is16BitUuid(parcelUuid)) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
}
private UUID readVirtualLabelFromParcelable(Parcel in, final int address) {
if (MeshAddress.isValidVirtualAddress(address)) {
final ParcelUuid parcelUuid = in.readParcelable(ParcelUuid.class.getClassLoader());
if (parcelUuid != null) {
return parcelUuid.getUuid();
}
}
return null;
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*/
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return (uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L;
}
protected AccessMessage(final Parcel source) {
super(source);
final ParcelUuid parcelUuid = source.readParcelable(ParcelUuid.class.getClassLoader());
if (parcelUuid != null) {
label = parcelUuid.getUuid();
}
lowerTransportAccessPdu = readSparseArrayToParcelable(source);
accessPdu = source.createByteArray();
transportPdu = source.createByteArray();
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*/
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
}
@Override
public BluetoothLeDevice onFilter(BluetoothLeDevice bluetoothLeDevice) {
BluetoothLeDevice tempDevice = null;
if (bluetoothLeDevice != null && bluetoothLeDevice.getDevice() != null
&& bluetoothLeDevice.getDevice().getUuids() != null
&& bluetoothLeDevice.getDevice().getUuids().length > 0) {
for (ParcelUuid parcelUuid : bluetoothLeDevice.getDevice().getUuids()) {
if (uuid != null && uuid == parcelUuid.getUuid()) {
tempDevice = bluetoothLeDevice;
}
}
}
return tempDevice;
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*/
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
}
/**
* Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
*/
public static boolean is32BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
if (is16BitUuid(parcelUuid)) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*/
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
}
@Test
public void scanForPeripheralsWithServicesTest() throws Exception {
application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);
UUID BLP_SERVICE_UUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb");
central.scanForPeripheralsWithServices(new UUID[]{BLP_SERVICE_UUID});
// Make sure startScan is called
ArgumentCaptor<List> scanFiltersCaptor = ArgumentCaptor.forClass(List.class);
ArgumentCaptor<ScanSettings> scanSettingsCaptor = ArgumentCaptor.forClass(ScanSettings.class);
ArgumentCaptor<ScanCallback> scanCallbackCaptor = ArgumentCaptor.forClass(ScanCallback.class);
verify(scanner).startScan(scanFiltersCaptor.capture(), scanSettingsCaptor.capture(), scanCallbackCaptor.capture());
// Verify there is only 1 filter set
List<ScanFilter> filters = scanFiltersCaptor.getValue();
assertEquals(1, filters.size());
// Verify the filter contains the UUID we added
ScanFilter uuidFilter = filters.get(0);
ParcelUuid parcelUuid = uuidFilter.getServiceUuid();
UUID uuid = parcelUuid.getUuid();
assertEquals(BLP_SERVICE_UUID.toString(), uuid.toString());
// Grab the scan callback that is used
Field field = BluetoothCentral.class.getDeclaredField("scanByServiceUUIDCallback");
field.setAccessible(true);
ScanCallback scanCallback = (ScanCallback) field.get(central);
// Fake scan result
ScanResult scanResult = mock(ScanResult.class);
BluetoothDevice device = mock(BluetoothDevice.class);
when(device.getAddress()).thenReturn("00:00:00:00");
when(scanResult.getDevice()).thenReturn(device);
scanCallback.onScanResult(CALLBACK_TYPE_ALL_MATCHES, scanResult);
// See if we get it back
ArgumentCaptor<BluetoothPeripheral> bluetoothPeripheralCaptor = ArgumentCaptor.forClass(BluetoothPeripheral.class);
ArgumentCaptor<ScanResult> scanResultCaptor = ArgumentCaptor.forClass(ScanResult.class);
verify(callback).onDiscoveredPeripheral(bluetoothPeripheralCaptor.capture(), scanResultCaptor.capture());
assertEquals(scanResultCaptor.getValue(), scanResult);
assertEquals(bluetoothPeripheralCaptor.getValue().getAddress(), "00:00:00:00");
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid.
* For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
* this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32;
return (int) value;
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid. For example, if
* 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid, this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
return (int) value;
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid. For example, if
* 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid, this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
return (int) value;
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid.
* For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
* this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
return (int) value;
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid.
* For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
* this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
return (int) value;
}