I'm trying to create a simple client-server application but I'm having some problems.
I did the server's code, as follows, and after running it I try to connect to it by the BluetoothExplorer, which is in the demo/gui folder of the stack. The device was found, but when the BTExplorer tries to find the available services at that device, an exception happens: javax.bluetooth.BluetoothStateException: org.javabluetooth.stack.hci.HCIException: Create Connection failed. (17)
Does someone knows why is it happening?
private void StackInit(String portName, String connName, int RMTU, int TMTU, String Id) throws IOException, HCIException, BluetoothStateException, PortInUseException {
StringBuffer str;
// Starts Bluetooth Stack by serial port
HCIDriver.init(new UARTTransport(portName));
BluetoothStack.init(new BluetoothStackLocal());
bluetoothStack = BluetoothStack.getBluetoothStack();
bluetoothStack.send_HCI_HC_Change_Local_Name("BT SERVER");
// Enable Inquiry, Page and also AutoInquiry
bluetoothStack.send_HCI_HC_Write_Scan_Enable((byte)0x03);
bluetoothStack.send_HCI_HC_Write_Event_Filter_Connection_Setup((byte)0x02);
bluetoothStack.send_HCI_HC_Write_Event_Filter_Inquiry_Result();
The problem was solved!!!
Probably the Bluetooth Tool Kit I'm using (Teleca - http://www.comtec.sigma.se\) does not support Master/Slave switching. So, in the getL2CAPLink method of the HCIDriver class, when it calls send_HCI_LC_Create_Connection(...), I've changed the Allow_Role_Switch parameter (the last one) to 0x00 which means, according to BT Specification: "The local device will be a master, and will not accept a role switch
requested by the remote device at the connection setup."
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm trying to create a simple client-server application but I'm having some problems.
I did the server's code, as follows, and after running it I try to connect to it by the BluetoothExplorer, which is in the demo/gui folder of the stack. The device was found, but when the BTExplorer tries to find the available services at that device, an exception happens: javax.bluetooth.BluetoothStateException: org.javabluetooth.stack.hci.HCIException: Create Connection failed. (17)
Does someone knows why is it happening?
This is the code:
import ...
public class BluetoothServer {
private LocalDevice localDevice = null;
private RemoteDevice remoteDevice = null;
private BluetoothStack bluetoothStack = null;
private DiscoveryAgent discoveryAgent = null;
private L2CAPConnectionNotifier notifier = null;
private boolean Active = true;
public BluetoothServer(String Port, String Name, int RMTU, int TMTU, String Id) {
try {
StackInit(Port, Name, RMTU, TMTU, Id);
}
catch (IOException e) {
e.printStackTrace();
}
catch (HCIException e) {
e.printStackTrace();
}
catch (PortInUseException e) {
e.printStackTrace();
}
}
private void StackInit(String portName, String connName, int RMTU, int TMTU, String Id) throws IOException, HCIException, BluetoothStateException, PortInUseException {
StringBuffer str;
// Starts Bluetooth Stack by serial port
HCIDriver.init(new UARTTransport(portName));
BluetoothStack.init(new BluetoothStackLocal());
bluetoothStack = BluetoothStack.getBluetoothStack();
bluetoothStack.send_HCI_HC_Change_Local_Name("BT SERVER");
// Enable Inquiry, Page and also AutoInquiry
bluetoothStack.send_HCI_HC_Write_Scan_Enable((byte)0x03);
bluetoothStack.send_HCI_HC_Write_Event_Filter_Connection_Setup((byte)0x02);
bluetoothStack.send_HCI_HC_Write_Event_Filter_Inquiry_Result();
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
// Service's string
str = new StringBuffer();
str.append("btl2cap://localhost:");
str.append(Id);
str.append(";ReceiveMTU=");
str.append(RMTU);
str.append(";TransmitMTU=");
str.append(TMTU);
str.append(";authenticate=false;encrypt=false;name=");
str.append(connName);
// Register the service in the SDDB
notifier = (L2CAPConnectionNotifier)Connector.open(str.toString());
System.out.println("Local Name: " + localDevice.getFriendlyName());
System.out.println("Waiting for clients...");
// Wait for clients
L2CAPConnection conn = notifier.acceptAndOpen();
System.out.println("Woohoo!!!");
// Close
notifier.close();
}
public static void main(String[] args) {
BluetoothServer srv = new BluetoothServer("COM1", "Server Service", 255, 255, "1122");
}
}
Thanks.
The problem was solved!!!
Probably the Bluetooth Tool Kit I'm using (Teleca - http://www.comtec.sigma.se\) does not support Master/Slave switching. So, in the getL2CAPLink method of the HCIDriver class, when it calls send_HCI_LC_Create_Connection(...), I've changed the Allow_Role_Switch parameter (the last one) to 0x00 which means, according to BT Specification: "The local device will be a master, and will not accept a role switch
requested by the remote device at the connection setup."