科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件VB.NET利用OBEX协议实现红外线文件传输

VB.NET利用OBEX协议实现红外线文件传输

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

目前的红外线传输大都遵循OBEX协议,这是由微软、苹果、诺基亚等公司专门为红外线传输而制定的一整套协议规则。

作者:白猫黑爪子 来源:天极开发 2007年11月9日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
二、程序实现

  .net1.0中对红外的支持全部在System.Net.Sockets空间中,使用很简单,和普通的网络连接差不多,主要有三个类:

IrDAClient 提供连接信息,并创建客户端连接对象,用以打开和关闭与服务器的连接。
IrDADeviceInfo 提供客户端在发现查询过程中获取的有关可用服务器和端口的信息。
IrDAListener 将套接字置于侦听状态,以监视来自指定服务或网络地址的连接。
  
  (一)、获取附近可用红外设备的信息

  在这个示例中,我获取红外设备通过IrDAClient的DiscoverDevices方法实现,该方法以IrDADeviceInfo类型的对象数组返回附近可用红外设备的相关信息。这个方法唯一的参数表示如果附近有许多红外设备时,最多收集多少个设备的信息。

  示例代码如下:

Dim taobjIrDADeviceInfo() As Sockets.IrDADeviceInfo
Dim tobjIrDAClient As New Sockets.IrDAClient
'开始检索附近的红外设备并加入到列表中,最多只搜索7个设备
taobjIrDADeviceInfo = tobjIrDAClient.DiscoverDevices(7)

  在得到的IrDADeviceInfo类型的变量有三个有用的属性,你可根据这些属性的值给用户提示,以便由用户选择应连接到那一个红外设备上:

  DeviceID 获取红外设备标识符,在连接红外设备中使用

  DeviceName 获取红设备名称。

  Hints 获取设备的类型,例如是红外手机还是红外PDA之类的说明

  (二)、用IrDAClient对象实现红外连接及数据发送

  IrDAClient和一般的网络连接对象的使用很相似,使用的基本步骤如下:

  1)、先使用connect方法连接指定的红外设备,这个方法需要一个IrDAEndPoint对象作为参数(这个对象指定需连接的红外设备,以及连接到红外设备提供的指定服务上)

  示例:

Dim tobjIrDAClient As New Sockets.IrDAClient
'ni_objIrDADeviceInfo是上文介绍的IrDADeviceInfo对象,以下语句的含义就是连接到'DeviceID指定的红外设备上,这个设备提供OBEX服务。
Dim tobjIrDAEndPoint As New IrDAEndPoint(ni_objIrDADeviceInfo.DeviceID, "OBEX")
Try
tobjIrDAClient.Connect(tobjIrDAEndPoint)
Catch ex As Exception
MessageBox.Show("IrDA Socket Connect 状态失败")
End Try

  2)、连接成功后就可以使用IrDAClient对象的GetStream 方法获得基础数据流,并利用流Read/Write方法实现数据传输的接收/发送。

  示例:两个方法的第一个参数都是一个字节数组

  数据发送:tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)

  数据接收:tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, 7)

  3)、完成所有通信后使用流以及IrDAClient的Close方法 关闭连接。

  示例:tobjIrDAClient.GetStream.Close()
     tobjIrDAClient.Close()

  (三)完整实现代码

  其实红外操作在net中实现就是这么简单,不过困难的是对OBEX协议的解释,基于代码重用的原则,我的程序主框架有两个基础类,clsIrDAOBEXOperator封装了文件发送的方法,方法中调用另一个类clsOBEXHeaders,这个类主门用来构建obex协议需要的数据包。代码如下,其中已包括必要的注释:

  clsIrDAOBEXOperator.vb的代码(代码中fnSendFileToIrDADevice是向红外设备发送文件的方法,它的第一个参数是代表红外设备的IrDADeviceInfo对象,第二个参数就是要发送的文件名),:

Imports System.Net
Imports System.Net.Sockets

Public Class clsIrDAOBEXOperator
'OBEX常数
Public Const cOBEX_VERSION As Byte = &H10
Public Const cOBEX_CONNECTFLAGS As Byte = &H0

Public Const cOBEX_CONNECT As Byte = &H80
Public Const cOBEX_DISCONNECT As Byte = &H81
Public Const cOBEX_PUT As Byte = &H2
Public Const cOBEX_NAME As Byte = &H1
Public Const cOBEX_PUT_FINAL As Byte = &H82
Public Const cOBEX_LENGTH As Byte = &HC3
Public Const cOBEX_BODY As Byte = &H48
Public Const cOBEX_END_OF_BODY As Byte = &H49

