| mross462@gmail.com 2007-11-27, 10:33 pm |
| 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 BluetoothStateExcept
ion{
try{
local = LocalDevice.getLocalDevice();
agent = local. getDiscoveryAgent();
device = agent. retrieveDevices(Disc
overyAgent.PREKNOWN);
}
catch(BluetoothState
Exception e){
}
try{
int n = 0;
remote = device[n];
while (remote. getFriendlyName(true
) != "Bluekeys"){
remote = device[n];
n++;
}
}
catch (IOException e){}
catch (NullPointerExceptio
n e){}
catch (ArrayIndexOutOfBoun
dsException 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
StreamConnectionNoti
fier notifier =
(StreamConnectionNot
ifier) 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(Rem
oteDevice btDevice, DeviceClass
cod) {
if (!devices.contains(btDevice)) {
devices. addElement(btDevice)
;
}
}
public void inquiryCompleted(int
discType) {
inquiryResult = discType;
synchronized (this) {
notify();
}
}
public void servicesDiscovered(i
nt transID, ServiceRecord[]
servRecord) {
services. addElement(servRecor
d[0]);
}
public void serviceSearchComplet
ed(int transID, int respCode) {
synchronized (this) {
notify();
}
}
}
|