Receiving data via bluetooth from other device?

I have a device that sends very simple data via Bluetooth to an Android app. I want to receive that data with an Arduino instead. Now the question is, what kind of bluetooth module do I need? Would this module do the job?

And what kind of code do I need to connect/pair and receive data?

The protocol seems to be custom. And the data that I want to extract can be found by searching for simple byte sequences in the received packets.

This is what other people have found out about the protocol:

http://forum.electricunicycle.org/topic/870-gotwaykingsong-protocol-reverse-engineering/?page=1#comment-8607

Ok, I got to a start from the data captures sent by @Tilmann. It's NOT a plaintext protocol, as I first thought, due it being parsing integers from text, but thankfully it seems to be a simpler binary protocol than, say, ASN.1 or Zigbee. I've got to the point where I can locate the following values:

62,84V 0,00km/h 0,000 "mRunNow" (trip-meter?) -0,25A Current 36,530 Temperature (Celsius?)

They're using a somewhat weird (and inefficient) way of parsing the binary data, where it's first turned into a hex-string and at some point decoded to string with GBK-charset ("GBK is an extension of the GB2312 character set for simplified Chinese characters, used in the People's Republic of China"), and then parse the values 2 bytes at a time as base-16 (hex) integers (which are later cast to shorts) from the middle of other data (I didn't notice at first that the integers were parsed as base-16). The datapackets seem to be separated by "18 5A 5A 5A 5A" (as hex), but it's not always the voltage etc. data after that. I'll try to dig deeper when I got the time.

Here's an example of the datacapture with seemingly correct values (after one of the 18 5A 5A... -headers):

INDEX 0 188C Voltage, 1/100th 1 0000 Speed, fixed point, 3.6 * value / 100 2-3 00000000 "mRunNow", 32bit-value, first bytes are high bits (shifted up 16 bits), the later low bits, 1/1000th (so probably meters turned into kilometers) 4 FFE6 Current, 1/100th 5 F808 Temperature, 1/340th + 36.53

The decoding seems to end (to start a new round later) after it detects something like "00 00 04" (called "type_ch" in the code) after the measurement values:

str2 = str1.substring(i + 4, i + 4 + 4); BluetoothChat.this.mPackageTemp[(i / 4)] = Integer.parseInt(str2, 16); continue; str2 = str1.substring(i + 4, i + 4 + 4); BluetoothChat.this.mPackageTemp[(i / 4)] = Integer.parseInt(str2, 16); continue; str2 = str1.substring(i + 4, i + 4 + 2); BluetoothChat.this.type_ch = Integer.parseInt(str2, 16); } } while (BluetoothChat.this.type_ch != 4);

The odotemeter seems to be sent out in it's own packet, terminated again with 00 00 04:

18 5A 5A 5A 5A 55 AA 00 08 82 EE 00 00 00 00 00 00 00 00 00 00 00 00 04

Don't know what the 55 AA is, but the 00 08 82 EE corresponds to 557 806 which is closest to the trip meter value of 557,798km I could find (that should be Tillmans odometer, but I think there's some rounding error, as so far I can't find that exact figure anywhere, and the value is divided by 1000 (to get kilometers from meters) and then formatted for display.

If someone has the Kingsong App, could you please send it to me (or just link where I can get it), if it's using the same protocol, getting it decompiled too might give some more clues.

I want to extract the 5 mentioned values (Voltage, Current, Speed, Temprature and mRunNow).

You may find the examples in Serial Input Basics useful.

The HC06 should work but it is not as capable as a HC05. The HC06 can only act as a slave whereas the HC05 can be master or slave.

...R

Thanks for your answer, Robin2. I already know how the serial communication with Arduino works. I'm worried about the code/commands required to receive data from the Bluetooth connection.

Can assure that the HC05 can handle the kind of protocol being used? In the post I found it says it doesn't use ASN.1 or Zigbee. I assume it is a custom protocol. Oh and it is binary based. Does this matter?

This is the one you were talking about, right? http://www.aliexpress.com/item/Free-shipping-1PCS-LOT-HC05-HC-05-master-slave-6pin-JY-MCU-anti-reverse-integrated-Bluetooth/1934703388.html

On this site I can see the commands required to connect to another device and pair, but what do I have to do to receive the data packets from the other device?

I presume by "protocol" you mean the way the data is contained in the stream of bytes.

If so, the HC05 (or any serial connection) does not need to know anything about it. All it sees is the stream of bytes.

The examples in Serial Input Basics are designed to receive a stream of chars but could easily be changed to receive bytes.

You could adapt the 2nd example to look for the succession of bytes that marks the end of a chunk of data.

You will obviously need to write appropriate code to parse the received data into its elements. The parse example in Serial Input ... should give you the general idea.

It may be a good idea to write a simple program that receives the data (maybe 32 bytes at a time) and just displays it on the Serial Monitor so you know exactly what the Arduino has to deal with.

...R

Okay, so as soon as the HC05 is connected, it will receive the raw Bluetooth packets and pass them through the serial connection?

I'm a bit confused because what would happen to the data stream is you send "AT+STATE" to query the current status of the other device? I mean suddenly there are 2 different things that I could receive. How would I know if it is the status or the other data stream?

felic: I'm a bit confused because what would happen to the data stream is you send "AT+STATE" to query the current status of the other device? I mean suddenly there are 2 different things that I could receive. How would I know if it is the status or the other data stream?

Yes it would be confusing. Don't send AT+State!

Seriously, there is no easy way to disentangle that stuff unless the sender stops sending first.

...R

Well that kinda sucks. :/

edit:

I just realized another problem: If I have to use the Arduino's serial pins for the Bluetooth module, I can't use it to send debugging messages to the serial console on my computer at the same time.

Any suggestions on how I could debug this properly then?

felic: I just realized another problem: If I have to use the Arduino's serial pins for the Bluetooth module, I can't use it to send debugging messages to the serial console on my computer at the same time.

You have not said so I assume you are using an Uno. If so, use SoftwareSerial to create an extra serial port for the HC05 and keep HardwareSerial free for communication with the PC.

A Mega or Leonardo has extra HardwareSerial ports.

...R

I'll probably go with something small. A pro mini probably. 3 pwm pins are already though, for an LED strip.

felic: I'll probably go with something small. A pro mini probably. 3 pwm pins are already though, for an LED strip.

A Micro has a spare Hardware Serial port, whereas a Mini does not.

...R