Public Const cOBEX_CONTINUE As Byte = &H90
Public Const cOBEX_SUCCESS As Byte = &HA0


'向外界公布可直接修改的变量
Public m_i32OBEXMAXSendBufferLen As Int32 = 255
Public m_i32OBEXMAXReceiveBufferLen As Int32 = 1024
Public m_i32OBEXMAXPacketSize As Int16 = &H808

'将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处(低位字在低字节,高位字在高字节)
Private Function fnInt16ToByteArray(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean
ni_abytDest(ni_i32StartIndex) = ni_i16Convert And &HFF
ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert >> 8

End Function

'将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处
Private Function fnInt16ToByteArrayLowByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean
ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert And &HFF
ni_abytDest(ni_i32StartIndex) = ni_i16Convert >> 8

End Function

'将一个32位整数转为4个字节的数组,并存入参数中指定索引开始的字节数组处
Private Function fnInt32ToByteArrayLowByteToHightPosition(ByVal ni_i32Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean
Dim ti32Tempa As Int32 = ni_i32Convert
ni_abytDest(ni_i32StartIndex + 3) = ni_i32Convert And &HFF
ni_abytDest(ni_i32StartIndex + 2) = (ni_i32Convert And &HFF00) >> 8
ni_abytDest(ni_i32StartIndex + 1) = (ni_i32Convert And &HFF0000) >> 16
ni_abytDest(ni_i32StartIndex) = (ti32Tempa And &HFF000000) >> 24

End Function


'将本设备中指定的文件传到指定的设备中去
Public Function fnSendFileToIrDADevice(ByVal ni_objIrDADeviceInfo As IrDADeviceInfo, _
ByVal ni_strFilePath As String) As Boolean
'ni_objIrDADeviceInfo:指定红外设备的IrDAInfo结构
'ni_strFilePath:要传输的文件路径
'返回值:成功返回true

Dim tobjIrDAClient As New Sockets.IrDAClient
Dim tobjIrDAEndPoint As New IrDAEndPoint(ni_objIrDADeviceInfo.DeviceID, "OBEX")
Dim tabytSendBuffer(m_i32OBEXMAXSendBufferLen * 2) As Byte
Dim tabytReceiveBuffer(m_i32OBEXMAXReceiveBufferLen) As Byte
Dim tblnTemp As Boolean
Dim ti32Tempa As Int32, ti32TempB As Int32, tieTempC As Int32
Dim tbytTempa As Byte
Dim ti32LoopA As Int32
Dim tobjBinaryReader As IO.BinaryReader
Dim tobjFileStream As IO.FileStream
Dim tabytTempa() As Byte, tabytTempb() As Byte, tabytTempC() As Byte, tabytTempD() As Byte
Dim tabytTempE() As Byte
Dim tobjOBEX_HEADERS As New clsOBEXHeaders
Dim ti32SendPackageSpare As Int32 '保存最大发送包数组-当前数据后的剩余空间
Dim tobjFileInfo As IO.FileInfo

tobjFileInfo = New IO.FileInfo(ni_strFilePath)

Try

Try
tobjIrDAClient.Connect(tobjIrDAEndPoint)

Catch ex As Exception
MessageBox.Show("IrDA Socket Connect 状态失败")
Return False
End Try

'创建一个最简单的连接信息
tabytTempa = tobjOBEX_HEADERS.fnCreateConnectHeaderRequest(, , , m_i32OBEXMAXPacketSize)

Try
tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)
tobjIrDAClient.GetStream.Flush()

Catch ex As Exception
MessageBox.Show("OBEX_CONNECT信息发送失败")
Application.DoEvents()
Return False
End Try

If tobjIrDAClient.GetStream.CanRead = True Then
Try
tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, 7)

Catch ex As Exception
MessageBox.Show("接收OBEX_CONNECT响应消息失败")
Return False
End Try

If tabytReceiveBuffer(0) <> cOBEX_SUCCESS Then
MessageBox.Show("OBEX_CONNECT没接收到相应的响应")
Return False
End If

End If

'发送put包
'Name包
tabytTempa = tobjOBEX_HEADERS.fnCreateNameHeader(IO.Path.GetFileName(ni_strFilePath))
ti32Tempa = tabytTempa.Length

'lenght包
tabytTempb = tobjOBEX_HEADERS.fnCreateLengthHeader(tobjFileInfo.Length)
ti32TempB = tabytTempb.Length


