Eddystone信标在使用Beacons-Android库时不起作用

问题描述 投票:0回答:1

我尝试如下使用信标Android库https://github.com/adriancretu/beacons-android#features在android设备上实现Eddystone信标。

public class MainActivity extends AppCompatActivity {

    EddystoneURL beacon = new EddystoneURL("www.github.com");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Beacons.initialize(this);

        beacon.init(5, AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY,AdvertiseSettings.ADVERTISE_TX_POWER_HIGH,0,"Eddie");
        beacon.start();

        UUID uuid = beacon.getUUID();
        String name = beacon.getName();
        Log.i("Log","name is "+name);
        Log.i("Log",uuid.toString());
        String url = beacon.getURL();
        Log.i("Log",url);
        Log.i("Log","App started");
        int k = beacon.getActiveState();
        Log.i("Log","active state : "+k);
        int powerlvl = beacon.getTxPowerLevel();
        Log.i("Log","Power Lvl : "+ powerlvl);
    }


}

并且我得到如下日志

2020-04-09 20:27:42.771 14922-14922/? I/Log: name is Eddie
2020-04-09 20:27:42.772 14922-14922/? I/Log: 21ac707d-2ef0-4578-aa52-f8b8020d97c3
2020-04-09 20:27:42.772 14922-14922/? I/Log: www.github.com
2020-04-09 20:27:42.772 14922-14922/? I/Log: App started
2020-04-09 20:27:42.772 14922-14922/? I/Log: active state : 0
2020-04-09 20:27:42.772 14922-14922/? I/Log: Power Lvl : 3

问题是信标扫描程序未识别出仿真信标。非常感谢您的帮助。谢谢。

java android bluetooth-lowenergy beacon eddystone
1个回答
0
投票

[我意识到这个问题是关于使用beacons-android库构建发射器的,所以更好的答案是显示如何使该库在该库中正常工作。

但是,如果找不到解决方案,并且OP允许使用Android Beacon Library(名称相同,但完全不同)来完成此操作,则下面的代码可以做到这一点:

try {
    byte[] urlBytes = UrlBeaconUrlCompressor.compress("https://www.github.com"");
    Identifier encodedUrlIdentifier = Identifier.fromBytes(urlBytes, 0, urlBytes.length, false);
    ArrayList<Identifier> identifiers = new ArrayList<Identifier>();
    identifiers.add(encodedUrlIdentifier);
    beacon = new Beacon.Builder()
            .setIdentifiers(identifiers)
            .setManufacturer(0x0118)
            .setTxPower(-59)
            .build();
    BeaconParser beaconParser = new BeaconParser()
            .setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT);
    BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), 
              beaconParser);
    beaconTransmitter.startAdvertising(beacon);
} catch (MalformedURLException e) {
    Log.d(TAG, "That URL cannot be parsed");
}

完全公开:我是Android Beacon Library开源项目的首席开发人员。

© www.soinside.com 2019 - 2024. All rights reserved.