简体   繁体   English

如何通过库android-beacon-library将android设备用于Beacon?

[英]How to use a android device to a Beacon by the library android-beacon-library?

BeaconParser beaconParser = new BeaconParser()
    .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
Beacon beacon = new Beacon.Builder()
    .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
    .setId2("1")
    .setId3("2")
    .setManufacturer(0x0118)
    .setTxPower(-59)
    .setDataFields(Arrays.asList(new Long[]{0l}))
    .build();
beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {

    @Override
    public void onStartFailure(int errorCode) {
        Log.e(TAG, "Advertisement start failed with code: " + errorCode);
    }

    @Override
    public void onStartSuccess(AdvertiseSettings settingsInEffect) {
        Log.i(TAG, "Advertisement start succeeded.");
    }
 });

Run this,The log "Advertisement start succeeded." 运行此命令,“广告开始成功”。 is shown, is success. 显示,是成功。 But,mBeaconManager.startMonitoringBeaconsInRegion(region); 但是,mBeaconManager.startMonitoringBeaconsInRegion(区域); not find the altBeacon. 找不到altBeacon。

You cannot detect a beacon on the same device that is transmitting the packets. 您无法在传输数据包的同一设备上检测到信标。 This is not a limitation specific tothe Android Beacon Library, it is just how Bluetooth LE works. 这不是Android Beacon Library特有的限制,而是蓝牙LE的工作原理。 It is common for radio hardware systems to be designed not to receive their own transmissions. 无线电硬件系统通常被设计为不接收它们自己的传输。

Thank you very much for your reply! 非常感谢您的回复!

public class MainActivity extends Activity implements BeaconConsumer, RangeNotifier {

    private BeaconManager mBeaconManager;
public void onResume() {
        super.onResume();
        mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
        // Detect the main Eddystone-UID frame:
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        // Detect the telemetry Eddystone-TLM frame:
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
        mBeaconManager.bind(this);
}

public void onBeaconServiceConnect() {
        Log.d("youfu","onBeaconServiceConnect.");
        Region region = new Region("all-beacons-region", null, null, null);
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        mBeaconManager.addRangeNotifier(this);
    }

@Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        Log.d("youfu","didRangeBeaconsInRegion region=== "+region);
        for (Beacon beacon: beacons) {
            if (beacon.getServiceUuid() == 0xfeaa && beacon.getBeaconTypeCode() == 0x00) {
                // This is a Eddystone-UID frame
                Identifier namespaceId = beacon.getId1();
Identifier instanceId = beacon.getId2();
                Log.d("youfu", "I see a beacon transmitting namespace id: "+namespaceId+
                        " and instance id: "+instanceId+
                        " approximately "+beacon.getDistance()+" meters away.");

                // Do we have telemetry data?
                if (beacon.getExtraDataFields().size() > 0) {
long telemetryVersion = beacon.getExtraDataFields().get(0);
                    long batteryMilliVolts = beacon.getExtraDataFields().get(1);
                    long pduCount = beacon.getExtraDataFields().get(3);
                    long uptime = beacon.getExtraDataFields().get(4);
Log.d("youfu", "The above beacon is sending telemetry version "+telemetryVersion+
                            ", has been up for : "+uptime+" seconds"+
                            ", has a battery level of "+batteryMilliVolts+" mV"+
                            ", and has transmitted "+pduCount+" advertisements.");
}
            }
        }
    }

@Override
    public void onPause() {
        super.onPause();
        mBeaconManager.unbind(this);
    }
}

The log like this:
12-30 10:52:57.232 16674-16674/? D/youfu: onBeaconServiceConnect.
12-30 10:52:58.483 16674-16870/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
12-30 10:52:59.591 16674-16925/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
12-30 10:53:00.699 16674-16945/? D/youfu: didRangeBeaconsInRegion region=== id1: null id2: null id3: null
region is null, so please help me to check the code, Thanks.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用android-beacon-library检测蓝牙设备 - Detect a bluetooth device using android-beacon-library 来自 android-beacon-library 的 ScanJob 中的 ConcurrentModificationException - ConcurrentModificationException in ScanJob from android-beacon-library 使用 android-beacon-library 检测信标不起作用 - Detecting Beacon Using android-beacon-library not working android-beacon-library: ibeacon: 如何访问服务数据? - android-beacon-library: ibeacon: how to access service data? 如何使用android-beacon-library(altbeacon)正确停止扫描信标 - How to properly stop scanning beacon using android-beacon-library (altbeacon) 无法使用AltBeacon / android-beacon-library检测iBeacon - Cannot detect iBeacon using AltBeacon/android-beacon-library Bootstrapnotifier无法识别以后添加的信标| Android-Beacon-Library - Bootstrapnotifier doesnt recognise later added beacons | Android-Beacon-Library 如何使用AltBeacon android-beacon-library检测多个信标的区域输入/退出? - How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library? 使用 Android 信标库查找设备到 Eddystone 信标的距离 - Find the distance to Eddystone beacon from the device using Android Beacon Library Proximity Beacon API vs Android Beacon Library? - Proximity Beacon API vs Android Beacon Library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM