Can someone help me convert my B4A code to B4I?
A 1 to 1 conversion doesn't work because B4I probably asks for it differently.
Here is my code that runs in B4A. Bluetooth sensors are read out.
Hier noch ein Auszug aus dem SwiftCode
A 1 to 1 conversion doesn't work because B4I probably asks for it differently.
Here is my code that runs in B4A. Bluetooth sensors are read out.
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
}
}