Android Question How create a binary string

Badesse

New Member
Licensed User
Longtime User
Hello, (Sorry for my Engish, I'm french and don't speak english very well)

Let me come ask you for a little help because I can not coding in b4a a Java function that I found on the Net.
This function is intended to return the Latitude and Longitude of the "celltower" on which the GSM is connected:
http://android-coding.blogspot.fr/2011/06/convert-celllocation-to-real-location.html

I can without problems recovering the lac and cid:
B4X:
Dim r As Reflector
Dim cid, lac As Int

r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "phone", "java.lang.String")
r.Target = r.RunMethod("getCellLocation")
cid = r.RunMethod("getCid")
lac = r.RunMethod("getLac")


I transferred my request through httpUtils2:
B4X:
Dim Http As HttpJob
Dim MyData as Byte

Http.PostBytes("http://www.google.com/glm/mmap",MyData)

My problem is that I can not build my binary string :/

I watched a lot of posts and tutorials, and tested libraries "ByteConverter" and "RandomAccessFile" but without success.

How can I get the following string in my MyData variable:
B4X:
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");

dataOutputStream.writeInt(cid);
dataOutputStream.writeInt(lac);

dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();

Thanking you in advance for your help.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use RandomAccessFile and create a temporary file. It should be simple to convert this code.

To convert writeUTF:
B4X:
Sub WriteUTF(raf As RandomAccessFile, s As String)
 Dim b() As Byte = s.GetBytes("UTF8")
 raf.WriteShort(b.Length, raf.CurrentPosition)
 raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)
End Sub
Then use Bit.InputStreamToBytes to read the data.
 
Upvote 0

Badesse

New Member
Licensed User
Longtime User
@eurojam :

Thank you for your reply but I've already tested this library but it doesn't work for me.

Return is "Provider Disabled"

I think it based on "opencellid.org" who doesn't know all CellTower (For Exemple the Tower which i'm connected is not known). I've tested it on the website without result.
I have a php page who test on Google API and Google know it.

@Erel :
Ok, thanks, i test this solution
 
Upvote 0

Badesse

New Member
Licensed User
Longtime User
Erel say : It should be simple to convert this code.

And me and me, i can not do this .... :oops:

Yet I think I understand :

B4X:
Sub Activity_Resume
   
    ' Get cid & lac
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "phone", "java.lang.String")
    r.Target = r.RunMethod("getCellLocation")
    cid = r.RunMethod("getCid")
    lac = r.RunMethod("getLac")

    ' Write Binary String
    If File.Exists(File.DirRootExternal, "tmp_out.dat") Then
        File.Delete(File.DirRootExternal, "tmp_out.dat")
    End If
   
    raf1.Initialize( File.DirRootExternal, "tmp_out.dat", False)
    raf1.WriteShort(21,raf1.CurrentPosition)
    raf1.WriteLong(0,raf1.CurrentPosition)
    WriteUTF(raf1,"en")
    WriteUTF(raf1,"Android")
    WriteUTF(raf1,"1.0")
    WriteUTF(raf1,"Web")
    raf1.WriteByte(27,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.WriteInt(3,raf1.CurrentPosition)
    WriteUTF(raf1,"")
    raf1.WriteInt(cid,raf1.CurrentPosition)
    raf1.WriteInt(lac,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.WriteInt(0,raf1.CurrentPosition)
    raf1.Flush
    raf1.Close

    ' Get Binary String
    MyData = Bit.InputStreamToBytes( File.OpenInput(File.DirRootExternal, "tmp_out.dat") )
   
    ' Test Binary String sent
    'ToastMessageShow( BC.StringFromBytes(MyData, "UTF8"),True)
   
    ' Send Request
    Http.Initialize("http",Me)
    Http.PostBytes("http://www.google.com/glm/mmap", MyData )

End Sub

Sub JobDone (Job As HttpJob)
    If Job.Success = True Then
       
        ' Write InputStream
        Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "tmp_in.dat", False)
        File.Copy2(Job.GetInputStream, out)
        out.Close
       
        ' Test Binary String return
        MyData = Bit.InputStreamToBytes( File.OpenInput(File.DirRootExternal, "tmp_in.dat") )
        ToastMessageShow( BC.StringFromBytes(MyData, "UTF8"),True)

    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub WriteUTF(raf As RandomAccessFile, s As String)
    Dim b() As Byte = s.GetBytes("UTF8")
    raf.WriteShort(b.Length, raf.CurrentPosition)
    raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)
End Sub

Do you have an idea of my stupidity ?
 

Attachments

  • Test.zip
    462.6 KB · Views: 275
Upvote 0
Top