I am trying to develop an Android application for a medical device using Bluetooth SPP. The Android application works as a Bluetooth server. The problem is that the medical device (UA-767PBT) may not conform to the SDP process and uses a fixed port # to connect. Thus, the connection only works after rebooting the Android device.
I am looking for a way to create a socket on the server side of Bluetooth with a specific rfcomm channel / port using Java reflection.
I found code doing similar things using Java reflection:
Create a socket with a Bluetooth client interface with a specific port #.
//Instead of using createRfcommSocketToServiceRecord Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); // using port
Check port # used in the socket on the server side of the Bluetooth.
BluetoothServerSocket serverSocket; BluetoothSocket socket; int port; serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(SERVICE_NAME, DEVICE_UUID); Field mSocketField = BluetoothServerSocket.class.getDeclaredField("mSocket"); mSocketField.setAccessible(true); socket = (BluetoothSocket) mSocketField.get(serverSocket); mSocketField.setAccessible(false); Field mPortField = BluetoothSocket.class.getDeclaredField("mPort"); mPortField.setAccessible(true); port = (Integer) mPortField.get(socket); mPortField.setAccessible(false); Log.d("BT Server:", "The random port#: " + port); socket = serverSocket.accept();
Create a socket on the server side of the Bluetooth with a specific port number. How?
I am also looking for other solutions for this connection problem.
source share