Android Question BLE Peripheral as Ibeacon

Discussion in 'Android Questions' started by Humberto, May 8, 2019.

Similar threads

B4A Library BLE Peripheral
B4A Tutorial BLE - Heart Rate Monitor
B4A Library Android Things
B4A Tutorial Discovering iBeacon devices with BLE2 library
B4A Code Snippet [B4X] Bytes To File
  1. Humberto

    Humberto Active Member Licensed User

    I found the sequence to set as Ibeacon with BLE Peripheral library but I could not succeed to create the map

    Code:
    '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();
    '}

    Private Sub Manager_StateChanged (State As Int)
        
    If State <> manager.STATE_POWERED_ON Then
            
    ToastMessageShow("Please enable Bluetooth"True)
            NotAvailable = 
    True
        
    Else
            peripheral.Initialize(
    "peripheral", manager)
            
            
    'peripheral.ManufacturerData = CreateMap(123: "this is my data".GetBytes("utf8"))
           ' peripheral.ManufacturerData = CreateMap(123: "this is my data".GetBytes("utf8"), 124: Array As Byte(1, 2, 3, 4))
            'peripheral.ManufacturerData = CreateMap(0x0700: "abc".GetBytes("utf8"))
            Dim xManuf () As Byte
            xManuf =  
    Array As Byte (221,48,14,45,65,48,14,45,65,48,14,45,65,48,14,45,65 ,12,35,12,36,111)
            
            
            peripheral.ManufacturerData = CreateMap(
    22: xManuf )
            
    If peripheral.IsPeripheralSupported = False Then
                
    ToastMessageShow("Peripheral mode not supported."True)
                NotAvailable = 
    True
            
    Else
     
                 peripheral.Start2(
    "B4APeripheral", CreateAdvertiseSettings)


    '            peripheral.Start("B4APeripheral")
                Wait For Peripheral_Start (Success As Boolean)
                
    Log("Peripheral started successfully? " & Success)
            
    End If
        
    End If
        SetState(
    False)
    End Sub
    The comment code is from a site that explain the sequency

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The Java code doesn't create a map. It creates an array of bytes.
     
  3. Humberto

    Humberto Active Member Licensed User

  4. Humberto

    Humberto Active Member Licensed User

    Last edited: May 8, 2019
  5. Erel

    Erel Administrator Staff Member Licensed User

    Porting this code: https://stackoverflow.com/a/48444693/971547

    Code:
    Dim bb As B4XBytesBuilder 'B4XCollections
    bb.Initialize
    bb.Append(
    Array As Byte(0x020x15))
    Dim bc As ByteConverter
    bb.Append(bc.HexToBytes(
    "0CF052C297CA407C84F8B62AAC4E9020"))
    bb.Append(
    Array As Byte(09060xb5))
    Dim b() As Byte = bb.ToArray
    peripheral.ManufacturerData = CreateMap(
    76: b )
     
  6. Humberto

    Humberto Active Member Licensed User

    Doesn´t work.

    The addManufacturerData expect an "int" and an "array" ( addManufacturerData( int , array) ).

    I found this link https://www.programcreek.com/java-a.../com/uriio/beacons/ble/iBeaconAdvertiser.java.

    I suggest to create a command
    Code:
    peripheral.Start3("Beacon_Name", CreateAdvertiseSettings, CreateAdvertiseData)

    Private Sub CreateAdvertiseData As Object
        
    Dim builder As JavaObject
        builder.InitializeNewInstance(
    "android.bluetooth.le.AdvertiseData.Builder"Null)
        builder.RunMethod(
    "setIncludeTxPowerLevel"Array(False))
        builder.RunMethod(
    "setIncludeDeviceName"Array(False))
        builder.RunMethod(
    "addManufacturerData"Array (0x4c, b) )
        
    Return builder.RunMethod("build"Null)
    End Sub
    So we can manage all the parameters from peripheral configuration


    This application can simulate and read beacon.
    https://play.google.com/store/apps/details?id=net.alea.beaconsimulator&hl=pt_BR

    Using this apk to read the ibeacon when the manufacterer is not set I can see the bluetooth conectio ( not as ibeacon ) and when the manufacturer is set I can´t see the bluetooth at all
     
    Last edited: May 9, 2019
  7. Erel

    Erel Administrator Staff Member Licensed User

    This happens inside the library. The items from the ManufacturerData map are added with builder.addManufacturerData.
     
  8. Humberto

    Humberto Active Member Licensed User

    Some tests

    First: I run the software Beacon Simulator FIG 1 and I create a IBeacon UUID 6bd58 ...." that I could read in the same software with the mode scan in a Moto G6 Fig 2 and it recognize as IBeacon

    Second: I run the B4a software in a Moto X without the command "peripheral.ManufacturerData = CreateMap(76: b )" and in Moto G6 I could read the bluetooth connection but as a regular Bluetooth Fig 3 ( I can see the name "SVO_Beacon" ) in the list.

    Three: I run the B4A software in a Moto X with the command "peripheral.ManufacturerData = CreateMap(76: b )" and in Moto G6 I couldn´t find any Bluetooth connection FIG 4.

    I send in attach the zip from the project and the link to apk that I used for simulate an IBeacon and to scan is at topic #6
     

    Attached Files:

  9. Erel

    Erel Administrator Staff Member Licensed User

    I've checked it. The problem is that BlePeripheral creates a service and also sets other settings that are not compatible with iBeacon configuration.

    Attached is an example of using JavaObject with BlePeripheral to create an iBeacon.
     

    Attached Files:

    Humberto likes this.
  10. Humberto

    Humberto Active Member Licensed User

    Works

    Thanks
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice