gpt4 book ai didi

android - 如何使用 BluetoothLeScanner 类广播 iBeacon?

转载 作者:行者123 更新时间:2023-11-29 01:04:22 34 4
gpt4 key购买 nike

我想用 BluetoothLeAdvertiser 通过 android 传输 iBluetooth来自 android.bluetooth.le 的类(class).但我似乎无法设置某些制造商规范数据字段以匹配 iBeacon 的字段。


iBeacon结构: iBeacon data packet

Thisthis告诉您 iBeacon 的 Manufacturer spec data 值需要包含什么。基本上:

Apple Company Identifier (Little Endian), 0x004c = 76
data type, 0x02 => iBeacon
data length, 0x15 = 21
uuid: 585CDE931B0142CC9A1325009BEDC65E
major: [0000]
minor: 0000
meaured power at 1 meter: 0xc5 = -59




我一直在关注 this tutorial具有以下步骤:

  1. 创建 AdvertiseData对象使用 AdvertiseData.Builder.addManufacturerData() (我认为应该是制造商规范数据,请参阅下面的问题)

    protected void setAdvertiseData() {

    AdvertiseData.Builder mBuilder = new AdvertiseData.Builder();

    ByteBuffer mManufacturerData = ByteBuffer.allocate(24);

    byte[] uuid = getIdAsByte(UUID.fromString("0CF052C2-97CA-407C-84F8-B62AAC4E9020"));

    mManufacturerData.put(0, (byte)0xBE); // Beacon Identifier

    mManufacturerData.put(1, (byte)0xAC); // Beacon Identifier

    for (int i=2; i<=17; i++) {

    mManufacturerData.put(i, uuid[i-2]); // adding the UUID

    }

    mManufacturerData.put(18, (byte)0x00); // first byte of Major
    mManufacturerData.put(19, (byte)0x09); // second byte of Major
    mManufacturerData.put(20, (byte)0x00); // first minor
    mManufacturerData.put(21, (byte)0x06); // second minor
    mManufacturerData.put(22, (byte)0xB5); // txPower
    mBuilder.addManufacturerData(224, mManufacturerData.array()); // using google's company ID
    mAdvertiseData = mBuilder.build();
    }
  2. 创建 AdvertiseSettings使用 AdvertiseSettings.Builder

    protected void setAdvertiseSettings() {
    AdvertiseSettings.Builder mBuilder = new AdvertiseSettings.Builder();
    mBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
    mBuilder.setConnectable(false);
    mBuilder.setTimeout(0);
    mBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM);
    mAdvertiseSettings = mBuilder.build();
    }
  3. 使用 BluetoothLeAdvertiser 广播 AdvertiseSettings 和 AdvertiseData . (这也需要一个回调,告诉蓝牙传输是否有效,没有问题)

    mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings, mAdvertiseData, mAdvertiseCallback);




这些步骤的问题是:




一开始我关注的是 this教程,并正在传输一个 Alt-Beacon。也许是 BluetoothLeAdvertiser class 假设我仍在传输与 iBeacon 结构不同的 Alt-Beacon。

Alt-Beacon 结构: Alt-Beacon Data packet



其他资源:

更新代码

public AdvertiseData getiBeaconData() {
AdvertiseData.Builder mBuilder = new AdvertiseData.Builder();
ByteBuffer mManufacturerData = ByteBuffer.allocate(24);

byte[] uuid = getIdAsByte(UUID.fromString(thisCont.getString( R.string.ble_uuid )) );
ParcelUuid parced = new ParcelUuid(UUID.fromString(thisCont.getString( R.string.ble_uuid )) );
/*

R.string.ble_uuid == CDB7950D-73F1-4D4D-8E47-C090502DBD63 这是关于 iBeacon 结构 http://smlie-blog.blogspot.com/2014/06/bluetooth-ibeacon-packet-format.html */

    //mManufacturerData.put((byte)0x00); //Manufactor Id 1
//mManufacturerData.put((byte)0x4C); //Manufactor Id 2
// mManufacturerData.put((byte)0xBE); //ibeconid1
mManufacturerData.put((byte)0x02); //ibeconid2
mManufacturerData.put((byte)0x15); //Data length
for (byte i:uuid) {
mManufacturerData.put(i); // adding the UUID
}
mManufacturerData.put((byte)0x00); //major first
mManufacturerData.put((byte)0x00); //major second
mManufacturerData.put((byte)0x00); //minor first
mManufacturerData.put((byte)0x00); //minor second
//mManufacturerData.put((byte)0xC5); //TX power

/*
mManufacturerData.put((byte)0x1A); // Beacon Identifier was BE iBeacon = 00
mManufacturerData.put((byte)0xFF); // Beacon Identifier was AC iBeacon = 02
/* the above 2 lines are the ibeacon AD Indicator

for (byte i:uuid) {
mManufacturerData.put(i); // adding the UUID
}
mManufacturerData.put((byte)0x00); // first byte of Major
mManufacturerData.put((byte)0x00); // second byte of Major
mManufacturerData.put((byte)0x00); // first minor
mManufacturerData.put((byte)0x00); // second minor
mManufacturerData.put((byte)0xC5); // txPower (RSSI) (from tx)
mBuilder.addManufacturerData(16, mManufacturerData.array()); // using google's company ID
*/

mBuilder.addManufacturerData(76, mManufacturerData.array()); // using google's company ID
//mBuilder.addServiceUuid(parced);
return mBuilder.build();

}

parsed data nrfBeacon raw packet data

最佳答案

我觉得你的代码很好。一些评论:

  • Apple 的制造商 ID 是 76:mBuilder.addManufacturerData(76, mManufacturerData.array())
  • iBeacon 类型为 0x02:mManufacturerData.put(0, (byte)0x02);//信标标识符
  • 长度为 0x15:mManufacturerData.put(1, (byte)0x15);//信标标识符
  • 我不确定这是否有效:UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020")。通常 fromString() 方法需要这样的东西:UUID.fromString("F0018B9B-7509-4C31-A905-1A27D39C003D")

总结:

protected void setAdvertiseData() {

AdvertiseData.Builder mBuilder = new AdvertiseData.Builder()

ByteBuffer mManufacturerData = ByteBuffer.allocate(23);

byte[] uuid = getIdAsByte(UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020"));

mManufacturerData.put(0, (byte)0x02);
mManufacturerData.put(1, (byte)0x15);

for (int i=2; i<=17; i++) {
mManufacturerData.put(i, uuid[i-2]); // adding the UUID
}

mManufacturerData.put(18, (byte)0x00); // first byte of Major
mManufacturerData.put(19, (byte)0x09); // second byte of Major
mManufacturerData.put(20, (byte)0x00); // first minor
mManufacturerData.put(21, (byte)0x06); // second minor
mManufacturerData.put(22, (byte)0xB5); // txPower

mBuilder.addManufacturerData(76, mManufacturerData.array());
mAdvertiseData = mBuilder.build();
}

关于android - 如何使用 BluetoothLeScanner 类广播 iBeacon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438881/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com