Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > General Computer Support > pls help me !!!!!!!!

Reply
Thread Tools

pls help me !!!!!!!!

 
 
Dharanya Dharanya is offline
Junior Member
Join Date: Dec 2011
Posts: 2
 
      12-26-2011
package BTClient;
import java.util.*;
import javax.bluetooth.*;
import javax.bluetooth.UUID;
import javax.microedition.lcdui.*;
import javax.microedition.io.StreamConnection.*;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.ServiceRecord;
import javax.microedition.midlet.*;
public class BTClient implements DiscoveryListener, Runnable {
// DiscoveryListener public static final String uuidString = "0123456789ABCDEF0123456789ABCDEF";
public static UUID uuid;
private LocalDevice localDevice;
String localBTAddress;
String localBTName;
DiscoveryAgent discoveryAgent;
Thread th;
Thread readWorkTh;
Thread writeWorkTh;
StreamConnection conn;
boolean exitFlag;
boolean BTReady;
DataInputStream in;
DataOutputStream out;
String sendText = "";
Hashtable remoteDevices = new Hashtable();
java.util.Hashtable String url;
ServiceRecord serviceRecord;
public BTClient() {

th = new Thread(this); th.start();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// @SuppressWarnings("empty-statement")
// @SuppressWarnings("empty-statement")
public void run(boolean exitFlag) {
if (!initBT()) {
return;
}
try {

synchronized (this) {

try {
wait();//会释放这个锁,让其它线程有机会运行。 }
}
catch (InterruptedException e) { e.printStackTrace();}
}

if (exitFlag) return;//返回
search(); ///////////////////////////// ///////////////////////////// // 等待URL准备好 synchronized (this) { //等到被唤醒后 再继续运行
try {
wait();//会释放这个锁,让其它线程有机会运行。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
catch (InterruptedException e) { e.printStackTrace();
////

if (exitFlag) return;//返回
conn = (StreamConnection) Connector.open(url);
in = conn.openDataInputStream();
out = conn.openDataOutputStream();
readWorkTh = new ReadWorkThread();
readWorkTh.start();
writeWorkTh = new WriteWorkThread();
writeWorkTh.start(); BTReady = true;
} catch (IOException e) {
return;//返回
} catch (SecurityException e) {
return;//返回
}
th = null;
} ////////////////////////////////////////////////////////////////////////////////////////////////////
public boolean initBT() { //初始化蓝牙-文文风 这很简单 初始化就可以了主要是地址!
boolean success = false;
try {
uuid = new UUID(uuidString, false);// 我们的UUID // 取得本地设备实例
localDevice = LocalDevice.getLocalDevice(); // 记录蓝牙地址
localBTAddress = localDevice.getBluetoothAddress(); // 记录蓝牙名称
localBTName = localDevice.getFriendlyName();
localDevice.setDiscoverable(DiscoveryAgent.GIAC); // 取得蓝牙代理
discoveryAgent = localDevice.getDiscoveryAgent();
success = true;
} catch (Exception e) {
System.err.println("初始化蓝牙设备失败:" + e);
}
return success;
} ////////////////////////////////////////////////////////////////////////////////////////////////////
public void search() {// 搜索设备,搜索服务
try {
//remoteDevices哈什表
// 清除remoteDevices
RemoteDevice remoteDevice;
remoteDevices.clear(); //cacheDevices++preDevices==remoteDevices RemoteDevice[] cacheDevices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACH ED);
// class RemoteDevice//缓存的
if (cacheDevices != null)
{ for (int i = 0; i < cacheDevices.length; i++) {
remoteDevices.put(cacheDevices[i].getBluetoothAddress(), cacheDevices[i]); //哈什表
}
}
RemoteDevice[] preDevices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREK NOWN); // class RemoteDevice//已知的
if (preDevices != null)
{ for (int i = 0; i < preDevices.length; i++) {
remoteDevices.put(cacheDevices[i].getBluetoothAddress(),cacheDevices[i]); //哈什表 这里好像写错了 应该是preDevices
}
} // 在缓存的和已知的设备上查询“服务”,函数定义在下面
searchServices(remoteDevices);
if (serviceRecord != null)// 找到返回
return; // 开始搜索设备
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);//这里的this是指本类.当然我们要在本类实现DiscoveryL istener接口. // 在搜索到的设备上查询服务
searchServices(remoteDevices);//去下面看看这个函数的实现
remoteDevices.clear();
} catch (BluetoothStateException e) {
e.printStackTrace();
}
if (serviceRecord != null)
{ notify();
}
else { }
}
private void searchServices(Hashtable remotes) throws BluetoothStateException {
// UUID[] UUIDs = new UUID[1]; UUIDs[1] = new UUID(uuidString, false); UUID UUIDs[0] = new UUID(0x0003);
for (Enumeration e = remotes.keys(); e.hasMoreElements()
{
String key = (String) e.nextElement();
RemoteDevice remoteDevice = (RemoteDevice) remotes.get(key);

discoveryAgent.searchServices(null, UUIDs, remoteDevice, this);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
public void deviceDiscovered(RemoteDevice device, DeviceClass cod)
{ deviceDiscoverd();
remoteDevices.put(device.getBluetoothAddress(), device);
}
//////////////////////////////////
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{ // 发现感兴趣的服务,这里直接使用第一个
if (servRecord == null || servRecord.length == 0)
{ url = null; serviceRecord = null; return;
}
url = serviceRecord.getConnectionURL(ServiceRecord.NOAUT HENTICATE_NOENCRYPT, false);
} ////////////////////////////////////////////////////////////////////////////////////////////////////
public void inquiryCompleted(int discType)
{ //设备查询完成
try
{
searchServices(remoteDevices); //开始查询服务了,
}
catch (BluetoothStateException e)
{
e.printStackTrace();
}
remoteDevices.clear();
}


public void serviceSearchCompleted(int transID, int respCode)
{//服务查询完成
synchronized (this)
{ //同步
notifyAll(); //服务查询完成后 会叫醒所有线程。
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////
public void close()
{
Object writeWorkTh;
Object in;
Object out;
Object conn;
Object readWorkTh;
boolean exitFlag;
try
{ exitFlag = true; synchronized (this)
{ notify(); //这也有唤醒 但是不知道具体做什么用的?请教一下
}
if (writeWorkTh != null)
{
synchronized (writeWorkTh)
{ writeWorkTh.notify(); //这也有唤醒,请教!
}
}
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
if (conn != null) conn.close();
if (readWorkTh != null)
{
readWorkTh.join();
}
if (writeWorkTh != null) writeWorkTh.join();
if (th != null) th.join();
} catch (IOException e)
{ e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
} ////////////////////////////////////////////////////////////////////////////////////////////////////
public void send(String str)
{ ////发送信息 外部对象会调用此方法
if (writeWorkTh == null) return;
sendText = str;
synchronized (writeWorkTh)
{ writeWorkTh.notify(); //指定线程唤醒 别睡了 干活了
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////
class ReadWorkThread extends Thread
{
public void run() {
try
{
while (!exitFlag)
{
String str = in.readUTF();
if (str != null)
{
}
}
} catch (IOException e)
{
}
}
} //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
class WriteWorkThread extends Thread
{ // @Override
public void run(){
try {
while (!exitFlag)
{ synchronized (this)
{
try
{ wait();//这等待一下
} catch (InterruptedException e)
{ e.printStackTrace();
}
if (exitFlag)// 可能因为关闭操作被打断
break;
if (sendText != null) out.writeUTF(sendText);
}////
}
} catch (IOException e)
{
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
public void startSearch() {

}
}

I am getting 2 error
C:\WTK25\apps\3f59c924faf\src\BTClient.java:28: ';' expected
java.util.Hashtable String url;
^
C:\WTK25\apps\3f59c924faf\src\BTClient.java:28: <identifier> expected
java.util.Hashtable String url;
i am not able to debug.....
 
Reply With Quote
 
 
 
 
Dharanya Dharanya is offline
Junior Member
Join Date: Dec 2011
Posts: 2
 
      12-28-2011
oh sad..what u did to rectify??? pls help me...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT - Pls help...pls recommend - laptop purchase irfansmith@gmail.com Computer Information 2 08-15-2008 11:34 PM
pls help me im desperate and in neeeeed of your help!!! weepeez@gmail.com Java 1 01-02-2008 04:50 PM
URGENT: Need help in setting environment variable.. pls help Rahul S. C Programming 3 11-01-2004 10:53 AM
Help newbie with Netbeans help system/javadocs pls. James Bond Java 0 08-03-2004 06:01 AM
pls, help.. i need a number..pls olabanji timothy MCSE 7 09-10-2003 04:02 PM



Advertisments