'时间
tabytTempC = tobjOBEX_HEADERS.fnCreateTimeHeaderISO(Now)

ReDim Preserve tabytTempa(tabytTempa.Length + tabytTempb.Length + tabytTempC.Length)

tabytTempb.CopyTo(tabytTempa, ti32Tempa)
tabytTempC.CopyTo(tabytTempa, ti32Tempa + ti32TempB)
tabytTempa = tobjOBEX_HEADERS.fnCreatePutHeader(tabytTempa)

'将名称、时间、文件长度等基本信息先发出去
Try
tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)
Catch ex As Exception
Return False
End Try

Try
tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length)
Catch ex As Exception
'接收服务器端响应时失败
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False

End Try

If tabytReceiveBuffer(0) <> cOBEX_SUCCESS And tabytReceiveBuffer(0) <> cOBEX_CONTINUE Then
'没有收到服务器的对应的响应信息
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End If


'开始正式发送文件的数据
Try
tobjFileStream = IO.File.Open(ni_strFilePath, IO.FileMode.Open, IO.FileAccess.Read)
tobjBinaryReader = New IO.BinaryReader(tobjFileStream)

Catch ex As Exception
MessageBox.Show("打开文件时出现错误")
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try

'读出文件的数据放到body中,总长度必须少于最大发送长度-put标志的3字节-body标志的3字节
'最多可发送的文件信息长度
ti32SendPackageSpare = m_i32OBEXMAXSendBufferLen - 6

'循环读取文件的内容,并发送到服务方
For ti32LoopA = 1 To Math.Ceiling(tobjFileInfo.Length / ti32SendPackageSpare)
Try
tabytTempa = tobjBinaryReader.ReadBytes(ti32SendPackageSpare)
Catch ex As Exception
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try

'构建合适的put包并发送
tabytTempb = tobjOBEX_HEADERS.fnCreateBodyHeader(tabytTempa) 'body标志
tabytTempa = tobjOBEX_HEADERS.fnCreatePutHeader(tabytTempb)

'开始发送
Try
tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)
tobjIrDAClient.GetStream.Flush()

Catch ex As Exception
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try

Try
tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length)
Catch ex As Exception
'接收服务器端响应时失败
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False

End Try

If tabytReceiveBuffer(0) <> cOBEX_SUCCESS And tabytReceiveBuffer(0) <> cOBEX_CONTINUE Then
'没有收到服务器的对应的响应信息
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End If

Next

'关闭文件
tobjBinaryReader.Close()
tobjFileStream.Close()

If ti32Tempa Then

End If
'文件数据已全部发送完毕,发送结束的final_put 包
'endbody包
tabytTempa = tobjOBEX_HEADERS.fnCreateEndOfBodyHeader(Nothing)
'putfinal
tabytTempa = tobjOBEX_HEADERS.fnCreatePut_FinalHeaderResponse(tabytTempa)
Try
tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)
tobjIrDAClient.GetStream.Flush()
Catch ex As Exception
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try


Try
tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length)
Catch ex As Exception
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try

If tabytReceiveBuffer(0) <> cOBEX_SUCCESS Then
'没有收到服务器的对应的响应信息
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End If

'发送disconnect包
tabytTempa = tobjOBEX_HEADERS.fnCreateDisConnectHeaderRequest()
Try
tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length)
tobjIrDAClient.GetStream.Flush()
Catch ex As Exception
tobjIrDAClient.GetStream.Close()
tobjIrDAClient.Close()
Return False
End Try

tobjIrDAClient.Close()

Catch ex As Exception
MessageBox.Show("过程发生不明错误")
Return False
End Try


'代码能来到这儿就是正常完成任务了
MsgBox("红外操作成功")
Return True

End Function
End Class
clsOBEXHeaders.vb文件的代码:
Public Class clsOBEXHeaders
#Region "OBEX常数"
Public Const cOBEX_VERSION As Byte = &H10
Public Const cOBEX_CONNECTFLAGS As Byte = &H0

Public Const cOBEX_CONNECT As Byte = &H80
Public Const cOBEX_DISCONNECT As Byte = &H81
Public Const cOBEX_PUT As Byte = &H2
Public Const cOBEX_NAME As Byte = &H1
Public Const cOBEX_PUT_FINAL As Byte = &H82
Public Const cOBEX_LENGTH As Byte = &HC3
Public Const cOBEX_BODY As Byte = &H48
Public Const cOBEX_END_OF_BODY As Byte = &H49
Public Const cOBEX_TIME_ISO As Byte = &H44

