Monday, July 13, 2009

J2ME Bluetooth connection steps


Following are the connection setup steps for J2ME Bluetooth.
(example shows Connecting to Server mode connection already
created with btspp)
  1. Inititate Search for devices.
  2. Method "deviceDiscovered" called by flatform for each device.
  3. Method "inquiryCompleted" called by flatform when device search completes.
  4. Then do a service search for selected device.
  5. Method "servicesDiscovered" called by flatform for each service.
  6. Method "serviceSearchCompleted" called by platform when service search completes.
  7. (there open a StreamConnection to chosen service URL)



//step 1
public void SearchAvailDevices() {
try {
//First get the local device and obtain the discovery agent.
m_LclDevice = LocalDevice.getLocalDevice();

m_DscrAgent = m_LclDevice.getDiscoveryAgent();

m_DscrAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException ex) {
vobt.addLog("Problem in searching the blue tooth devices : " + ex.getMessage());
System.out.println("Problem in searching the blue tooth devices");
ex.printStackTrace();
}

}
//step 2 -- PLATFORM CALL
//Called when device is found during inquiry
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {

try {
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));
vobt.addLog("**Device Discovered**");
vobt.addLog("#Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
vobt.addLog("#Bluetooth Address: " + btDevice.getBluetoothAddress());

//put remote devices in Array
remoteDevicesArray[deviceCount] = btDevice;
deviceClassesArray [deviceCount] = cod;
deviceCount++;
} catch (Exception e) {
System.out.println("Device Discovered Error: " + e);
vobt.addLog("deviceDiscovered : " + e);
}
//step 3--PLATFORM CALL
//when device search completes
public void inquiryCompleted(int discType) { }
//step 4
//Search for srvices in Given device as following
public void serviceSearch(int selectedIndex)
{
try {
UUID[] uuidSet = new UUID[1];
uuidSet[0] = RFCOMM_UUID; // UUID USED TO INITIATE SERVER
int searchID = m_DscrAgent.searchServices(null, uuidSet, remoteDevicesArray[selectedIndex], this);
} catch (BluetoothStateException ex) {
ex.printStackTrace();
vobt.addLog("deviceDiscovered : " + ex);
}
}
// step 5 -- PLATFORM CALL
//when service found during service search (for each time new service found)
public void servicesDiscovered(int transID, ServiceRecord[] records) {

for (int i = 0; i < records.length; i++) {
m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);

System.out.println(m_strUrl);
if (m_strUrl.startsWith("btspp")) //search for URLS with btspp
{
m_bServerFound = true;
m_bInitClient = true;
break;
}

}
}


// step6 -- PLATFORM CALL
// when service search complete for given device
public void serviceSearchCompleted(int transID, int respCode) {
try { // open stream connection with needed URL
StrmConn = (StreamConnection) Connector.open(m_strUrl);
}
catch()
{}
}




Share Article : J2ME Bluetooth connection steps
Share/Save/Bookmark

3 comments:

BlueToothunt said...

Hey
Crystal Clear ,Thanks

Cegonsoft said...

I have tried your given steps , But i could not get output..Can you show one demo through video?

Srinath Gamage said...

These code is from a working application I developed. Anyway I'm sorry, I don't have much spare time to do a video.

Post a Comment