国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.NET利用OBEX協議實現紅外線文件傳輸

2024-07-10 13:07:13
字體:
來源:轉載
供稿:網友
  十一月開始沒有做什么項目,每天站在在辦公室窗前靜靜地看著樓下的行人來來往往,然后等待著領工資的時刻。為了讓智慧的大腦不因無所事是而僵硬,我決定找點傷腦筋的事情來維持腦袋的正常運轉。經過考慮,決定用vb.net實現紅外線文件傳輸的例程。

  一、obex協議淺析

  目前的紅外線傳輸大都遵循obex協議,這是由微軟、蘋果、諾基亞等公司專門為紅外線傳輸而制定的一整套協議規則。最新協議版本是1.3版,在官方網站上下載要20美元(有錢的可以去下,我反正是玩玩,叫我交錢是不可能的,本文中實現的依據是在網上找到的obex協議1.2版本的文檔)。協議文檔的第二章obex object model是關鍵部份,實現文件傳輸必須對這章說明仔細研究清楚。以下先就對這章的一些關鍵點進行講解。

  1、obex協議的對象模型

  1)obex協議使用一系列的數據包(header)來進行某種對象(通常是文件)的傳輸,其基本格式是這樣的:

  
'數據包的標識
  
'數據包內的數據

  其中
是個單字節(八位二進制)字符,這個字符的低六位標識數據包代表的意義,高兩位表示這個數據包的總長度的表達方式,如下表:

高倆位二進制數據意義
00這個數據包的
是一個以空字符結尾的unicode字符串
01 這個數據包的
是一個以空字符結尾的單字節組成的字符串,
的前兩個字節數據組成的16位整數表示整個數據包的長度(包括
的總長)
10
的長度只有一個字節數據
11
的長度只有四個字節數據,并以網格數據格式排列(高位數據放在低位字節中存儲)

  注意:在
的16位數據(如包的長度、unicode字符在發送方均要做高位字放在低位字發送的處理。由于沒注意這個問題,我曾在開頭的四五天時間里嘔血數升而一直沒有成功將數據發送成功)

  在應用中,數據包可以嵌套。也就是:header value可以包含其它的數據包,所以長度標識非常重要,它可以幫助軟件的實現根據包的長度迅速分離出包內的數據。

  在本文實現中主要用到的數據包標識如下(其余的項請參閱詳細官方協議):

  常用數據包標識列表

十六進制值 標識名稱 標識含義
0x01name 標記對象的名稱(通常是文件的文件名)
0xc3length 以字節為單位計算的對象長度
0x44 time時間(以iso 8601規范為標準)
0x480x49bodyend of body標識一個對象數據塊的開始標識這是對象的最后一個數據塊

  obex協議數據對象傳輸是按照服務器端/客戶端的方式進行的,每個操作均提供一個操作碼以明確操作的含義。以下給出部分數據發送所需操作碼列表:

0x80connect標識申請開始一個對象傳輸會話,并可以在這個數據包中告知紅外接收方一些必要的兼容性信息。
0x81 disconnect 標識對象傳輸會話結束
0x020x82putfinal_put 發送對象的put動作(當標識為0x82時說明這是最后的一個put動作)
0xa0 success 說明接收端已成功收到put動作發送的所有數據(一般是在成功收到final_put標識的數據包后的反饋)
0x90 continue 說明接收端已收到put動作發送的數據,因為final_put還沒出現,所以要求發送端繼續發送數據。

  發送方和接收方是的通信的基本格式如下:

字節0 字節1,2 字節三以后的數據
操作碼 整個通信數據包的長度通訊的數據

  以下結合基本傳輸步驟,對上文列出的數據包使用方法進行講解:

  1、由發送方向接收文件的接收方進行連接請求。

  發送方需要使用的操作碼為connect,在obex協議中對connect數據包格式作如下規定:

字節0 字節1、2字節3字節4 字節5、6字節7
connect操作碼(0x80)connect數據包的總長度obex協議的版本(目前為1.0,16進制表示為0x10)保留未用,設為0最大可處理的obex包長度其它的數據包(可選)

  服務端根據連接的請求向客戶端做出響應:

字節0 字節1、2字節3 字節4 字節5、6 字節7
響應的操作碼 響應數據包的總長度obex協議的版本(目前為1.0,16進制表示為0x10)保留未用,設為0最大可處理的obex包長度其它的數據包(可選)

  如果接收方允許連接,響應的操作碼會為success(0xa0)其它的響應操作碼均被認為連接失敗。

  2、發送方向接收方發送數據

  發送方通過put和final_put這兩個操作將傳輸的數據信息向接收方發送。

  發送方的put/final_put使用格式。

字節0 字節1,2 字節三以后的數據
put操作碼(0x02)final_put操作碼(0x82)整個通信數據包的長度 通訊的數據(由其它的例如name/body等數據包構成)

  當最后一次傳輸對象的數據時要使用final_put告知接收方這是最后一個數據包了,以便接收方根據接收到的數據進行處理(例如將接收到的文件存盤)。

  接收方的響應格式:

字節0 字節1,2 字節三以后的數據
響應操作碼典型的有兩個值:success操作碼(0xa0)continue操作碼(0x90 )整個通信數據包的長度 通訊的數據(由其它的例如name/body等數據包構成)

  如果接收方成功收到put操作的數據,應該返回continue開始的響應信息,告知發送方繼續發送,在成功收到final_put發送的數據后,應返回success響應信息,告知發送方整個對象都接收完成。(但由于部分適配器只會返回success響應,出于兼容性考慮,在編程中應理解為無論接收到success或continue響應都代表數據發送成功。)

  3、發送方關閉連接

  發送方在最后應以disconnect信息明確結束連接,這并不是必須的,但推薦使用。

  發送方格式:

字節0字節1,2 字節三以后的數據
disconnect操作碼整個通信數據包的長度可選的通訊的數據(由其它的數據包構成)

  接收方響應:

字節0 字節1,2字節三以后的數據
success操作碼(0xa0)服務不可用操作碼(0xd3)整個通信數據包的長度可選的通訊的數據(由其它的數據包構成)

  當成功斷開時,接收方會發送success信息。否則,如果發送方的disconnect操作包含錯誤信息(例如一個錯誤的連接id)時,會返加0xd3操作碼。但并不能操作所有接收方的程序都實現這個響應的功能,也就是說,發送方并不能保證一定會得到響應信息。
  二、程序實現

  .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,郵箱:[email protected]。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临桂县| 镶黄旗| 宜兰市| 蒲江县| 镇平县| 天气| 花莲县| 华容县| 邯郸县| 浮山县| 津南区| 桐庐县| 扬中市| 石渠县| 都匀市| 古交市| 红桥区| 古丈县| 潮州市| 聊城市| 司法| 张家口市| 屏山县| 星子县| 扶余县| 太谷县| 含山县| 嘉荫县| 原阳县| 昭苏县| 营口市| 湖北省| 舒城县| 天气| 北宁市| 安陆市| 绵竹市| 屏山县| 玉门市| 潮州市| 革吉县|