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