Public Const cOBEX_CONTINUE As Byte = &H90
Public Const cOBEX_SUCCESS As Byte = &HA0

#End Region

#Region "类内私有函数"

'将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处
Private Sub sbInt16ToByteArrayLowByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32)

ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert And &HFF
ni_abytDest(ni_i32StartIndex) = ni_i16Convert >> 8

End Sub

''将一个16位整数转为2个字节的数组,高位字放入高字节,并存入参数中指定索引开始的字节数组处
'Private Sub sbInt16ToByteArrayHightByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32)

' ni_abytDest(ni_i32StartIndex) = ni_i16Convert And &HFF
' ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert >> 8

'End Sub

'将一个32位整数转为4个字节的数组,并存入参数中指定索引开始的字节数组处
Private Sub sbInt32ToByteArrayLowByteToHightPosition(ByVal ni_i32Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32)

Dim ti32Tempa As Int32 = ni_i32Convert
ni_abytDest(ni_i32StartIndex + 3) = ni_i32Convert And &HFF
ni_abytDest(ni_i32StartIndex + 2) = (ni_i32Convert And &HFF00) >> 8
ni_abytDest(ni_i32StartIndex + 1) = (ni_i32Convert And &HFF0000) >> 16
ni_abytDest(ni_i32StartIndex) = (ti32Tempa And &HFF000000) >> 24

End Sub

#End Region

'创建一个新的ConnectHeader客户端请求信息
Public Function fnCreateConnectHeaderRequest(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing, _
Optional ByVal ni_bytOBEX_Version As Byte = cOBEX_VERSION, _
Optional ByVal ni_bytFlags As Byte = cOBEX_CONNECTFLAGS, _
Optional ByVal ni_i16MaxPacketLength As Int16 = 255) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then
ReDim tabytFuncReturn(7 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(6)
End If

tabytFuncReturn(0) = cOBEX_CONNECT
tabytFuncReturn(3) = ni_bytOBEX_Version
tabytFuncReturn(4) = ni_bytFlags
sbInt16ToByteArrayLowByteToHightPosition(ni_i16MaxPacketLength, tabytFuncReturn, 5)

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入connect包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(7, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 7)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 7, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

'创建一个新的ConnectHeader的服务器端响应信息数据
Public Function fnCreateConnectHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing, _
Optional ByVal ni_bytOBEX_Version As Byte = cOBEX_VERSION, _
Optional ByVal ni_bytFlags As Byte = cOBEX_CONNECTFLAGS, _
Optional ByVal ni_i16MaxPacketLength As Int16 = 255) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then
ReDim tabytFuncReturn(7 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(6)
End If

tabytFuncReturn(0) = cOBEX_SUCCESS
tabytFuncReturn(3) = ni_bytOBEX_Version
tabytFuncReturn(4) = ni_bytFlags
sbInt16ToByteArrayLowByteToHightPosition(ni_i16MaxPacketLength, tabytFuncReturn, 5)

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入connect包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(7, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 7)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 7, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function


'创建一个新的DisConnectHeader客户请求信息
Public Function fnCreateDisConnectHeaderRequest(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

tabytFuncReturn(0) = cOBEX_DISCONNECT

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

'创建一个新的ContinueHeader信息
Public Function fnCreateContinueHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

tabytFuncReturn(0) = cOBEX_CONTINUE

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

'创建一个新的SuccessHeader信息
Public Function fnCreateSuccessHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

tabytFuncReturn(0) = cOBEX_SUCCESS

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

'创建一个新的TimeHeader信息
Public Function fnCreateTimeHeaderISO(ByVal ni_dtmDate As Date, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte
Dim tstrDate As String
Dim tabytTempa() As Byte

'tstrDate = ni_dtmDate.Year & "." & ni_dtmDate.Month & "." & ni_dtmDate.Day & "T" & _
' ni_dtmDate.Hour & ":" & ni_dtmDate.Minute & ":" & ni_dtmDate.Second & "Z" & ControlChars.NullChar

tstrDate = String.Format("{0:yyyy/MM/dd\THH:mm:ss\Z}", ni_dtmDate) & ControlChars.NullChar
tabytTempa = Text.Encoding.BigEndianUnicode.GetBytes(tstrDate)


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + tabytTempa.Length + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2 + tabytTempa.Length)
End If

tabytFuncReturn(0) = cOBEX_TIME_ISO

tabytTempa.CopyTo(tabytFuncReturn, 3) '将时间字节串放入返回数组中

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3 + tabytTempa.Length, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3 + tabytTempa.Length, tabytFuncReturn, 1)
End If

Return tabytFuncReturn


End Function

'创建一个新的BodyHeader信息
Public Function fnCreateBodyHeader(ByVal ni_abytBodyContent() As Byte, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte
Dim tabytTempa As Byte
Dim ti32BodyContentLength As Int32 = 0


'定义合适大小的数组以容纳bodyheader的内容
If Not (ni_abytBodyContent Is Nothing) Then '有body的内容
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytBodyContent.Length + ni_abytOtherHeaders.Length - 1)
ni_abytBodyContent.CopyTo(tabytFuncReturn, 3)
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, ni_abytBodyContent.Length + 3)
Else
ReDim tabytFuncReturn(2 + ni_abytBodyContent.Length)
ni_abytBodyContent.CopyTo(tabytFuncReturn, 3)
End If

