I'm writing an application to communicate between my smartphone and a computer using a bluetooth device.
I'm using Bluecove to manage the bluetooth on the computer, and the android API for my android device.
However, when I'm debugging, nothing happens. I think that the problem is that the UUID is wrong. I'm not sure how to get the devices to identify each other, in order to establish a connection.
I have read some other "questions" about those tags, but what I've tried didn't fix my problem:
Here's what I've written so far:
For tho android (Server) (This is the function that will make the connection)
public void connectSocket(){ blueAdapter.cancelDiscovery(); // Cancel discovery because it'll slow down the connection
final BluetoothServerSocket serverSocket; BluetoothServerSocket sSocket= null; try{ sSocket = blueAdapter.listenUsingRfcommWithServiceRecord("BluetoothJANE", MY_UUID); }catch(IOException e){} serverSocket = sSocket; Thread acceptThread = new Thread(new Runnable() { @Override public void run() { BluetoothSocket socket = null; while(true){ try{ socket = serverSocket.accept(); }catch(IOException e){ break; } if(socket != null){ try{ iStream = socket.getInputStream(); oStream = socket.getOutputStream(); } catch(IOException e){} } } } }); acceptThread.start();
}
For java app on PC (This is the constructor of the class (it's on an independent thread) that will manage the bluetooth connection)
public ModuleBluetooth() {
StreamConnectionNotifier notifier = null; StreamConnection connection = null; try { blueDevice = LocalDevice.getLocalDevice(); blueDevice.setDiscoverable(DiscoveryAgent.GIAC); String url = "btspp://localhost:" + MY_UUID.toString() + ";name=RemoteBluetooth"; notifier = (StreamConnectionNotifier) Connector.open(url); } catch (BluetoothStateException e) { System.out .println("ModuleBluetooth: Error getting the bluetooth device"); } catch (IOException e) { } System.out.println("waiting for connection..."); try { connection = notifier.acceptAndOpen(); System.out.println("Conenction created"); } catch (IOException e) { System.out.println("Can not create the connection"); }
}
Could somebody please help me? Any thoughts would be very much appreciated.
I have also tried to use some functions to acquire the UUID (in android) such as, [fetchUuidsWithSdp][2] (and the related functions) but eclipse doesn't recognize that functions (It seems that they don't exist in "my" API).