BlackBerry 蓝牙编程(三)

作者: 俞伟

 

蓝牙 务器

 

首先启 蓝牙

 

1 .     第一步 L o cal D e v ice:

LocalDevice device = LocalDevice. getLocalDevice ();

 

2 .     第二步 置搜 并设 置搜索 式为 局模

device.setDiscoverable(DiscoveryAgent. GIAC );

 

3 .     启动蓝 服务:

三种连 方式   S P P , L 2 CA P , OBEX ,根 条件 同服务。

   

public   void   startBluetoothService ( int   uuid){

_uuid   =   uuid;

try {

//   获取本 设备

LocalDevice   device = LocalDevice. getLocalDevice ();

int   mode   = device.getDiscoverable();

if   (mode   != DiscoveryAgent. GIAC ){

 

// 设置搜 为全局 式, 蓝牙设备

device.setDiscoverable(DiscoveryAgent. GIAC );

 

switch ( _uuid ){

 

case BluetoothJSR82Demo. SPP_UUID :

// 启动 SPP 服务, StreamConnectio n

SPPServerThread   sppThread = new   SPPServerThread();

sppThread.start();

break ;

 

case BluetoothJSR82Demo. OPP_UUID :

// 启动 OPP 服务,     OBEX

OPPServerThread   oppThread = new   OPPServerThread();

oppThread.start();

break ;

 

case   BluetoothJSR82Demo. L2CAP_UUID :

// 启动 L2 CA P 服务 ,   L2CAPConnectio nNotifier

L2CAPServerThread   l2capThread = new   L2CAPServerThread();

l2capThread.start();

break ;

}

}

catch (BluetoothStateException   bse){

BluetoothJSR82Demo. errorDialog (bse.toString());

}

}

 

 

启用蓝牙 S PP 服务

 

蓝牙 SP P 基于 Str ea m Co nn ect i o n

 

1 .     首先创 服务 一标识 U UID

UUID uuid = new UUID( _uuid );

 

2 .     获取本 蓝牙 备,启动 服务

LocalDevice local = LocalDevice. getLocalDevice (); StreamConnectionNotifier service = (StreamConnectionNotifier) Connector. open ( "btspp://localhost:" + uuid + ";name=" + SERVICE_NAME_SPP );

connection   = service.acceptAndOpen();

 

3 .     获取输 输出 与客户端 进行读写

 

 

 

 

public   void runSPPService(){

try {

StreamConnection   connection = null ;

DataOutputStream os = null ; DataInputStream is = null ; try {

// 创建服 标识     UUID

UUID   uuid = new   UUID( _uuid );

// 获取本 设备

LocalDevice local = LocalDevice. getLocalDevice ();

 

// 创建 StreamCo nnectionNotifier

StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open ( "btspp://localhost:" + uuid + ";name=" +

SERVICE_NAME_SPP );

// 等待     St re am C on ne ct io n   客户端 链接

connection = service.acceptAndOpen();

 

// 获取输 取数据

is   = connection.openDataInputStream();

byte []   buffer = new   byte [1024];

int readBytes = is.read(buffer);

String   receivedMessage = new   String(buffer,   0, readBytes);

// 发送消 户端

String message = "/nJSR-82 SERVER says hello!" ; os = connection.openDataOutputStream(); os.write(message.getBytes());

os.flush();

}

finally {

os.close();

is.close();

}

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

}


 

启动蓝牙 L 2 C AP 服务

 

1 首先创建 UUID ,获取本 地连接

UUID uuid = new UUID( _uuid );

LocalDevice local = LocalDevice. getLocalDevice ();

 

2 打开 L 2 CA P 连接 等待 户端连接

L2CAPConnectionNotifier service = (L2CAPConnectionNotifier) Connector. open ( "btl2cap://localhost:" + uuid + ";name=" + SERVICE_NAME_L2CAP );

connection   = service.acceptAndOpen();

 

3 接收客 端消息

byte [] buffer = new byte [ 1024 ];

int   readBytes   = connection.receive(buffer);

String receivedMessage = new String(buffer, 0, readBytes);

 

4 向客户 发送

String message = "/nJSR-82 SERVER says hello!" ;

connection.send(message.getBytes());

 

 

 

public   void runL2CAPService (){

try {

L2CAPConnection connection = null ;

try {

// 创建 UUID 获取本 蓝牙

UUID   uuid = new   UUID( _uuid );

LocalDevice local = LocalDevice. getLocalDevice ();

 

// 打开 L2CAP 连接, 待客 端的

L2CAPConnectionNotifier service = (L2CAPConnectionNotifier) Connector. open ( "btl2cap://localhost:" + uuid + ";name=" +

SERVICE_NAME_L2CAP );

connection   = service.acceptAndOpen();

 

// 创建缓 读取数据

byte []   buffer = new   byte [   1024 ];

int   readBytes   = connection.receive(buffer);

String receivedMessage = new String(buffer, 0, readBytes);

 

// 向客户端发 送数据

String message = "/nJSR-82 SERVER says hello!" ;

connection.send(message.getBytes());

}

finally {

connection.close();

}

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

}

 

启动蓝牙 OBE X 服务

 

1 首先创 蓝牙 务唯一标 UUI D 并获 本地蓝 设备

UUID uuid = new UUID( _uuid );

LocalDevice local = LocalDevice. getLocalDevice ();

 

2 打开 OBE X 连接

SessionNotifier sessionNotifier = (SessionNotifier) Connector. open ( "btgoep://localhost:" + uuid + ";name=" + SERVICE_NAME_OPP );

 

3 注册 OBE X 器来处理 客户端

sessionNotifier.acceptAndOpen( new ObexServerRequestHandler());

   

public   void run(){

try {

// 创建服 唯一 UUID ,获取 地蓝 设备

UUID   uuid = new   UUID( _uuid );

LocalDevice local = LocalDevice. getLocalDevice ();

 

// 打开 OBEX 接并 等待客 OBEX 连接

SessionNotifier sessionNotifier = (SessionNotifier) Connector.open ( "btgoep://localhost:" + uuid + ";name=" + SERVICE_NAME_OPP );

 

// 注册 ObextServe rRequestHandler 来处 客户端

sessionNotifier.acceptAndOpen( new   ObexServerRequestHandler());

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

}

 

                  4 . 实现 OBE X 器接口 S e rverR equ est H and ler o n C o nn ect ( ) OB EX 接建 的时候 被调用 是处 连接相关 逻辑的 客户端有 数据通过 OBEX 连接输入 服务端 时, o n P u t() 用, 取数据 处理 同时 Operati o n 输出流 向客户端 发送数

 

 

 

 

class   ObexServerRequestHandler   extends   ServerRequestHandler{

 

// 当与客 端连 ,调用 函数

public   int onConnect(HeaderSet   request, HeaderSet reply){

updateStatus( "[SERVER]   OPP session created" );

return   ResponseCodes. OBEX_HTTP_OK ;

}

 

// 当接收 据和 该函 ,从 Op er a ti on 获取 InputStream, OutputStream

public   int onPut(Operation   op){

try {

InputStream is = op.openInputStream();

 

// 读取数据

byte   b[]   = new   byte [1024];

int   len;

while (is.available() > 0 && (len = is.read(b)) > 0){

updateStatus( new   String(b,   0, len));

}

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

return   ResponseCodes. OBEX_HTTP_OK ;

}

 

// 当断开 接时 该函数

public   void onDisconnect(HeaderSet   req, HeaderSet resp){

updateStatus( "[SERVER] OPP connection closed" );

}

}

 

 

 

BlackBerry SDK下载

 

 

  • BlackBerry Java Plug-in for Eclipse v1.1
  • Java Plug-in for Eclipse Update Site
  • BlackBerry Web Plug-in v2.0
  • BlackBerry Widget SDK v1.0
  • BlackBerry Theme Studio v5.0
  • Plazmic Content Developer’s Kit v4.7
  • BlackBerry smartphone simulators
  •  

    相关链接:

    BlackBerry 蓝牙编程(一)
    BlackBerry 蓝牙编程(二)

    BlackBerry 蓝牙编程(三)

     

    你可能感兴趣的:(BlackBerry 蓝牙编程(三))