Android Question Send BLE Alert to Smart Band (Mi Band 5)

Hi,
I am trying to send alert to smart band (Mi Band 5).
I am sending this alert message : “pollutant_id:pM10 Max:314
For sms/mms alert, the alert message has to be preceded by 0x05 and 0x01
so if I convert the alert message to Hex, the following code works :
However I do not want to convert the alert message to Hex manually. Is there a way to convert the message to array of hex bytes and add it after the 0x05 and 0x01 hex bytes?
B4X:
Sub send_hex_alert(str As String)
    Dim bc As ByteConverter
    pollutant= bc.HexFromBytes(str.GetBytes("UTF8"))
    Log(pollutant)
    Try
        manager.WriteData(s1, misms,Array As Byte(0x05,0x01,0x70,0x6f,0x6c,0x6c,0x75,0x74,0x61,0x6e,0x74,0x5f,0x69,0x64,0x3a,0x50,0x4d,0x31,0x30,0x20,0x4d,0x61,0x78,0x3a,0x33,0x31,0x34))
      'manager.WriteData(s1, misms,Array As Byte(0x05,0x01,pollutant) - this doesn't work. Need a way to convert pollutant variable to array of bytes as shown above[B][/B]
    Catch
        FailedToSend
        Return
    End Try
End Sub
Thanks
 
The best way is with B4XBytesBuilder:
B4X:
Dim bb As B4XBytesBuilder
bb.Initialize
bb.Append(Array(0x05, 0x01))
bb.Append("pollutant_id:pM10 Max:314".GetBytes("utf8"))
manager.WriteData(s1, misms, bb.ToArray)
Thank you for the reply and introducing me to powerful B4XBytesBuilder. However, I am getting 'Object[] cannot be converted to byte[]' error while compiling the above code. I am unable to resolve it by myself.
B4A line: 137
bb.Append(Array(0x05, 0x01))
javac 1.8.0_261
src\b4a\example\starter.java:181: error: incompatible types: Object[] cannot be converted to byte[]
_bb._append /*b4a.example.b4xbytesbuilder*/ (null,(byte[])(new Object[]{(Object)(0x05),(Object)(0x01)}));
 
Upvote 0
Top