Android Bluetooth Simple Data Receiver

As a newcomer to Android programming, I have a question: I want to receive data (two bytes) from a bluetooth / serial module connected to a microprocessor. This data should be printed on the screen, and updated - once per second. I found this already: How to prevent the bluetooth connection of the bluetooth bluetooth after death immediately after .connect ()? What should be the working code, but nothing happens. I changed the mac address to 00: 11:12: 05: 03: 67, which should match my bluetooth module.

Am I heading completely the wrong direction with this? I guess I want it quite simply: just a simple data transfer via Bluetooth.

Any thoughts would be greatly appreciated.

Regards,

Kevin

+3
android bluetooth
Feb 06 2018-12-12T00:
source share
1 answer

The question you were talking about is very useful as it provides a workaround for a situation where a regular call to .createRfcommSocketToServiceRecord() not working. I personally used this workaround in the project that I am currently doing. What I do, I first try to make a call to .createRfcommSocketToServiceRecord() , and if that fails, my code then tries to connect to the .getClass().getMethod("createRfcommSocket", new Class[]{int.class}); . After experimenting with multiple Bluetooth-to-serial .createRfcommSocketToServiceRecord() , some of them are generally a bit "awkward," and the last reflection method works when .createRfcommSocketToServiceRecord() does not.

However ... although this question you are referring to gives a very useful compact code, I don't think this is the best place for you. The place you should start is in the Bluetooth documentation for Android , which explains the whole process very well, including how to use separate Threads to handle discovery, connection, etc. It is actually very easy to start using the source code of the Bluetooth Chat . Using this, you can quickly get up and work and connect to the Bluetooth-serial module. You just need to make sure that you change the UUID to the value required for the serial port profile (SPP):

 private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

This, I hope, will be enough to have a simple application that will talk to your serial Bluetooth module. The Bluetooth Chat sample application also provides you with dialog boxes that handle device discovery, pairing, and all these good things, so you don’t have to worry about hard-coding the MAC address of your device like you do.

If you are having trouble connecting, you should be specific about what is actually happening; that is, what exceptions do you get, etc.

+5
Feb 06 '12 at 18:22
source share



All Articles