gpt4 book ai didi

java - 如何在android中配置ble广告以供ios检测

转载 作者:行者123 更新时间:2023-12-01 17:39:31 25 4
gpt4 key购买 nike

根据我当前的配置,其他 Android 设备可以很好地读取设置的 UUID,但是当涉及到 iOS 时,我的同事从这个广告/GATT 广播中获取了 UUID 的变体。我真的需要广播 iOS 版 GATT 才能发现我吗?

开始广告:

@ReactMethod
private void advertise(Callback advCallBack) {
...
private static UUID myUUID = UUID.fromString("A85A30E5-93F3-42AE-86EB-33BFD8133597");

AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH).setConnectable(false).build();

ParcelUuid pUuid = new ParcelUuid(myUUID);

AdvertiseData data = new AdvertiseData.Builder().setIncludeDeviceName(false).addServiceUuid(pUuid)
.setIncludeTxPowerLevel(true).build();

AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}

@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
};

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

启动 GATT 服务器:

@ReactMethod
private void startServer(Callback srvCallBack) {
mBluetoothGattServer = mBluetoothManager.openGattServer(getReactApplicationContext(), mGattServerCallback);
if (mBluetoothGattServer == null) {
srvCallBack.invoke(false);
return;
}

mBluetoothGattServer.addService(new BluetoothGattService(myUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY));

srvCallBack.invoke(true);
}

private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
return;
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
return;
}
}
};

我们不需要相互连接,我们只需要扫描它的存在,也许还可以传递一些我现在无法从 Android 执行的信息,因为仅包含设备名称就会给我一个错误代码1 基本上意味着有效负载大于广告数据包允许的 31 个字节。有什么建议吗?

最佳答案

我通过在广告数据中包含 serviceData 解决了这个问题。

      AdvertiseData advertiseData = new AdvertiseData.Builder().setIncludeDeviceName(true)
.addServiceUuid(new ParcelUuid(SERVICE_UUID)).addServiceData(new ParcelUuid(SERVICE_UUID), serviceData)
.setIncludeTxPowerLevel(true).build();

关于java - 如何在android中配置ble广告以供ios检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60983458/

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