2010年4月7日 星期三

Bluetooth sample code

在Bluetooth 的傳輸的Protocl上.包括 RFCOMM 和 Object Exchange (OBEX) 兩種方式.底下就protocl 部分做個簡單的介紹 Ref: http://funningboy.blogspot.com/2010/04/bluetooth-case-study.html 藍芽通訊協定stack
  • HCI 主機控制器介面(Host Controller Interface). 主機(電腦)和控制器(藍芽設備)之間的介面.
  • L2CAP 邏輯連結控制器適配協定(Logical Link Controller Adaptation Protocol),這一層充當其他所有層的資料多工器.
  • BNEP 即藍芽網路封裝協定(Bluetooth Network Encapsulation Protocol) , 可以在藍芽上執行其他網路協定, 例如 IP、TCP 和 UDP.
  • RFCOMM 稱作虛擬串流通訊協定(virtual serial port protocol),因為它允許藍芽設備類比串流的功能
  • OBEX 通訊協定層是在 RFCOMM 層上面實作, 把資料以物件 "檔" (data) 的形式傳輸
  • SDP 是服務發現協定(Service Discovery Protocol)層,用於在遠端藍芽設備上尋找服務
  • AVCTP 和 AVDTP,用於藍芽上音頻和視頻的控制.

底下用 jsr82 來實現 RFCOMM.

Server.java

import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;

public class RFCOMMServer {

    public static void main( String args[] ) {
        try {
            String url = "btspp://localhost:" +
                new UUID( 0x1101 ).toString() +
                ";name=SampleServer";
            StreamConnectionNotifier service =
                (StreamConnectionNotifier) Connector.open( url );

            StreamConnection con =
                (StreamConnection) service.acceptAndOpen();
            OutputStream os = con.openOutputStream();
            InputStream is = con.openInputStream();

            String greeting = "JSR-82 RFCOMM server says hello";
            os.write( greeting.getBytes() );

            byte buffer[] = new byte[80];
            int bytes_read = is.read( buffer );
            String received = new String(buffer, 0, bytes_read);
            System.out.println("received: " + received);
         
            con.close();
        } catch ( IOException e ) {
            System.err.print(e.toString());
        }
    }
}

Client.java

import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;

public class RFCOMMClient {

    public static void main( String args[] ) {
        try {
            String url = "btspp://0123456789AB:3";

            StreamConnection con =
                (StreamConnection) Connector.open(url);

            OutputStream os = con.openOutputStream();
            InputStream is = con.openInputStream();

            String greeting = "JSR-82 RFCOMM client says hello";
            os.write( greeting.getBytes() );

            byte buffer[] = new byte[80];
            int bytes_read = is.read( buffer );
            String received = new String(buffer, 0, bytes_read);
            System.out.println("received: " + received);
         
            con.close();
        } catch ( IOException e ) {
            System.err.print(e.toString());
        }
    }
}

請注意 "url" 要改成你的 address & port.

如不知道,可用jsr 82所提供的 ServiceSearch.java

Ref: http://www.jsr82.com/

結果: BlueCove version 2.1.0 on winsock wait for device inquiry to complete... Device 001D289E7AFE found name K530i Device 2421AB354DC5 found Device 001E45B4779B found name Z610i Device Inquiry completed! 3 device(s) found BlueCove stack shutdown completed

1 則留言:

  1. The specification does not address nor provide APIs for the following:
    1. Audio (voice) transmissions
    2. Telephony Control Protocol – Binary (TCS Binary or TCS-BIN)
    The API is intended to provide the following capabilities:
    1. Register services
    2. Discover devices and services
    3. Establish RFCOMM, L2CAP and OBEX connections
    4. Conduct these activities in a secure fashion

    回覆刪除