Else '没有body的内容
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)

Else
ReDim tabytFuncReturn(2)
End If

End If

tabytFuncReturn(0) = cOBEX_BODY

'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(tabytFuncReturn.Length, tabytFuncReturn, 1)

Return tabytFuncReturn


End Function

'创建一个新的EndOfBodyHeader信息
Public Function fnCreateEndOfBodyHeader(ByVal ni_abytBodyContent() As Byte, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte
Dim tabytTempa As Byte
Dim ti32BodyContentLength As Int32 = 0


If Not (ni_abytBodyContent Is Nothing) Then
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytBodyContent.Length + ni_abytOtherHeaders.Length - 1)
ni_abytBodyContent.CopyTo(tabytFuncReturn, 3)
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3 + ni_abytBodyContent.Length)
Else
ReDim tabytFuncReturn(2 + ni_abytBodyContent.Length)
ni_abytBodyContent.CopyTo(tabytFuncReturn, 3)
End If

Else '没有body内容
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)

Else
ReDim tabytFuncReturn(2)
End If

End If

'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(tabytFuncReturn.Length, tabytFuncReturn, 1)

tabytFuncReturn(0) = cOBEX_END_OF_BODY

Return tabytFuncReturn


End Function

'创建一个新的LengthHeader信息
Public Function fnCreateLengthHeader(ByVal ni_i32Length As Int32) As Byte()
Dim tabytFuncReturn() As Byte

ReDim tabytFuncReturn(4)

tabytFuncReturn(0) = cOBEX_LENGTH

'加入包的内容
Call sbInt32ToByteArrayLowByteToHightPosition(ni_i32Length, tabytFuncReturn, 1)

Return tabytFuncReturn

End Function

'创建一个新的NameHeader信息
Public Function fnCreateNameHeader(ByVal ni_strName As String, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte
Dim tabytTempa() As Byte
Dim ti32BodyContentLength As Int32 = 0


tabytTempa = Text.Encoding.BigEndianUnicode.GetBytes(ni_strName & ControlChars.NullChar)


If ni_strName <> "" Then
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + tabytTempa.Length + ni_abytOtherHeaders.Length - 1)
Else '没有其它数据包附后
ReDim tabytFuncReturn(2 + tabytTempa.Length)
End If

Else
If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面

ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

End If



tabytFuncReturn(0) = cOBEX_NAME

If ni_strName <> "" Then

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3 + tabytTempa.Length, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + tabytTempa.Length + 3, tabytFuncReturn, 1)
End If
tabytTempa.CopyTo(tabytFuncReturn, 3)

Else '没有实际的body数据

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

End If

Return tabytFuncReturn

End Function

'创建一个新的PutHeader信息
Public Function fnCreatePutHeader(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

tabytFuncReturn(0) = cOBEX_PUT

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

'创建一个新的PutHeader信息
Public Function fnCreatePut_FinalHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte()
Dim tabytFuncReturn() As Byte


If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面
ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1)
Else
ReDim tabytFuncReturn(2)
End If

tabytFuncReturn(0) = cOBEX_PUT_FINAL

If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了
'加入包的长度
Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1)
Else '还有其它头信息
ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3)
Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1)
End If

Return tabytFuncReturn

End Function

End Class

  以上代码均为VB.net 2003编写,在我的多普达515手机加红外适配器上测试通过,不过红外传输是一个相对比较专业的课题,我的代码并不是足够健壮和可靠,希望对这方面有研究的朋友与我探讨。我的QQ:85403578,邮箱:missilecat@163.com。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章