Android Open Source - LeBlue Le Scan Record Impl






From Project

Back to project page LeBlue.

License

The source code is released under:

Apache License

If you think the Android project LeBlue listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package houtbecke.rs.le;
/*w  ww .j a va  2s .c om*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

public class LeScanRecordImpl implements LeScanRecord {

    final byte[] scanrecord;
    final Collection<LeRecord> records = new ArrayList<>();
    public LeScanRecordImpl(byte[] scanrecord) {
        this.scanrecord = scanrecord;
        parse();
    }

    @Override
    public LeRecord[] getRecords() {
        LeRecord[] ret = new LeRecord[records.size()];
        return records.toArray(ret);
    }

    @Override
    public LeRecord[] getRecords(int... types) {
        int len = 0;
        for (LeRecord record: records)
            for (int type: types)
                len+= record.getType() == type ? 1 : 0;

        LeRecord[] ret = new LeRecord[len];
        int count = 0;
        for (LeRecord record: records)
            for (int type: types)
            if (record.getType() == type) {
                ret[count] = record;
                count++;
            }
        return ret;
    }

    @Override
    public UUID[] getServices() {
        LeRecord[] uuids16 = getRecords(2, 3);
        LeRecord[] uuids128 = getRecords(6, 7);

        List<UUID> uuidList = new ArrayList<>();
        for (LeRecord record: uuids16) {
            ByteBuffer buffer = ByteBuffer.wrap(record.getRecordContent()).order(ByteOrder.LITTLE_ENDIAN);

            while (buffer.remaining() >= 2)
                uuidList.add(UUID.fromString(String.format(
                        "%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));

        }
        for (LeRecord record: uuids128) {
            ByteBuffer buffer = ByteBuffer.wrap(record.getRecordContent()).order(ByteOrder.LITTLE_ENDIAN);
            while (buffer.remaining() >= 16) {
                long lsb = buffer.getLong();
                long msb = buffer.getLong();
                uuidList.add(new UUID(msb, lsb));
            }
        }
        UUID[] uuids = new UUID[uuidList.size()];
        return uuidList.toArray(uuids);
    }


    public boolean hasService(UUID uuid ){
        UUID[] uuids = getServices();
        for(UUID u :uuids){
            if (u.equals(uuid)) return true;
        }
        return false;
    }

    @Override
    public byte[] getRawData() {
        return scanrecord;
    }

    void parse() {
        int recordLength, recordPos = 0;
        while (recordPos < scanrecord.length && ((recordLength = scanrecord[recordPos++] & 0xFF) != 0)) {
            final int type = scanrecord[recordPos++]  & 0xFF;
            final byte[] record = Arrays.copyOfRange(scanrecord, recordPos, recordPos + recordLength);
            if (type != 0) {
                records.add(new LeRecord() {
                    @Override
                    public int getType() {
                        return type;
                    }

                    @Override
                    public byte[] getRecordContent() {
                        return record;
                    }
                });
            }
            recordPos += recordLength -1;
        }
    }

}




Java Source Code List

houtbecke.rs.le.BleException.java
houtbecke.rs.le.LeCharacteristicListener.java
houtbecke.rs.le.LeDefinedUUIDs.java
houtbecke.rs.le.LeDeviceListener.java
houtbecke.rs.le.LeDeviceState.java
houtbecke.rs.le.LeDevice.java
houtbecke.rs.le.LeFormat.java
houtbecke.rs.le.LeGattCharacteristic.java
houtbecke.rs.le.LeGattDescriptorValue.java
houtbecke.rs.le.LeGattDescriptor.java
houtbecke.rs.le.LeGattService.java
houtbecke.rs.le.LeGattStatus.java
houtbecke.rs.le.LeRecord.java
houtbecke.rs.le.LeRemoteDeviceListener.java
houtbecke.rs.le.LeRemoteDevice.java
houtbecke.rs.le.LeScanRecordImpl.java
houtbecke.rs.le.LeScanRecord.java
houtbecke.rs.le.LeUtil.java
houtbecke.rs.le.dummy.DummyLeRemoteDevice.java
houtbecke.rs.le.example.MainActivity.java
houtbecke.rs.le.fourdotthree.LeDevice43.java
houtbecke.rs.le.fourdotthree.LeGattCharacteristic43.java
houtbecke.rs.le.fourdotthree.LeGattService43.java
houtbecke.rs.le.fourdotthree.LeRemoteDevice43.java
houtbecke.rs.le.interceptor.AndroidEventSinkUtil.java
houtbecke.rs.le.interceptor.BaseIntercepting.java
houtbecke.rs.le.interceptor.EventSinkUtil.java
houtbecke.rs.le.interceptor.InterceptingLeCharacteristicListener.java
houtbecke.rs.le.interceptor.InterceptingLeDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeDevice.java
houtbecke.rs.le.interceptor.InterceptingLeGattCharacteristic.java
houtbecke.rs.le.interceptor.InterceptingLeGattService.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDevice.java
houtbecke.rs.le.interceptor.LeInterceptor.java
houtbecke.rs.le.interceptor.LeLogCatInterceptor.java
houtbecke.rs.le.interceptor.LeSessionInterceptor.java
houtbecke.rs.le.mock.LeDeviceMock.java
houtbecke.rs.le.mock.LeGattCharacteristicMock.java
houtbecke.rs.le.mock.LeGattServiceMock.java
houtbecke.rs.le.mock.LeMockController.java
houtbecke.rs.le.mock.LeRemoteDeviceMock.java
houtbecke.rs.le.mock.LeSessionController.java
houtbecke.rs.le.mock.NativeWaitNotify.java
houtbecke.rs.le.mock.SimpleWaitNotify.java
houtbecke.rs.le.mock.WaitNotify.java
houtbecke.rs.le.session.CharacteristicsMockerObject.java
houtbecke.rs.le.session.DeviceMockerObject.java
houtbecke.rs.le.session.EventSinkFiller.java
houtbecke.rs.le.session.EventSink.java
houtbecke.rs.le.session.EventSource.java
houtbecke.rs.le.session.EventType.java
houtbecke.rs.le.session.Event.java
houtbecke.rs.le.session.GattServiceMockerObject.java
houtbecke.rs.le.session.ListEventSinkSource.java
houtbecke.rs.le.session.MockedResponseObject.java
houtbecke.rs.le.session.MockedResponse.java
houtbecke.rs.le.session.MockerObject.java
houtbecke.rs.le.session.Mocker.java
houtbecke.rs.le.session.RemoteDeviceMockerObject.java
houtbecke.rs.le.session.SessionObject.java
houtbecke.rs.le.session.Session.java