Bluetooth Listener and Sending Problem
Hello All,
I'm currently having Trouble Sending and or Receiving a character over
a BT connection from a Blackberry Pearl to a Parani ESD200.
I've included my code below.
Thanks,
M. Ross
/*
* BluetoothListener.java
*
* Created on November 27, 2007, 12:43 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package hello;
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.lang.*;
import java.util.Vector;
import java.io.*;
/**
*
* @author Mike Ross
*/
public class BluetoothListener implements DiscoveryListener{
public LocalDevice local;
public RemoteDevice remote;
public DiscoveryAgent agent;
public boolean inquiryStarted;
public String remoteDeviceName;
public DiscoveryListener listener;
public String url;
public char readCharacter;
public DataInputStream in;
public DataOutputStream out;
public String deviceName;
public int accecpting;
public String passcode = "9153094887#";
public char [] passcodeArray = passcode.toCharArray();
public Vector devices = new Vector();
public Vector services = new Vector(); // ServiceRecord
public RemoteDevice[] device;
int inquiryResult;
int inquiryTrials;
UUID[] uuidSet;
UUID serviceUUID = new UUID(0x003);
/** Creates a new instance of BluetoothListener */
public BluetoothListener() {
}
public void beginBluekeys() throws BluetoothStateException{
try{
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
device = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
}
catch(BluetoothStateException e){
}
try{
int n = 0;
remote = device[n];
while (remote.getFriendlyName(true) != "Bluekeys"){
remote = device[n];
n++;
}
}
catch (IOException e){}
catch (NullPointerException e){}
catch (ArrayIndexOutOfBoundsException e){}
try{
// Search Services
agent.searchServices(null,new UUID[] {new
UUID(0x003)},remote,this);
// Get bluetooth address and set URL
String BID = remote.getBluetoothAddress();
String serviceURL = "bttsp://" + BID + ":1";
//Set Stream Connection Nofitifer and Service Record
StreamConnectionNotifier notifier =
(StreamConnectionNotifier) Connector.open(serviceURL);
ServiceRecord serviceRecord =
local.getRecord(notifier);
StreamConnection connect =(StreamConnection)
Connector.open(serviceURL, Connector.READ_WRITE, true);
//Open Connection and In and Out Data Streams
in = connect.openDataInputStream();
out = connect.openDataOutputStream();
// Wait For Hardware Handshaking
while(in.read() != 'A'){
out.write('s');
}
// Hardware Has Shaken Hands
int i = 0;
// Send Passcode to device and wait for "C"
while(in.read() != 'C'){
//Check for last accepctance
if (in.read() == 'A'){
// Write Current Passcode Character
out.write(passcodeArray[i]);
i++;
}
// If A Not Found, decrement to begining of
Passcode array
else{
while (i >= 0){
i--;
}
}
}
// If complete then start At Begining of array for
next exectue.
while (in.read() == 'C' & i >=0) i--;
}
catch(IOException e){
System.out.println("IO Exception Caught");
return;
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass
cod) {
if (!devices.contains(btDevice)) {
devices.addElement(btDevice);
}
}
public void inquiryCompleted(int discType) {
inquiryResult = discType;
synchronized (this) {
notify();
}
}
public void servicesDiscovered(int transID, ServiceRecord[]
servRecord) {
services.addElement(servRecord[0]);
}
public void serviceSearchCompleted(int transID, int respCode) {
synchronized (this) {
notify();
}
}
}
|