package SST.com.tw;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.Vector;
import javax.microedition.io.*;
public class BTCV extends MIDlet implements DiscoveryListener, CommandListener, Runnable{
// Base ------------------------------------------
Display dsp;
Form frm;
ChoiceGroup cg;
Form saveFrm;
// Command -----------------------------------------
Command OK;
Command Exit;
Command Send;
// BlueTooth ---------------------------------------
LocalDevice local = null; // Local Device Info
DiscoveryAgent agent = null; // Device Scanner
Vector devicesFound = null; // Remote device Info
StreamConnection conn;
DataInputStream dis = null;
DataOutputStream dos = null;
String url;
Thread readThread = null;
public BTCV () {
dsp = Display.getDisplay(this);
frm = new Form("BT SP");
try {
local = LocalDevice.getLocalDevice();
} catch (BluetoothStateException ex) {
}
cg = new ChoiceGroup("Select Bluetooth Device,"+local.getFriendlyName().toString(), ChoiceGroup.EXCLUSIVE);
OK = new Command("OK", Command.OK, 1);
Exit = new Command("Exit", Command.EXIT, 1);
Send = new Command("Send", Command.OK, 1);
frm.append(cg);
frm.addCommand(OK);
frm.addCommand(Exit);
frm.setCommandListener(this);
readThread = new Thread(this);
}
public void startApp() {
dsp.setCurrent(frm);
scanOverFlag = false;
runFlag = false;
doDeviceDiscovery();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
this.notifyDestroyed();
}
boolean scanOverFlag;
int cc;
public void commandAction(Command cmnd, Displayable dsplbl) {
if (cmnd == OK) {
do {
if (!scanOverFlag) // waiting scan
{
break;
} //cg.append("OK ", null);
try {
RemoteDevice rd = (RemoteDevice) devicesFound.elementAt(cg.getSelectedIndex());
agent.searchServices(null, new UUID[]{SERIAL}, rd, this);
} catch (BluetoothStateException ex) {
//ex.printStackTrace();
cg.append("searchService Error", null);
}
if (url == null)
{
break;
}
try {
conn = (StreamConnection) Connector.open(url);
dos = conn.openDataOutputStream();
dis = conn.openDataInputStream();
runFlag = true;
frm.removeCommand(OK);
frm.addCommand(Send);
readThread.start();
readThreadFlag = false;
} catch (IOException ex) {
frm.append("connect error");
}
} while (false);
}else if (cmnd == Exit) {
try {
conn.close();
readThreadFlag = true;
} catch (IOException ex) {
//ex.printStackTrace();
}
this.destroyApp(false);
}else if (cmnd == Send) {
try {
if (dos != null) {
dos.writeChars(Integer.toString(cc));
cc++;
//dos.writeByte(0x30);
//dos.close();
}
}catch (IOException ex) {
frm.append("write error");
}
}
}
private void doDeviceDiscovery() {
devicesFound = new Vector();
try {
agent = local.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, this); // start scan
} catch (BluetoothStateException ex) {
//ex.printStackTrace();
}
}
private UUID SERIAL = new UUID(0x1101);
private UUID RFCOMM = new UUID(0x0003);
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
try {
devicesFound.addElement(rd);
cg.append(rd.getFriendlyName(true), null);
} catch (IOException ex) {
//ex.printStackTrace();
}
}
public void servicesDiscovered(int transID, ServiceRecord[] srs) {
url = srs[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
}
public void serviceSearchCompleted(int transID, int respCode) {
switch (respCode)
{
case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
frm.append("service complete");
break;
case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
cg.append("no record", null);
break;
case DiscoveryListener.SERVICE_SEARCH_ERROR:
break;
}
}
public void inquiryCompleted(int param) {
switch (param)
{
case DiscoveryListener.INQUIRY_COMPLETED:
scanOverFlag = true; // device discovery over
frm.append("device complete");
break;
case DiscoveryListener.INQUIRY_ERROR:
break;
case DiscoveryListener.INQUIRY_TERMINATED:
break;
}
}
private boolean runFlag;
private boolean readThreadFlag;
public void run() {
while (!runFlag);
while (!readThreadFlag) {
byte buf = 0;
try {
buf = dis.readByte();
//String.valueOf(buf)
frm.append(new String(new byte[] {buf})); // ascii
} catch (IOException ex) {
//ex.printStackTrace();
frm.append("get error");
}
}
}
}
實作這個程式
它的基本流程就是
先找尋附近的設備 -> 選定設備 & 找尋服務 -> 最後才能建立連線
接著 利用 JAVA 所實作的 call back 下去做處理
利用 button 去控制發 byte
再開一個 thread 去接收資料
接進來的 byte JAVA 會自動幫你變成 ASCII
所以要轉回 byte
Sony Ericsson 可用
Moto V系列 卡在發資料
Nokia 5800 找不到裝置
請先 登入 以發表留言。