OBEX Bluetooth VB.NET

H

henrycortezwu

Hi All,
I'd like to transfer files from a Desktop computer to my
Cellphone/PDA using VB.NET

I've research a bit on this, and I found that I would need to do it
using OBEX via Serial Bluetooth COM Port.

I've read that using OpenNetCF Serial component will make things
easier coding things up because as of the moment there is no Serial
Component on the .NET Framework.

What puzzles me is that I cant find a sample VB.NET applicaton for
Desktop that uses Bluetooth to transfer files to a mobile device. What
I mean is that all samples and documentation regarding bluetooth leads
on me developing a "mobile" application.

Which for my need is actually the other way around, I'd like to
create a "Desktop" application to transfer files to the mobile device.

Is it correct that I can use OpenNetCF library to create a standard
VB.NET Windows Desktop Application?

Can anyone please kindly enlighten me on how to program a "Desktop"
application that transfer files to a mobile device with
VB.NET (VS2003).



Thanks so very much!!,
Henry Wu :)
 
H

henrycortezwu

Hi Peter,
I installed the OpenNetCF BlueTooth and tried converting the sample
OBEX C# program to VB.NET.

I've sucessfully saw my Nokia 6600 bluetooth device!
Now, on trying to send the HelloWorld.txt file, I get an error:
"An unhandled excepition of type 'System.OverflowException' occured in
mscorlib.dll

Additional Information: Value was either too large or too small for an
unsigned byte.

The function that the error occured is at OBEXRequest function at
Convert.ToByte(packetsizeHi).

'PUT-final Header
tSendByte(0) = reqtype ' Request type e.g. PUT-final
130
tSendByte(1) = Convert.ToByte(packetsizeHi) ' Packetlength
Hi
tSendByte(2) = Convert.ToByte(packetsizeLo) ' Packetlength
Lo

Any issues when converting to VB.NET?

Thanks,
Henry :)


For Reference on the OBEXRequest conversion to VB.NET


Private Function OBEXRequest(ByVal tReqType As String, ByVal tName
As String, ByVal tType As String, ByVal tFileContent As String) As
Integer
'send client request

Dim i As Integer
Dim offset As Integer
Dim packetsize As Integer
Dim reqtype As Byte = &H82

Dim tTypeLen As Integer = &H3
Dim typeheadsize As Integer
Dim typesizeHi As Integer = &H0
Dim typesizeLo As Integer = &H3

'tName = "contact.vcf";
'tType = "text/x-vCard";
'tFileContent =
"BEGIN:VCARD\r\nVERSION:2.1\r\nN:;aardvark\r\nFN:aardvark\r\nEND:VCARD\r\n";

If tReqType = "GET" Then
reqtype = &H83 ' 131 GET-Final
End If

If tReqType = "PUT" Then
reqtype = &H82 ' 130 PUT-Final
End If

packetsize = 3

'Name Header
Dim tNameLength As Integer = tName.Length
Dim nameheadsize As Integer = (3 + (tNameLength * 2) + 2)
Dim namesizeHi As Integer = (nameheadsize & &HFF00) / &HFF
Dim namesizeLo As Integer = nameheadsize & &HFF
packetsize = packetsize + nameheadsize

If tType <> "" Then
'Type Header
tTypeLen = tType.Length
typeheadsize = 3 + tTypeLen + 1
typesizeHi = (typeheadsize & &HFF00) / &HFF
typesizeLo = typeheadsize & &HFF
packetsize = packetsize + typeheadsize
End If

'Body
Dim fileLen As Integer = tFileContent.Length
Dim fileheadsize As Integer = 3 + fileLen
Dim filesizeHi As Integer = (fileheadsize & &HFF00) / &HFF
Dim filesizeLo As Integer = fileheadsize & &HFF

packetsize = packetsize + fileheadsize

Dim packetsizeHi As Integer = (packetsize & &HFF00) / &HFF
Dim packetsizeLo As Integer = packetsize & &HFF

Dim tSendByte() As Byte = New Byte(packetsize) {}

'PUT-final Header
tSendByte(0) = reqtype ' Request type e.g. PUT-final
130
tSendByte(1) = Convert.ToByte(packetsizeHi) ' Packetlength
Hi
tSendByte(2) = Convert.ToByte(packetsizeLo) ' Packetlength
Lo

