通过使用AltBeacon格式在广告发布时添加自定义数据 [英] Add custom data while advertising by using AltBeacon format

查看:182
本文介绍了通过使用AltBeacon格式在广告发布时添加自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建与物理网络交互的应用程序时,我正在学习Android应用程序开发.我想根据AltBeacon规范做广告,并以 string bytes 的形式添加小的有效载荷.我遇到了 Beacon.Builder Javadoc,但是它写得不好.我应该从上面的javadoc中使用哪个函数,或者还有其他可用的东西?

I am learning Android app development while building an app that interacts with the physical web. I want to advertise based on AltBeacon specification and add small payload in the form of string or bytes. I came across Beacon.Builder Javadoc but it is not well written. Which function should I use from the above javadoc or is there something else available?

我的目标是使用我的应用做广告并传输某些字符串.如果另一台设备安装了我的应用,则它应显示带有该字符串的通知.我所有的代码均来自.

My goal is to advertise using my app and transmit certain string. If the other device has my app then it should show notification with that string. All my code has been taken from this.

推荐答案

标准蓝牙信标布局(iBeacon,AltBeacon,Eddystone-UID)旨在传输唯一的数字标识符,而不是字符串. (Eddystone-URL有点例外,因为它旨在传输编码的URL字符串.)

Standard bluetooth beacon layouts (iBeacon, AltBeacon, Eddystone-UID) are designed to transmit a unique numeric identifier, not a string. (Eddystone-URL is a bit of an exception as it is designed to transmit an encoded URL string.)

但是,当然可以传输在信标传输中编码的字符串.只需了解这有一些限制:

However, it is certainly possible to transmit a string encoded in a beacon transmission. Just understand that there are a few limits to this:

  1. 空间量有限.信标传输中只有大约20个字节的可用空间,如果使用ASCII之类的编码,则为20个字符,如果使用UTF-8,则取决于字符,可能会更少.

  1. The amount of space is limited. Only about 20 bytes of usable space is available in beacon transmissions, which is 20 characters if using an encoding like ASCII, and possibly fewer depending on the characters if using UTF-8.

如果广告是您的",并且需要完全解码为字符串,则需要一定数量的匹配字节来确定(以合理的确定性).使用两个字节的匹配值将使您在65536中有1%的机会意外地将别人的信标解码为字符串.

You need a certain number of matching bytes to figure out (with some reasonable certainty) if the advertisement is "yours" and should be decoded as a string at all. Using a two byte matching value would give you a 1 in 65536 chance of accidentally decoding somebody else's beacon as a string.

如果要在iOS上接收和解码,则不能真正使用iBeacon,因为必须使用16字节的UUID进行匹配,剩下两个字节来保存字符串.

If you want to receive and decode on iOS, you can't really use iBeacon, because a 16 byte UUID must be used for matching, leaving two few bytes left over to hold the string.

这是一个示例,说明如何使用修改后的AltBeacon布局来实现此目的,其中前两个字节的匹配标识符0x8b9c用于确保它是您的信标,后两个18字节的标识符用于存储编码的字符串.显示发送最大长度为18个字符的ASCII字符串的代码段:

Here's an example of how you could do this using a modified AltBeacon layout, with a first two byte matching identifier of 0x8b9c used to make sure it is your beacon, and the second 18 byte identifier used to store an encoded string. The code snippet that shows transmitting an ASCII string that is a maximum of 18 characters long:

public static final Identifier MY_MATCHING_IDENTIFIER = Identifier.fromInt(0x8b9c);
...
mBeaconManager.getBeaconParsers().clear();
BeaconParser customBeaconParser = new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-5,i:6-23,p:24-24,d:25-25");
mBeaconManager.addBeaconParser(customBeaconParser);
String stringToTransmit = "Only 18 chars fit!";
byte[] stringToTransmitAsAsciiBytes = stringToTransmit.getBytes(StandardCharsets.US_ASCII);
Beacon beacon = new Beacon.Builder().setId1(MY_MATCHING_IDENTIFIER.toString())
            .setId2(Identifier.fromBytes(stringToTransmitAsAsciiBytes, 0, 18, false).toString())
            .setTxPower(-59).build();
mBeaconTransmitter = new BeaconTransmitter(this, customBeaconParser);
mBeaconTransmitter.startAdvertising(beacon);

在这里收到它:

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    for (Beacon beacon : beacons) {
        if (beacon.getId1().equals(MY_MATCHING_IDENTIFIER)) {
            byte[] bytes = beacon.getId2().toByteArray();
            String receivedString = null;
            try {
                receivedString = new String(bytes, 0, bytes.length, "ASCII");
            } catch (UnsupportedEncodingException e) {
                Log.d(TAG, "Cannot decode ASII");
            }
            Log.d(TAG, "I just received: "+receivedString);
        }
    }
}

编辑:确保如上所示清除信标解析器,否则您的代码将尝试使用默认的信标解析器.请参见上面的代码更改.

Make sure you clear the beacon parsers as shown above otherwise your code will try to use the default beacon parser. See code changes above.

这篇关于通过使用AltBeacon格式在广告发布时添加自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