简体   繁体   中英

How to configure ble advertisement in android for ios to detect

With my current configuration, other android device gets to read the set UUID just fine but when it comes to iOS, my colleague is getting variants of UUID from this advertisement/GATT broadcast. Do I really need to broadcast GATT for iOS to discover me?

Starting the advertisement:

@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);
}

Starting the GATT Server:

@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;
        }
    }
};

We don't need to connect to each other, we just need to scan for its existence and maybe pass bits of information which I can't do right now from android because just including the device name is giving me an Error code 1 which basically means that the payload is bigger than the allowed 31 bytes for the advertisement packet. Any advise?

I solved the problem by including a serviceData on the advertising data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM