Simple Bluetooth data receiver Android
Asked Answered
A

1

3

As a beginner in 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 to the screen and updated say once per second. I found this already: How to prevent Android bluetooth RFCOMM connection from dying immediately after .connect()? Which should be a working code but nothing is happening. I changed the mac address to 00:11:12:05:03:67, which should correspond with my bluetooth module.

Am I heading completely in the wrong direction with this? I guess what I want is quite basic: just a simple one way data transmission over bluetooth.

Any thoughts will be very much appreciated.

Best wishes,

Kevin

Alanson answered 6/2, 2012 at 17:12 Comment(0)
F
5

The question you've referred to there is very useful as it provides a workaround for a situation where the usual call to .createRfcommSocketToServiceRecord() fails to work. I have personally used that workaround solution in a project I'm doing right now. What I do is I attempt the .createRfcommSocketToServiceRecord() call first, and if that fails, my code then attempts to connect with the .getClass().getMethod("createRfcommSocket", new Class[]{int.class}); workaround. Having experimented with a number of Bluetooth-to-serial PCBs, some of them tend to be a bit 'awkward' and the latter reflection method works when the .createRfcommSocketToServiceRecord() does not.

However... although that question you referenced does provide a very useful compact piece of code, I don't think it's the best place for you to start. The place you should start is at Android's Bluetooth documentation, which explains the whole process extremely well, including how to use separate Threads to handle discovery, connection, etc. In fact it's really easy to get started using the Bluetooth Chat source code. Using that, you can get up and running and connected to a Bluetooth-serial module very quickly. You just need to make sure you change the UUID to that required for Serial Port Profile (SPP):

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

That will hopefully be enough to have a simple application that will talk to your Bluetooth serial module. The Bluetooth Chat example application also provides you with dialogs that handle device discovery, pairing, and all that good stuff, so you don't have to mess about hard-coding in your device's MAC address like you have been.

If you have problems connecting then you need to be specific about what's actually happening; i.e., what exceptions you're getting, and so forth.

Firdausi answered 6/2, 2012 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.