BlackBerry 蓝牙编程(二)

作者: 俞伟

 

搜索服务

 

搜索一 指定 需指 UUI D 组, UUI D 义了 连接方 ,和 ID U UID 括两 个标识 第一个 识表 示连接 式, 如:

 

连接方式

UUID

位数

SDP

0 x0 0 0 1

16 -b it

RFCOM M

0 x0 0 0 3

16 -b it

OBEX

0 x0 0 0 8

16 -b it

L2 CA P

0 x0 1 0 0

16 -b it

Seri al P o rt

0 x1 1 0 1

16 -b it

 

 

 

第二个 识表 服务 I D 这是蓝 客户 与服务端 共同定 好的 务唯一识 ,可

16 -b it, 3 2 -b it, 1 2 8 -b it

 

D isc o v ery Ag ent.s earchS er v ices( in t[] at trSet, UUI D [] uu id Set, Re m o te D e v ic e b t D e v , D isc o v ery L isten er li st ener ) ,该 API 动服 搜索 uu id Set 定义寻 的服 , b tD e v 服务端, li s te n er D is co v er yL ist ener 服务搜索 提供的 调操 函数 se arch Servic es( ) 每一个 现的 牙设备上 寻找目 服务 D isc o v ery Ag ent 可以 时在 牙设备 查找 服务 m axSer v ic eSearche s 定义本 设备 多可 发起的 务查 查找 没有结 时, 函数 sear chS erv i ces() 线程进 等待 当服务查 询结 继续 行。

 

 

private   boolean searchServices(Vector   devList)   {

 

UUID[] searchList = new UUID[2];

searchList[0] = new UUID(0x0003);

searchList[1]   = new   UUID( "cccccccccccccccccccccccccccccccc" ,

false );

 

for ( int i = 0; i < devList.size(); i++) {

 

try {

agent .searchServices( null , searchList, (RemoteDevice)

devList.elementAt(i),   this );

}   catch   (BluetoothStateException   e) {}

 

synchronized ( bluelock ) {

serviceSearchCount ++;

if   ( serviceSearchCount   ==   maxServiceSearches )   {

try   {

bluelock .wait();

}   catch   (Exception   e) {}

}

}

}

 

while   ( serviceSearchCount   >   0) {

synchronized ( bluelock ) {

try   {

bluelock .wait();

}   catch   (Exception   e) {

}

}

}

 

return   record   !=   null ;

}

 

 

 

serv ic esD i sco v e red(in t tran sID, Ser v ic eRe c o rd [] se rvRe co rd ) ser v iceSea rch Comp lete d (in t

tran sID, in t respC o d e) D i sco v er yL ist ener 供的 务搜索 调接 在蓝 牙服务 上找 到指定 服务, s erv i ces D i sco v ered( ) 用收入找 到的服 。当 一个蓝牙 服务端 索服 务完毕, s erv i ceSear chCo m p let ed() 被调

 

 

 

 

 

public   void serviceSearchCompleted( int   transID,   int   respCode)   {

serviceSearchCount --; synchronized ( bluelock ) { bluelock .notify();

}

}

 

public   void servicesDiscovered( int   transID,   ServiceRecord[] servRecord) {

record = servRecord[0];

}

 


获取连接 UR L

 

获取服务 S erv ic eRe co rd 之后, Ser v iceR ec o rd .g et Co nn ec tio n U RL (in t, bo o lean ) 取连接

URL

 

 

public   void getBluetoothUrl()   throws   Exception   {

record = findService();

url   =   record .getConnectionURL(ServiceRecord. NOAUTHENTICATE_NOENCRYPT ,

false );

}

 

建立连接

 

获取连接 URL 之后, Strea m C o nn ect i o n 。从连 实例 取输入流 和输出 进行数 据交互。

 

 

public   void connect()   throws   Exception   {

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

dos   =   streamConnection .openDataOutputStream();

dis   =   streamConnection .openDataInputStream();

}


客户 三种牙连

 

S PP 连接方式

 

SPP 接方 通过 Str ea m Co nn ect i o n 连接 UR L 格式 下: b tspp : / / [b lu et o o th add ress ]: [p o rt] b tspp : / / 0 00 a 9 5 0 2 0 c 7 b : 5; n am e =SP P ex

 

public void runSPPConnection (){

try {

StreamConnection connection = null ; DataOutputStream os = null ; DataInputStream is = null ;

try {

//   创建 St re am Co nn ec ti on

connection = (StreamConnection) Connector. open ( _url );

 

// 打开输入 流读取

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

os.flush();

 

//   打开输出 流发送

is   = connection.openDataInputStream();

byte []   buffer = new   byte [   1024 ];

int readBytes = is.read(buffer);

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

}

finally {

os.close(); is.close(); connection.close();

}

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

}

 

L2 C APC onne c t i o n 连接方式

 

L2 CA P 连接 URL 式如

 

bt l 2cap:/ / [ B l u e t oo t h Ad d r e ss ] :[po r t];n am e =[n am e t e xt]

 

bt l 2cap:/ / 00 0a 95 02 0c7b: 5;n am e =sy n c l 2cap

 

 

 

public   void runL2CAPConnection (){

try {

L2CAPConnection   connection = null ;

try {

// L2 CAP 连接

connection = (L2CAPConnection) Connector. open ( _url );

 

// 想服 端发送

String message = "/n[CLIENT] JSR -82 CLIENT says hello!" ;

connection.send(message.getBytes());

 

// 读取 务端数据

byte []   buffer = new   byte [   1024 ];

int   readBytes   = connection.receive(buffer);

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

}

finally {

connection.close();

}

}

catch (IOException   ioe){

BluetoothJSR82Demo. errorDialog (ioe.toString());

}

}


OBE X 连接方式

 

OBEX UR L

 

"tcpob e x:// li t e spee d: 65 12 "

 

 

"btg oe p:// 00 0a 95 02 0c7 b: 99 6"

 

"i r d ao b e x://d i sco v e r .08 ; i as=fax "

 

 

 

public   void runOBEX Connection()

{

try {

Connection connection = null ; OutputStream outputStream = null ; Operation putOperation = null ; ClientSession cs = null ;

try {

// 打开连 获取 ClientSession

connection = Connector. open ( _url ); cs = (ClientSession) connection; cs.connect( null );

 

// 设置 He ad er Se t 并填充

byte   filebytes[]   = "[CLIENT]   Hello.." .getBytes();

HeaderSet hs = cs.createHeaderSet(); hs.setHeader(HeaderSet. NAME , "test.txt" ); hs.setHeader(Header Set. TYPE , "text/plain" ); hs.setHeader(HeaderSet. LENGTH , new Long(filebytes. length ));

 

// Cl ie nt S es si on 中获取 Operation putOperation = cs.put(hs);

 

// Op er at i on 获取输 流, 送数据

outputStream = putOperation.openOutputStream();

outputStream.write(filebytes);

}

finally {

outputStream.close(); putOperation.close(); cs.disconnect( null ); connection.close( );

}

}

catch (Exception   e)

{

BluetoothJSR82Demo. errorDialog (e.toString());

}

}

 

 

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 蓝牙编程(二))