Example usage for android.os ParcelUuid ParcelUuid

List of usage examples for android.os ParcelUuid ParcelUuid

Introduction

In this page you can find the example usage for android.os ParcelUuid ParcelUuid.

Prototype

public ParcelUuid(UUID uuid) 

Source Link

Document

Constructor creates a ParcelUuid instance from the given UUID .

Usage

From source file:Main.java

/**
 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
 * but the returned UUID is always in 128-bit format.
 * Note UUID is little endian in Bluetooth.
 *
 * @param uuidBytes Byte representation of uuid.
 * @return {@link ParcelUuid} parsed from bytes.
 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
 *///from  w  w w.ja  v a  2  s.  c  o  m
public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
    if (uuidBytes == null) {
        throw new IllegalArgumentException("uuidBytes cannot be null");
    }
    int length = uuidBytes.length;
    if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT && length != UUID_BYTES_128_BIT) {
        throw new IllegalArgumentException("uuidBytes length invalid - " + length);
    }

    // Construct a 128 bit UUID.
    if (length == UUID_BYTES_128_BIT) {
        ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
        long msb = buf.getLong(8);
        long lsb = buf.getLong(0);
        return new ParcelUuid(new UUID(msb, lsb));
    }

    // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
    // 128_bit_value = uuid * 2^96 + BASE_UUID
    long shortUuid;
    if (length == UUID_BYTES_16_BIT) {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
    } else {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
        shortUuid += (uuidBytes[2] & 0xFF) << 16;
        shortUuid += (uuidBytes[3] & 0xFF) << 24;
    }
    long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
    long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
    return new ParcelUuid(new UUID(msb, lsb));
}

From source file:com.example.emulator.EmulatorFragment.java

@Override
public ParcelUuid getServiceUUID() {
    return new ParcelUuid(BATTERY_SERVICE_UUID);
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

private synchronized void startAdvertising(BluetoothGattService service) {
    mGattServer.addService(service);/*  w w w .  ja  v a 2 s .  c  o  m*/
    synchronized (Driver.class) {
        mClassicAdvertiser.addService(service.getUuid(), sClassicDiscoverableDurationInSec);
        sClassicDiscoverableDurationInSec = 0;
    }
    if (mLeAdvertiser != null) {
        final UUID uuid = service.getUuid();
        AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).build();
        AdvertiseData data = new AdvertiseData.Builder().addServiceUuid(new ParcelUuid(uuid))
                .setIncludeTxPowerLevel(true).build();
        AdvertiseCallback callback = new AdvertiseCallback() {
            @Override
            public void onStartFailure(int errorCode) {
                Log.e(TAG, "startAdvertising failed: " + uuid + ", errorCode:" + errorCode);
            }
        };
        // TODO(jhahn): The maximum number of simultaneous advertisements is limited by the chipset.
        // Rotate active advertisements periodically if the total number of advertisement exceeds
        // the limit.
        mLeAdvertiser.startAdvertising(settings, data, callback);
        mLeAdvertiseCallbacks.put(uuid, callback);
    }
}

From source file:org.thaliproject.p2p.btconnectorlib.internal.bluetooth.BluetoothUtils.java

/**
 * Creates a new Bluetooth socket with the given service record UUID and the given channel/port.
 * @param bluetoothDevice The Bluetooth device.
 * @param serviceRecordUuid The service record UUID.
 * @param channelOrPort The RFCOMM channel or L2CAP psm to use.
 * @param secure If true, will try to create a secure RFCOMM socket. If false, will try to create an insecure one.
 * @return A new Bluetooth socket with the specified channel/port or null in case of a failure.
 *//*from w ww. j  av a2  s .  c  o  m*/
