Search code examples
javaandroidbluetooth-lowenergycore-bluetoothaltbeacon

Advertise in CoreBluetooth format using the AltBeacon library


I'm trying to achieve inter-os advertising and scanning functionality between Android and ios using BLE. I need to understand what's the process i need to follow for advertising in CoreBluetooth from an Android Device. I've been using AltBeacon library for advertising in iBeacon format which works fine but the ios limitations of not being able to scan only a limited number of beacons on iBeacon has forced me to move to CoreBluetooth framework.

Here's the sample code i use to advertise in iBeacon format:

BluetoothManager bluetoothManager =
            (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null) {
        BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
        BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
        if (mBluetoothLeAdvertiser != null) {
            beacon = new Beacon.Builder()
                    .setId1(userId)
                    .setId2("1")
                    .setId3("1")
                    .setManufacturer(0x004C)
                    .setTxPower(-75)
                    .setDataFields(Arrays.asList(new Long[]{0l}))
                    .build();
            beaconParser = new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
            beaconTransmitter = new BeaconTransmitter(InventaSdk.getContext(), beaconParser);
            beaconTransmitter.setBeacon(beacon);
        }
    }

Solution

  • CoreBluetooth is not a format. It is a set of APIs on iOS. Using CoreBluetooth you can detect a variety of beacon formats with limitations:

    • AltBeacon format can be detected only with the iOS app in the foreground
    • Eddystone formats can be detected in the foreground and the background but is slower to detect in the background than iBeacon.
    • iBeacon format cannot detected by CoreBluetooth. For that you must use CoreLocation.

    To advertise in AltBeacon (detectable by core Bluetooth) use this code:

    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();
    BeaconParser beaconParser = new BeaconParser()
            .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
    BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
    beaconTransmitter.startAdvertising(beacon);
    

    Parsing beacons with CoreBlutooth is tricky. You probably want to use a tool to do so like iOS Beacon Tools.

    It is also worth adding that iOS cannot advertise AltBeacon or Eddystone formats. It can only advertise iBeacon and GATT service UUIDs.