Android listen for connections without UUID

2020-07-24 05:48发布

问题:

I have written an Android game that uses Bluetooth to transfer data. I copied the code from the Bluetooth Chat application that comes with the ADK. My phone doesn't seem to be able to connect with UUID so I would like to replace it with other methods. I have already replaced my connecting methods with

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

and now I wonder how I can listen for that kind of connections? Currently there reads

tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,MY_UUID_SECURE);

but this obviously requires the UUID to be sent.

回答1:

Thanks for Mister Smith by providing me with an answer. With a little research I was able to find an another StackOverflow question with the same subject and a good source file with the soluction I was looking for.

What I basically had to do is to get the BluetoothAdapter with private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); and then listen for connections with

Method m = adapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket tmp = (BluetoothServerSocket) m.invoke(adapter, 1);