📄 bluepadclient.java
字号:
//------------------------------------------------------------------
// ____ _____ _
// / ___| ___ _ __ _ _ | ____|_ __(_) ___ ___ ___ ___ _ __
// \___ \ / _ \| '_ \| | | | | _| | '__| |/ __/ __/ __|/ _ \| '_ \
// ___) | (_) | | | | |_| | | |___| | | | (__\__ \__ \ (_) | | | |
// |____/ \___/|_| |_|\__, | |_____|_| |_|\___|___/___/\___/|_| |_|
// |___/
//------------------------------------------------------------------
// Copyright (c) 2003 Sony Ericsson Mobile Communications AB
// Research Trinagle Park, NC 27709
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
//------------------------------------------------------------------
package example.bluetooth;
import javax.bluetooth.*;
import java.io.IOException;
import javax.microedition.io.*;
/**
* The BluePadClient starts a BlueTooth client connection and
* attempts to connect to the server on the specified remote device.
* Handles the client-specific funcionality.
*
* @author Paul H. Nichols
* @version 1.0
*/
class BluePadClient extends Thread {
private BluePad bluePad;
private L2CAPConnection conn = null;
private ServiceRecord record;
private String conURL;
public BluePadClient(BluePad pad, ServiceRecord record) {
this.bluePad = pad;
this.record = record;
}
public BluePadClient(BluePad pad, String URL) {
this.bluePad = pad;
this.conURL = URL;
pad.printString("URL: " + conURL);
}
public void run() {
try {
if (conURL == null) {
if (record == null) {
bluePad.printString("No record!");
return;
}
int security = ServiceRecord.AUTHENTICATE_NOENCRYPT; // NOAUTHENTICATE_NOENCRYPT, AUTHENTICATE_ENCRYPT, AUTHENTICATE_NOENCRYPT
boolean master = false;
conURL = record.getConnectionURL(security, master);
}
int index= conURL.indexOf(':');
String protocol= conURL.substring(0, index);
// Must be L2CAPP
if (!protocol.equals("btl2cap")) {
bluePad.printString("Wrong protocol: " + protocol);
return;
}
conn = (L2CAPConnection)Connector.open(conURL);
int length = conn.getReceiveMTU();
bluePad.setStateConnected(conn);
byte data[] = new byte[length];
length = conn.receive(data);
while (length != -1) {
bluePad.receiveData(data, 0, length);
try {
length = conn.receive(data);
}
catch (IOException e) {
break;
}
}
}
catch (Exception e) {
bluePad.printString("Exception (c1): " + e.getMessage());
}
finally {
close();
}
} // run
/**
* Close down the client and notify the bluePad that the
* state is now disconnected.
*/
public void close() {
if (conn == null) {
return;
}
try {
conn.close();
}
catch (IOException e) {
}
conn = null;
bluePad.setStateDisconnected();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -