Android Open Source - android-sdk Advertisement Packet Parser






From Project

Back to project page android-sdk.

License

The source code is released under:

MIT License

If you think the Android project android-sdk 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 io.relayr.ble.parser;
/*w w  w  .j  av a 2 s. c om*/
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class AdvertisementPacketParser {

    private static final String TAG = "AdvertisementPacketParser";
    private static final int SERVICES_MORE_AVAILABLE_16_BIT = 0x02;
    private static final int SERVICES_COMPLETE_LIST_16_BIT = 0x03;
    private static final int SHORTENED_LOCAL_NAME = 0x08;
    private static final int COMPLETE_LOCAL_NAME = 0x09;

    /**
     * Decodes the services uuid from the advertisement packet since
     * {@link android.bluetooth.BluetoothDevice#getUuids()} returns null most of the times.
     */
    public static List<String> decodeServicesUuid(byte[] data) {
        List<String> serviceUuids = new ArrayList<>();
        if (data != null) {
            int packetLength = data.length;
            for (int index = 0; index < packetLength-1; index++) {
                int fieldLength = data[index];
                int fieldName = data[++index];

                if (fieldName == SERVICES_MORE_AVAILABLE_16_BIT || fieldName == SERVICES_COMPLETE_LIST_16_BIT) {
                    for (int i = index + 1; i < index + fieldLength - 1; i += 2)
                        serviceUuids.add(decodeService16BitUUID(data, i));
                }
                index += fieldLength - 1;
            }
        }
        return serviceUuids;
    }

    /**
     * Decodes the device name from the Complete Local Name or Shortened Local Name field in the
     * Advertisement packet. {@link android.bluetooth.BluetoothDevice#getName()} does it already,
     * although some phones skip it. i.e. Sony Xperia Z1 (C6903) with Android 4.3 where getName()
     * always returns <code>null</code>.
     */
    public static String decodeDeviceName(byte[] data) {
        String name = null;
        int fieldLength, fieldName;
        int packetLength = data.length;
        for (int index = 0; index < packetLength-1; index++) {
            fieldLength = data[index];
            if (fieldLength == 0)
                break;
            fieldName = data[++index];

            if (fieldName == COMPLETE_LOCAL_NAME || fieldName == SHORTENED_LOCAL_NAME) {
                name = decodeLocalName(data, index + 1, fieldLength - 1);
                break;
            }
            index += fieldLength - 1;
        }
        return name;
    }

    private static String decodeLocalName(final byte[] data, final int start, final int length) {
        try {
            return new String(data, start, length, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            Log.e(TAG, "Unable to encode the complete local name to UTF-8", e);
            return "";
        } catch (final IndexOutOfBoundsException e) {
            Log.e(TAG, "Error when reading complete local name", e);
            return "";
        }
    }

    private static String decodeService16BitUUID(byte[] data, int startPosition) {
        return Integer.toHexString(decodeUuid16(data, startPosition));
    }

    private static int decodeUuid16(byte[] data, int start) {
        int b1 = data[start] & 0xff;
        int b2 = data[start + 1] & 0xff;

        return (b2 << 8 | b1);
    }
}




Java Source Code List

io.relayr.DebugModules.java
io.relayr.LoginEventListener.java
io.relayr.Modules.java
io.relayr.RelayrApp.java
io.relayr.RelayrModule.java
io.relayr.RelayrSdk.java
io.relayr.SocketClient.java
io.relayr.activity.LoginActivity.java
io.relayr.activity.UiModule.java
io.relayr.api.ApiModule.java
io.relayr.api.CloudApi.java
io.relayr.api.DebugApiModule.java
io.relayr.api.MockBackend.java
io.relayr.api.MockCloudApi.java
io.relayr.api.MockOauthApi.java
io.relayr.api.MockRelayrApi.java
io.relayr.api.MockStatusApi.java
io.relayr.api.MockSubscriptionApi.java
io.relayr.api.OauthApi.java
io.relayr.api.RelayrApi.java
io.relayr.api.StatusApi.java
io.relayr.api.SubscriptionApi.java
io.relayr.api.Utils.java
io.relayr.api.package-info.java
io.relayr.ble.BleDeviceManager.java
io.relayr.ble.BleDeviceMode.java
io.relayr.ble.BleDeviceType.java
io.relayr.ble.BleDevice.java
io.relayr.ble.BleDevicesScanner.java
io.relayr.ble.BleModule.java
io.relayr.ble.BleScannerFilter.java
io.relayr.ble.BleSocketClient.java
io.relayr.ble.BleUtils.java
io.relayr.ble.BluetoothGattStatus.java
io.relayr.ble.DebugBleModule.java
io.relayr.ble.DeviceCompatibilityUtils.java
io.relayr.ble.MockBleUtils.java
io.relayr.ble.NullableRelayrBleSdk.java
io.relayr.ble.RelayrBleSdkImpl.java
io.relayr.ble.RelayrBleSdk.java
io.relayr.ble.parser.AdvertisementPacketParser.java
io.relayr.ble.parser.BleDataParser.java
io.relayr.ble.service.BaseService.java
io.relayr.ble.service.BluetoothGattReceiver.java
io.relayr.ble.service.BondingReceiver.java
io.relayr.ble.service.DirectConnectionService.java
io.relayr.ble.service.MasterModuleService.java
io.relayr.ble.service.OnBoardingService.java
io.relayr.ble.service.Service.java
io.relayr.ble.service.ShortUUID.java
io.relayr.ble.service.Utils.java
io.relayr.ble.service.error.CharacteristicNotFoundException.java
io.relayr.ble.service.error.DisconnectionException.java
io.relayr.ble.service.error.GattException.java
io.relayr.ble.service.error.ReadCharacteristicException.java
io.relayr.ble.service.error.WriteCharacteristicException.java
io.relayr.ble.service.package-info.java
io.relayr.ble.package-info.java
io.relayr.log.LoggerStorage.java
io.relayr.log.Logger.java
io.relayr.model.AccelGyroscope.java
io.relayr.model.App.java
io.relayr.model.BookmarkDevice.java
io.relayr.model.Bookmark.java
io.relayr.model.Command.java
io.relayr.model.CreateWunderBar.java
io.relayr.model.DeviceModel.java
io.relayr.model.Device.java
io.relayr.model.LightColorProx.java
io.relayr.model.LogEvent.java
io.relayr.model.ModelDefinition.java
io.relayr.model.Model.java
io.relayr.model.OauthToken.java
io.relayr.model.ReadingMeaning.java
io.relayr.model.Reading.java
io.relayr.model.Status.java
io.relayr.model.TransmitterDevice.java
io.relayr.model.Transmitter.java
io.relayr.model.User.java
io.relayr.model.WebSocketConfig.java
io.relayr.model.WunderBar.java
io.relayr.model.package-info.java
io.relayr.storage.DataStorage.java
io.relayr.storage.RelayrProperties.java
io.relayr.util.DebugUtilModule.java
io.relayr.util.MockReachabilityUtils.java
io.relayr.util.ReachabilityUtils.java
io.relayr.util.UtilModule.java
io.relayr.websocket.DebugWebSocketModule.java
io.relayr.websocket.MockWebSocketFactory.java
io.relayr.websocket.MockWebSocket.java
io.relayr.websocket.WebSocketCallback.java
io.relayr.websocket.WebSocketClient.java
io.relayr.websocket.WebSocketFactory.java
io.relayr.websocket.WebSocketModule.java
io.relayr.websocket.WebSocket.java
io.relayr.websocket.package-info.java
io.relayr.package-info.java