offset = 2

'Name Header
tSendByte(offset + 1) = &H1 ' HI for Name header
tSendByte(offset + 2) = Convert.ToByte(namesizeHi) ' Length of
Name header (2 bytes per char)
tSendByte(offset + 3) = Convert.ToByte(namesizeLo) ' Length of
Name header (2 bytes per char)

' Name+\n\n in unicode
Dim tNameU() As Byte =
System.Text.Encoding.BigEndianUnicode.GetBytes(tName)
tNameU.CopyTo(tSendByte, offset + 4)

offset = offset + 3 + (tNameLength * 2)
tSendByte(offset + 1) = &H0 ' null term
tSendByte(offset + 2) = &H0 ' null term

offset = offset + 2

If tType <> "" Then
'Type Header
tSendByte(offset + 1) = &H42 ' HI for Type Header 66
tSendByte(offset + 2) = Convert.ToByte(typesizeHi) ' Length
of Type Header
tSendByte(offset + 3) = Convert.ToByte(typesizeLo) ' Length
of Type Header

For i = 0 To (tTypeLen - 1) Step i + 1
tSendByte(offset + 4 + i) =
Convert.ToByte(Convert.ToChar(tType.Substring(i, 1)))
Next
tSendByte(offset + 3 + tTypeLen + 1) = &H0 ' null
terminator

offset = offset + 3 + tTypeLen + 1
End If

'Body
tSendByte(offset + 1) = &H49 'HI End of Body 73
tSendByte(offset + 2) = Convert.ToByte(filesizeHi) '
tSendByte(offset + 3) = Convert.ToByte(filesizeLo) '1k payload
+ 3 for HI header

For i = 0 To (fileLen - 1) Step i + 1
tSendByte(offset + 4 + i) =
Convert.ToByte(Convert.ToChar(tFileContent.Substring(i, 1)))
Next
'tSendByte[offset+4+fileLen] = &H00; // null terminator

offset = offset + 3 + fileLen

stream.Write(tSendByte, 0, tSendByte.Length)

'listen for server response

'TODO: can hang here forever waiting response...

Dim x As Boolean = stream.DataAvailable ' changed
bluetoothclient - public NetworkStream GetStream()

Dim tArray4() As Byte = New Byte(3) {}
stream.Read(tArray4, 0, 3)

x = stream.DataAvailable

If tArray4(0) = 160 Then
Dim plength As Integer = (tArray4(1) * 256) + tArray4(2) -
3
Dim tArray5() As Byte = New Byte(plength) {}
If plength > 0 Then
stream.Read(tArray5, 0, plength)
'TODO: data in returned packet to deal with
End If
Return 160
End If

If tArray4(0) = 197 Then
Return 197
End If

If tArray4(0) = 192 Then
Return 192
End If

Return 0
End Function
 
H

henrycortezwu

Hi,
I found an error in my VB conversion.
Example Mistake:
Dim filesizeHi As Integer = (fileheadsize & &HFF00) / &HFF
Dim filesizeLo As Integer = fileheadsize & &HFF
Example Correction:
Dim filesizeHi As Integer = (fileheadsize AND &HFF00) / &HFF
Dim filesizeLo As Integer = fileheadsize AND &HFF

It doesnt make an error anymore, however,...

I always stumble on Return 0 , which on the sample program is an
"Other Error" error.
The ff: part of the sample code is where the Return 0 always returns

Dim tArray4() As Byte = New Byte(3) {}
stream.Read(tArray4, 0, 3)

x = stream.DataAvailable

If tArray4(0) = 160 Then
Dim plength As Integer = (tArray4(1) * 256) + tArray4(2) -
3
Dim tArray5() As Byte = New Byte(plength) {}
If plength > 0 Then
stream.Read(tArray5, 0, plength)
'TODO: data in returned packet to deal with
End If
Return 160
End If

If tArray4(0) = 197 Then
Return 197
End If

If tArray4(0) = 192 Then
Return 192
End If

Return 0



Any clues why tArray4(0) returns always 0?

Thanks,
Henry :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top