I have been challenged by a professor to develop a little Bluetooth Demo app on Android. I knew nothing about developping for Android until 2 weeks ago when he gave me that challenge. I'm also quite new at Java programming in general, so I'm starting from far. But anyway...
So I did most of the tutorial, and I read about Bluetooth in Android, looked at the Bluetooth Chat sample code, and I'm now trying to do my little app. So for my demo, I will try to establish a connection between my real phone and my Bluetooth mouse. I want to move a shape on the screen of my phone in response to my mouse movement.
I encounter many problem, but so far my main one is to open a socket with my unsecure mouse. When I try using the method listenUsingRfcommWithServiceRecord
, it ask a UUID as a parameter. But my mouse most likely doesn't have a UUID to respond, so I guess this method is not the good one.
When I read the documentation about this method, it says that to open an unsecure server socket with a device like a mouse, I must use the listenUsingInsecureRfcommWithServiceRecord
method. But this method is not available when I call it, it gets underlined in red and Eclipse says that it is undefined for the type BluetoothAdapter.
private BluetoothServerSocket connectDevice(BluetoothAdapter adapter, BluetoothDevice device){
BluetoothServerSocket socket = null;
try{
socket = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.randomUUID());
}
catch(IOException e){
Toast.makeText(this, "Connection failed.\n" + e.getMessage(), Toast.LENGTH_SHORT);
}
return socket;
}
Please don't flame me if I'm doing it all wrong, it's my first question here and I'm starting with Java programming.
listenUsingInsecureRfcommWithServiceRecord()
This is only available on API Level 10 and later, i.e., Android v2.3.3 onwards.
That may be the problem if you're building for a version previous to that.
See to the right-hand side of the grey bar in the docs
EDIT: In light of the fact it isn't possible to extend BluetoothAdapter, listenUsingInsecureRfcommWithServiceRecord()
simply does this...
return createNewRfcommSocketAndRecord(name, uuid, false, false);
The source for createNewRfcommSocketAndRecord() (which is a private method of BluetoothAdapter), can be found here...createNewRfcommSocketAndRecord
Not sure if will help but you might be able to reproduce its functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With