German B4A Code nach B4I

Wolli013

Well-Known Member
Licensed User
Longtime User
Kann mir hier jemand helfen meinen B4A Code in B4I umzusetzen?
Eine 1 zu 1 Umsetzung klappt leider nich weil B4I es wohl anders abfragt.
Hier mein Code der in B4A läuft. Es werden Bluetooth Sensoren ausgelesen.

B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
 
Dim bc As ByteConverter 
Dim key As Int = -1 'type is important. Must be Int.
 
If AdvertisingData.ContainsKey(key) Then

Dim b() As Byte = AdvertisingData.Get(key)
Dim hex As String = bc.HexFromBytes(b)

'Temperatur
Private temperature As Int
Private tem1,tem2,hum As Int
Private temp1,hum1 As Float
  
Dim temperatureSign As Boolean = Bit.And(b(4), 0x80) <> 0
temperature = Bit.And(b(4), 0x7f)' + (b(5) / 100.0)
    If temperatureSign Then
    temperature = -temperature
    End If
tem1 = temperature
tem2 = b(5)

'Luftfeuchtigkeit
    Dim Str3 As String
     Str3 = hex.SubString2(6, 8)
      hum = Bit.ParseInt(Str3, 16)
 
'Ergebniss
    temp1 = tem1&"."&tem2 ' = Grad
    hum1 = hum/2 ' = Relative Luftfeuchtigkeit
 
End If

End Sub

Hier noch ein Auszug aus dem SwiftCode

B4X:
class DecodeFormat3: RuuviTagDecoder {
    func decode(data: Data) -> RuuviTag {
        let ruuviTag = RuuviTag()
        
        ruuviTag.humidity = Double(data[3]) * 0.5
        
        let temperatureSign = (data[4] >> 7) & 1
        let temperatureBase = data[4] & 0x7F
        let temperatureFraction = Double(data[5]) / 100.0
        var temperature = Double(temperatureBase) + temperatureFraction
        if (temperatureSign == 1) {
            temperature *= -1;
        }
        ruuviTag.temperature = temperature
        
        let pressureHi = data[6] & 0xFF
        let pressureLo = data[7] & 0xFF
        ruuviTag.pressure = Double(pressureHi) * 256.0 + 50000.0 + Double(pressureLo)
        ruuviTag.pressure /= 100.0;
        
        ruuviTag.accelerationX = Double(UInt16(data[8]) << 8 | UInt16(data[9] & 0xFF)) / 1000.0;
        ruuviTag.accelerationY = Double(UInt16(data[10]) << 8 | UInt16(data[11] & 0xFF)) / 1000.0;
        ruuviTag.accelerationZ = Double(UInt16(data[12]) << 8 | UInt16(data[13] & 0xFF)) / 1000.0;
        
        let battHi = data[14] & 0xFF;
        let battLo = data[15] & 0xFF;
        ruuviTag.voltage = (Double(battHi) * 256.0 + Double(battLo)) / 1000.0;
        
        ruuviTag.updatedAt = NSDate()
        return ruuviTag
    }
}
 
Last edited:
Top