麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

關(guān)于管理藍(lán)牙SDP記錄和游戲服務(wù)器之間連接的若干建議

2019-11-18 16:13:04
字體:
供稿:網(wǎng)友

放眼市場(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è)問題:

關(guān)于管理藍(lán)牙SDP記錄和游戲服務(wù)器之間連接的若干建議(圖一)

圖1  SDP連接成功

如圖1所示兩個(gè)MIDlets正在試圖進(jìn)行連接,并且連接成功。

關(guān)于管理藍(lán)牙SDP記錄和游戲服務(wù)器之間連接的若干建議(圖二)

圖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:ea834a8566aa4e0fb02ce4c1a53700c9;name=SomeServer" );
    }
    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:ea834a8566aa4e0fb02ce4c1a53700c9;name=SomeServer" );
    }
    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)



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩99 | 精品国产一区在线 | 一区国产在线观看 | 久久久一区二区精品 | 一级片999 | 国产免费观看视频 | 色爱99| 一本色道久久综合亚洲精品图片 | 久久福利剧场 | 国产精品免费观看视频 | 色婷婷a v| 欧美亚洲一区二区三区四区 | 国产精品欧美久久久久一区二区 | 国产一区二区三区高清 | 中文字幕在线观看网址 | 毛毛片在线看 | 国产精品自在线拍 | 成人免费网站在线观看视频 | 九九热九九热 | 久久国产精品久久精品国产演员表 | 免费一级片观看 | 一级黄色免费电影 | 亚洲视色 | jizzjizzjizz少妇 | 久久久精品视频免费 | 一区二区三区视频在线播放 | 日韩视频不卡 | 国内精品久久久久久久久久久久 | 亚洲精品久久久久久 | 美女污污视频在线观看 | 久久精品一区二区三区四区五区 | 国产精品成人亚洲一区二区 | 一级片九九| 国产精品色在线网站 | 欧美日韩一区二区综合 | 欧美日韩免费一区 | 国产成人精品区一区二区不卡 | 色综合久久久久综合99 | 日韩在线播放第一页 | 黄网站免费观看视频 | 国产高潮国产高潮久久久91 |