google / eddystone

Specification for Eddystone, an open beacon format from Google
Apache License 2.0
3.08k stars 762 forks source link

ADVERTISE_FAILED_DATA_TOO_LARGE #200

Closed satendra929 closed 7 years ago

satendra929 commented 7 years ago

Hi,

As per the repo , I am trying to make an app that sends out UID frames. Here's a code snippet private void advertise() { //To check if Bluetooth Multiple Advertising is supported on the Device if( !BluetoothAdapter.getDefaultAdapter().isMultipleAdvertisementSupported() ) { Toast.makeText( this, "Multiple advertisement not supported", Toast.LENGTH_SHORT ).show(); start.setEnabled(false); }

    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    //defining the settings used while advertising
    AdvertiseSettings settings = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH).setConnectable(true).build();

    //We make Parcel UUID(UUID can be generated online) and Advertise Data object
    ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );

    //building servicedata
    byte txPower = (byte) -16;
    byte FrameType = 0x00;
    byte[] namespaceBytes ="01020304050607080910".getBytes();
    byte[] instanceBytes ="AABBCCDDEEFF".getBytes();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        os.write(new byte[]{FrameType,txPower});
        os.write(namespaceBytes);
        os.write(instanceBytes);
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte[] serviceData =os.toByteArray();

    AdvertiseData data = new AdvertiseData.Builder().addServiceData(pUuid,serviceData).addServiceUuid(pUuid).setIncludeDeviceName(false).setIncludeTxPowerLevel(false).build();

     //callback to check success or failure when advertising
    AdvertiseCallback advertisingCallback = new AdvertiseCallback() {

    advertiser.startAdvertising( settings, data, advertisingCallback );
}

What could be causing the data to be too large ? The service data is built as per the code in the repo.

adriancretu commented 7 years ago

Namespace is 16 bytes not 32. This question would make more sense on StackOverflow maybe?

satendra929 commented 7 years ago

Sorry I am new to this community. I thought this would be relevant as the code I wrote was in reference to the one in the repo. I'll go ahead and remove this if asked to.

peetsasaki commented 7 years ago

They bytes you've provided show a common pitfall: Confusing hex vs bytes vs UTF-8 vs other encodings.

The code "01020304050607080910".getBytes(); encodes the given string into bytes using the platform's default charset (Could be UTF-8, or something else). https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()

Instead, what you want here is to parse the given string as hex. The sample code here: https://github.com/google/eddystone/blob/master/eddystone-uid/tools/txeddystone-uid/TxEddystone-UID/app/src/main/java/com/google/sample/txeddystone_uid/MainActivity.java#L357 does this, but there may be libs you could use to avoid having to "roll your own".

satendra929 commented 7 years ago

Thanks a lot for your reply. I think that is what causing the issue.