Phone seems to be connected but acceptAndOpen blocks

13 views
Skip to first unread message

Saxer

unread,
Sep 2, 2008, 5:19:50 AM9/2/08
to bluecove-users
Hello,

I've developed an application where a phone connectes to a bluecove-
equipped PC. I used a bit of code from the BluecoveExamples. I'm
running Bluecove on WinXP SP2 with Widcomm.

The problem (coming from time to time):
- The phone discovers the host-pc
- The phone discovers the right service
- The BT-Icon in the Taskbar turns "green"
- My StreamConnectionNotifier.acceptAndOpen() keeps blocking

I was able to fix the problem (not always but very often) by starting
the BluecoveTest-Application and run some tests between phone and pc.

I don't know if I'm simply missing some initing or service
registration. But as I said, sometimes it works like a charme.

Here's the relevant code from the host pc (currently all the record
specific methods come from the bluecove-examples, I hope my formatting
doesn't break the message-view):

private void buildServiceRecordPub(ServiceRecord record)
throws ServiceRegistrationException {
final short UUID_PUBLICBROWSE_GROUP = 0x1002;
final short ATTR_BROWSE_GRP_LIST = 0x0005;
// Add the service to the 'Public Browse Group'
DataElement browseClassIDList = new DataElement(
DataElement.DATSEQ);
UUID browseClassUUID = new UUID(UUID_PUBLICBROWSE_GROUP);
browseClassIDList.addElement(new DataElement(DataElement.UUID,
browseClassUUID));
setAttributeValue(record, ATTR_BROWSE_GRP_LIST,
browseClassIDList);
}

private void setAttributeValue(ServiceRecord record, int attrID,
DataElement attrValue) {
try {
if (!record.setAttributeValue(attrID, attrValue)) {
}
} catch (Exception e) {
}
}

private void buildServiceRecord(ServiceRecord record)
throws ServiceRegistrationException {
String id = "";
try {
if (Configuration.useServiceClassExtUUID.booleanValue()) {
DataElement serviceClassIDList = record
.getAttributeValue(BluetoothTypesInfo.ServiceClassIDList);
if (serviceClassIDList == null) {
serviceClassIDList = new DataElement(
DataElement.DATSEQ);
}
serviceClassIDList.addElement(new DataElement(
DataElement.UUID, Consts.uuidSrvClassExt));
setAttributeValue(record,
BluetoothTypesInfo.ServiceClassIDList,
serviceClassIDList);
}

if (Configuration.testAllServiceAttributes.booleanValue()) {
id = "all";
ServiceRecordTester.addAllTestServiceAttributes(record);
return;
}

id = "pub";
buildServiceRecordPub(record);
id = "int";
record.setAttributeValue(
Consts.TEST_SERVICE_ATTRIBUTE_INT_ID,
new DataElement(
Consts.TEST_SERVICE_ATTRIBUTE_INT_TYPE,
Consts.TEST_SERVICE_ATTRIBUTE_INT_VALUE));
id = "long";
record.setAttributeValue(
Consts.TEST_SERVICE_ATTRIBUTE_LONG_ID,
new DataElement(
Consts.TEST_SERVICE_ATTRIBUTE_LONG_TYPE,
Consts.TEST_SERVICE_ATTRIBUTE_LONG_VALUE));
if (!Configuration.testIgnoreNotWorkingServiceAttributes
.booleanValue()) {
id = "str";
record
.setAttributeValue(
Consts.TEST_SERVICE_ATTRIBUTE_STR_ID,
new DataElement(
DataElement.STRING,
Consts.TEST_SERVICE_ATTRIBUTE_STR_VALUE));
}
id = "url";
record.setAttributeValue(
Consts.TEST_SERVICE_ATTRIBUTE_URL_ID,
new DataElement(DataElement.URL,
Consts.TEST_SERVICE_ATTRIBUTE_URL_VALUE));

id = "bytes";
record.setAttributeValue(
Consts.TEST_SERVICE_ATTRIBUTE_BYTES_ID,
new DataElement(
Consts.TEST_SERVICE_ATTRIBUTE_BYTES_TYPE,
Consts.TEST_SERVICE_ATTRIBUTE_BYTES_VALUE));

id = "variable";
// updateVariableServiceRecord(record);

id = "info";
record.setAttributeValue(
Consts.SERVICE_ATTRIBUTE_BYTES_SERVER_INFO,
new DataElement(DataElement.URL,
ServiceRecordTester.getBTSystemInfo()));

id = "update";
// LocalDevice.getLocalDevice().updateRecord(record);

} catch (Throwable e) {
Logger.error("ServiceRecord " + id, e);
}
}

public void run() {

try {
UUID uuid = new UUID("B10C0BE1111111111111111111110001",
false);

String connectionString = "btspp://localhost:" + uuid
+ ";name=Sensorama SPP Server"
+ Configuration.serverURLParams();
System.out.println(connectionString);

streamConnNotifier = (StreamConnectionNotifier) Connector
.open(connectionString);

LocalDevice localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);

ServiceRecord record = localDevice
.getRecord(streamConnNotifier);
buildServiceRecord(record);
localDevice.updateRecord(record);

System.out.println("\nServer Started. Waiting for clients to
connect...");

StreamConnection connection = streamConnNotifier
.acceptAndOpen();

System.out.println("Client connected via BT");

DataInputStream dit = new DataInputStream(connection
.openInputStream());
DataOutputStream dout = new DataOutputStream(connection
.openOutputStream());

while (active) {

long id = dit.readLong();
int token = dit.readInt();

if (token == ProtocolStatics.MobileOriginated.FirstLevel.DATA) {
manageRead(dit, id);
} else {
// TODO: Error!!!
}
}
} catch (IOException io) {
io.printStackTrace();
}

}

Here comes the client:

//check for spp service
RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index);
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID("B10C0BE1111111111111111111110001",false);
agent.searchServices(null,uuidSet,remoteDevice,this);
try {
synchronized(lock){
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(connectionURL==null){
System.out.println("Device does not support Simple SPP Service.");
System.exit(0);
}
//connect to the server and send a line of text
streamConnection=(StreamConnection)Connector.open(connectionURL,
Connector.READ_WRITE, true);
Reply all
Reply to author
Forward
0 new messages