Example usage for android.bluetooth.le ScanRecord getServiceData

List of usage examples for android.bluetooth.le ScanRecord getServiceData

Introduction

In this page you can find the example usage for android.bluetooth.le ScanRecord getServiceData.

Prototype

@Nullable
public byte[] getServiceData(ParcelUuid serviceDataUuid) 

Source Link

Document

Returns the service data byte array associated with the serviceUuid .

Usage

From source file:com.google.sample.beaconservice.MainActivityFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getActivity().getSharedPreferences(Constants.PREFS_NAME, 0);
    arrayList = new ArrayList<>();
    arrayAdapter = new BeaconArrayAdapter(getActivity(), R.layout.beacon_list_item, arrayList);

    scanCallback = new ScanCallback() {
        @Override/*ww  w.j ava  2  s  .  co m*/
        public void onScanResult(int callbackType, ScanResult result) {
            ScanRecord scanRecord = result.getScanRecord();
            if (scanRecord == null) {
                Log.w(TAG, "Null ScanRecord for device " + result.getDevice().getAddress());
                return;
            }

            byte[] serviceData = scanRecord.getServiceData(EDDYSTONE_SERVICE_UUID);
            if (serviceData == null) {
                return;
            }

            // We're only interested in the UID frame time since we need the beacon ID to register.
            if (serviceData[0] != EDDYSTONE_UID_FRAME_TYPE) {
                return;
            }

            // Extract the beacon ID from the service data. Offset 0 is the frame type, 1 is the
            // Tx power, and the next 16 are the ID.
            // See https://github.com/google/eddystone/eddystone-uid for more information.
            byte[] id = Arrays.copyOfRange(serviceData, 2, 18);
            if (arrayListContainsId(arrayList, id)) {
                return;
            }

            // Draw it immediately and kick off a async request to fetch the registration status,
            // redrawing when the server returns.
            Log.i(TAG, "id " + Utils.toHexString(id) + ", rssi " + result.getRssi());

            Beacon beacon = new Beacon("EDDYSTONE", id, Beacon.STATUS_UNSPECIFIED, result.getRssi());
            insertIntoListAndFetchStatus(beacon);
        }

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

    createScanner();
}