放眼市場(chǎng)上各種各樣的JSR82 MIDlets,有一點(diǎn)需要注意,一些MIDlets并沒有以一種合適的方式處理服務(wù)發(fā)現(xiàn)協(xié)議(SDP)記錄。在藍(lán)牙領(lǐng)域內(nèi)SDP記錄是非常難以領(lǐng)會(huì)的,但是在JSR82中并沒有這么困難。
這篇短小的文章會(huì)就SDP記錄的一般問題給予一些建議。
我們先簡(jiǎn)要地看看為什么需要SDP記錄。SDP記錄是一種用來確認(rèn)設(shè)備上的一種特定服務(wù)的記錄。沒有SDP記錄,一部移動(dòng)電話就不可能檢測(cè)到另一個(gè)設(shè)備上的服務(wù)。大部分的電話擁有一個(gè)以上的SDP記錄,例如,它們很可能有對(duì)于L2CAP和串口的記錄。
關(guān)于SDP記錄最主要的問題的就是很多開發(fā)者忘記了從數(shù)據(jù)庫中刪除SDP記錄。這就導(dǎo)致了最終用戶不能重新連接來運(yùn)行游戲。
下面兩張簡(jiǎn)單的圖片說明了這個(gè)問題:
圖1 SDP連接成功
如圖1所示兩個(gè)MIDlets正在試圖進(jìn)行連接,并且連接成功。
圖2 SDP連接失敗
在圖2中我們可以看到,一個(gè)MIDlet再一次嘗試連接(可能是同一用戶或者一個(gè)新用戶)。連接失敗,因?yàn)檫@個(gè)MIDlet試圖連接的SDP記錄沒有監(jiān)聽器,所以Bluetooth棧除了拒絕連接別無選擇。經(jīng)常發(fā)生的情況是,客戶端MIDlet接收到兩者的SDP記錄,但是只選擇第一個(gè)進(jìn)行連接,因?yàn)樗幌M谝粋€(gè)服務(wù)器上有一個(gè)SDP記錄。
在下面的代碼示例中了,展示了一個(gè)簡(jiǎn)單的服務(wù)器。這個(gè)例子關(guān)注了必須的close()調(diào)用。
例子:
public class SEMCSPPServer extends Thread
{
PRivate StreamConnectionNotifier server = null;
private StreamConnection sppConnection = null;
public SEMCSPPServer()
{
// This will create create an SDP record in the dB
try
{
server = (StreamConnectionNotifier)Connector.open( "BTspp://localhost:ea
}
catch( Exception e ) {}
}
public void run()
{
// Wait for connections
try
{
sppConnection = server.acceptAndOpen();
}
catch( Exception e ) { e.printStackTrace(); }
// Let the server do something fun here
try
{
// Open the Streams to be used for communications
InputStream in = sppConnection.openInputStream();
OutputStream out = sppConnection.openOutputStream();
// Let the server do something fun here
while()
{
}
// Server is done, now cleanup
// Close the Streams
try
{
in.close();
}
catch( IOException ioe ) {}
try
{
out.close();
}
catch( IOException ioe ) {}
in = null;
out = null;
}
catch( Exception e ) {}
// Close the Connection
try
{
sppConnection.close();
}
catch( IOException ioe ) {}
sppConnection = null;
// To make it possible for a client to re-connect
// we need to remove the current SDP record
// If the MIDlet ends here we SHOULD still
// close the notifier, but the MIDlet environment will
// clean-up after us
server.close();
} // run
}
自然地,你就擁有了幾種類型不同的服務(wù)器管理者,它們管理所有的服務(wù)器連接,并且使SDP記錄重新利用,例如,有一種服務(wù)器管理者始終監(jiān)聽連接。例如,在一個(gè)多玩家的藍(lán)牙游戲中允許玩家隨時(shí)進(jìn)入和退出。
服務(wù)器管理者例子:
// A simple server handler
public class SEMCSPPServerHandler
{
private StreamConnectionNotifier server = null;
private StreamConnection sppConnection = null;
public SEMCSPPServerHandler()
{
// This will create create an SDP record in the dB
try
{
server = (StreamConnectionNotifier)Connector.open( "btspp://localhost:ea
}
catch( Exception e ) {}
while( true )
{
// Wait for connections
try
{
sppConnection = server.acceptAndOpen();
}
catch( Exception e ) { e.printStackTrace(); }
if( sppConnection != null )
{
SEMCSPPServer sp = new SEMCSPPServer( sppConnection );
sp.start();
sp = null;
}
// The server handler is now ready to deal with new connections
// Note, there is no need to create a new SDP record
}
// Remove the SDP record
server.close();
}
}
// A simple server class to deal with 1 connection
public class SEMCSPPServer extends Thread
{
private StreamConnection sppConnection = null;
public SEMCSPPServer( StreamConnection sppConnection )
{
this.sppConnection = sppConnection;
}
public void run()
{
try
{
// Open the Streams to be used for communications
InputStream in = sppConnection.openInputStream();
OutputStream out = sppConnection.openOutputStream();
// Let the server do something fun here
while()
{
}
// Server is done, now cleanup
// Close the Streams
try
{
in.close();
}
catch( IOException ioe ) {}
try
{
out.close();
}
catch( IOException ioe ) {}
in = null;
out = null;
}
catch( Exception e ) {}
// Close the Connection
try
{
sppConnection.close();
}
catch( IOException ioe ) {}
sppConnection = null;
// The server is no longer active
} // run
}
需要學(xué)習(xí)的經(jīng)驗(yàn)
如果Connector. open()調(diào)用沒有很好的管理,除非退出MIDlet(這種情況下SDP數(shù)據(jù)庫被在一個(gè)清空)否則要重新連接到那個(gè)SDP記錄是不可能的。現(xiàn)實(shí)生活中,你必須要退出游戲然后重新啟動(dòng),這將會(huì)使最終用戶灰心地離開。
當(dāng)然,在你可以的應(yīng)用中可能包括多于一個(gè)的SDP記錄,但是對(duì)于適當(dāng)?shù)墓δ苄孕枨笠_保MIDlet監(jiān)聽所有的記錄。
原文地址:http://developer.sonyeriCSSon.com/site/global/techsupport/tipstrickscode/java/p_advice_bluetooth_sdp_game+server.jsp
(出處:http://www.companysz.com)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注