niedziela, 3 lipca 2016

Android - how to communicate with PC using serial port over bluetooth

One time I needed to simulate a device which communicates with PC over bluetooth. I thought that I can use my smartphone with Android because this is faster way than creating new device using microcontroler and bluetooth module. I needed only my phone, Android Studio and a USB cable. I used code from Bluetooth Android Guide.
The most important line is:
 tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
Name parameter can be any value, but in the UUID you should pass correct value if you want to use serial port. This value is:
UUID.fromString(00001101-0000-1000-8000-00805F9B34FB)

Where base UUID is "00000000-0000-1000-8000-00805F9B34FB" and 0x1101 is the UUID of "SerialPort" service class. You can check service classes here.
After uploading application to the device and running application, you have to pair your phone with a PC. Sometimes COM port (on Windows) is binded just after pairing, but often you must do it manually. To do this you have to open bluetooth settings.


Then go to the "COM Ports" tab and click "Add". Select outgoing type and also your device from listbox. You should also see your application name below.



Click OK and you should get your COM port number.

Brak komentarzy:

Prześlij komentarz