public static BluetoothSocket createBluetoothSocketToServiceRecord(BluetoothDevice bluetoothDevice,
        UUID serviceRecordUuid, int channelOrPort, boolean secure) {
    Constructor[] bluetoothSocketConstructors = BluetoothSocket.class.getDeclaredConstructors();
    Constructor bluetoothSocketConstructor = null;

    for (Constructor constructor : bluetoothSocketConstructors) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        boolean takesBluetoothDevice = false;
        boolean takesParcelUuid = false;

        for (Class<?> parameterType : parameterTypes) {
            if (parameterType.equals(BluetoothDevice.class)) {
                takesBluetoothDevice = true;
            } else if (parameterType.equals(ParcelUuid.class)) {
                takesParcelUuid = true;
            }
        }

        if (takesBluetoothDevice && takesParcelUuid) {
            // We found the right constructor
            bluetoothSocketConstructor = constructor;
            break;
        }
    }

    // This is the constructor we should now have:
    // BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, BluetoothDevice device,
    //      int port, ParcelUuid uuid) throws IOException

    // Create the parameters for the constructor
    Object[] parameters = new Object[] { Integer.valueOf(1), // BluetoothSocket.TYPE_RFCOMM
            Integer.valueOf(-1), Boolean.valueOf(secure), Boolean.valueOf(secure), bluetoothDevice,
            Integer.valueOf(channelOrPort), new ParcelUuid(serviceRecordUuid) };

    bluetoothSocketConstructor.setAccessible(true);
    BluetoothSocket bluetoothSocket = null;

    try {
        bluetoothSocket = (BluetoothSocket) bluetoothSocketConstructor.newInstance(parameters);
        Log.d(TAG, "createBluetoothSocketToServiceRecord: Socket created with channel/port " + channelOrPort);
    } catch (Exception e) {
        Log.e(TAG, "createBluetoothSocketToServiceRecord: Failed to create a new Bluetooth socket instance: "
                + e.getMessage(), e);
    }

    return bluetoothSocket;
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

private synchronized void startBluetoothLeScanner() {
    if (mLeScanner == null) {
        return;/*from   ww w  . j  a  v  a2  s  .co m*/
    }

    ImmutableList.Builder<ScanFilter> builder = new ImmutableList.Builder();
    for (UUID uuid : mScanUuids) {
        builder.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(uuid)).build());
    }
    List<ScanFilter> filters = builder.build();

    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();

    // ScanFilter doesn't work with startScan() if there are too many - more than 63bits - ignore
    // bits. So we call startScan() without a scan filter for base/mask uuids and match scan results
    // against it.
    final ScanFilter matcher = new ScanFilter.Builder().setServiceUuid(mScanBaseUuid, mScanMaskUuid).build();

    mLeScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
                // This callback will never be called with this callback type, since the
                // scan setting is for CALLBACK_TYPE_ALL_MATCHES. But just for safety.
                return;
            }
            if (!matcher.matches(result)) {
                return;
            }
            BluetoothDevice device = result.getDevice();
            synchronized (Driver.this) {
                if (mScanSeens != null && mScanSeens.put(device, result.getRssi()) == null) {
                    mGattReader.readDevice(device);
                }
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.e(TAG, "startScan failed: " + errorCode);
        }
    };

    mLeScanner.startScan(filters, settings, mLeScanCallback);
}

From source file:edu.umich.eecs.lab11.camera.CameraFragment.java

private void startAdvertising() {
    if (mBluetoothLeAdvertiser == null)
        return;/*w  w  w. j a  v  a2 s.  co m*/

    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM).build();

    AdvertiseData data = new AdvertiseData.Builder().addServiceUuid(new ParcelUuid(DeviceProfile.BEACON_UUID))
            .addServiceData(new ParcelUuid(DeviceProfile.BEACON_UUID), DeviceProfile.URI_AD).build();

    mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}

From source file:com.lef.ibeacon.service.UpdateService.java

private void broadcastUuid1Write(final UUID uuid) {
    final Intent intent = new Intent(ACTION_UUID1_WRITE_READY);
    intent.putExtra(EXTRA_DATA1, new ParcelUuid(uuid));
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.lef.ibeacon.service.UpdateService.java

private void broadcastUuid2Write(final UUID uuid) {
    final Intent intent = new Intent(ACTION_UUID2_WRITE_READY);
    intent.putExtra(EXTRA_DATA2, new ParcelUuid(uuid));
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.lef.ibeacon.service.UpdateService.java

private void broadcastUuid1Read(final UUID uuid) {
    //      logw("broadcastUuid1Read: " + uuid.toString());
    final Intent intent = new Intent(ACTION_UUID1_READ_READY);
    intent.putExtra(EXTRA_DATA1, new ParcelUuid(uuid));
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.lef.ibeacon.service.UpdateService.java

private void broadcastUuid2Read(final UUID uuid) {
    final Intent intent = new Intent(ACTION_UUID2_READ_READY);
    intent.putExtra(EXTRA_DATA2, new ParcelUuid(uuid));
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}