felis / USB_Host_Shield_2.0

Revision 2.0 of USB Host Library for Arduino.
https://chome.nerpa.tech
1.78k stars 782 forks source link

Two Arduinos connected over Bluetooth SPP? #79

Open magictaler opened 10 years ago

magictaler commented 10 years ago

Hi Kristian,

Here is an 'addon' to 'USB Host Shield 2.0' library: http://magictale.com//download/USB_Host_Bluetooth_SPPi.zip

It allows to initiate connection to a remote device over SPP. Theoretically speaking, now it is possible to connect two Arduinos with SPP, however, it has been only tested for 'Arduino + ELM327'.

Would you mind to incorporate it into your library provided you find it useful?

Thank you, Dmitry

Lauszus commented 10 years ago

Thank you @magictaler! I will hopefully have time to get it merged into master this week :)

xxxajk commented 10 years ago

nice idea :-) On Apr 5, 2014 11:12 AM, "Kristian Sloth Lauszus" notifications@github.com wrote:

Thank you @magictaler https://github.com/magictaler! I will hopefully have time to get it merged into master this week :)

— Reply to this email directly or view it on GitHubhttps://github.com/felis/USB_Host_Shield_2.0/issues/79#issuecomment-39640985 .

magictaler commented 10 years ago

Thank you guys!

Lauszus commented 10 years ago

Sorry for not getting back to you. I have been really busy this week, but I got one week vacation now, so I should be able to get it merge into master soon.

Lauszus commented 10 years ago

@magictaler I just merged in your code: https://github.com/felis/USB_Host_Shield_2.0/commit/ee90afde31006e6686e8dc9e3df714a83ef6ee5a :) I only got one question. Why did you modify the SPP constructor?

Lauszus commented 10 years ago

Hmm the code does not work with my Mac or a Bluetooth module I had laying around. I will look into it in the next couple of weeks, so I can get it working.

magictaler commented 10 years ago

Lauszus: Why did you modify the SPP constructor?

That way you can choose whether to wait for a any remote device to initiate paring process (your original logic) or initiate pairing process ourselves to a remote device with specific address. In first case you construct the instance like this: SPP SerialBT(&Btd, "Lauszus's Arduino", "1234"); in second case you do it like this: uint8_t remote_addr[6] = {0x58, 0x69, 0x00, 0x11, 0x22, 0x40}; SPPi SerialBT(&Btd, "Luminardo", "1234", true, (uint8_t*)&remote_addr);

Now you can argue about fourth boolean argument 'pair' - and yes, probably it becomes redundant.

Lauszus commented 10 years ago

Yes it makes sense when you are using the SPPi class, but why would you want this when using SPP? If you set it to true and parse the remote address in the constructor you will make it pair with the device, but the SPP library doesn't know how to connect to it, so it will simply make a HCI connection and then just sit there - the other device will most likely just disconnect from it when it has taken too long.

Lauszus commented 10 years ago

Btw:

SPPi SerialBT(&Btd, "Luminardo", "1234", true, (uint8_t*)&remote_addr);

Can just be written as:

SPPi SerialBT(&Btd, "Luminardo", "1234", true, remote_addr);
magictaler commented 10 years ago

Agreed, SPP doesn't need this logic.

Lauszus commented 10 years ago

Okay good, then I will just remove it. I renamed SPP and SPPi to SPPServer and SPPClient respectively to prevent too much confusion: https://github.com/felis/USB_Host_Shield_2.0/commit/b516af57dff1cd1ac2f45691f5f09c8f8ee51a95.

I will update the readme with some more information when I merge it into master.

magictaler commented 10 years ago

Great. You also raised a question about missing code or missing "SDP_task()" but I don't see this question in this discussion tree. Have you removed it already?

magictaler commented 10 years ago

Lauszus: Hmm the code does not work with my Mac or a Bluetooth module I had laying around. I will look into it in the next couple of weeks, so I can get it working.

Can you maybe tell at which point it stopped? Did you manage to pair, to establish connection to SDP service etc?

Lauszus commented 10 years ago

You also raised a question about missing code or missing "SDP_task()" but I don't see this question in this discussion tree. Have you removed it already?

Yes I did, but I removed it, as I first tested it with my Android phone. It would not become the master in the connection (It just sit there after it received a SDP connection request) - I believe that I will need to setup the SDP connection and not the other way around. My Mac on the other hand initialized the SDP connection after it receives it, so I guess that your Bluetooth module does this too?

Can you maybe tell at which point it stopped? Did you manage to pair, to establish connection to SDP service etc?

My Mac reponse to "SDP Service Search Attribute Response 2" is "SDP_ErrorResponse", so for some reason it does not like the data it receives, but everything up to that point worked just fine.

It's pretty late here, but I will post the full output tomorrow.


Could you please confirm that the code from the SPPClient branch?

magictaler commented 10 years ago

Hmm, actually, the logic in SPPi is implemented in such a way that Arduino is not waiting, it is actively sending SDP connection request and when it is established, it is sending 'Service search attribute request'...

As for your Mac... I have a sneaking suspicion that it doesn't like second 'Service search attribute request' because of last two bytes which contain 'remaining length'. For some reasons my ELM327 responds with 'bytes received', not 'remaining' and that value is used for second request... not sure what it will be in case of Mac, would be interesting to see.

Let me get back home first (as now I am at work :) ) and then I will be able to test your code with my hardware.

Lauszus commented 10 years ago

Yes it sends out the SDP connection request, but it then relies on the device to setup the rest of the SDP connection. I suspect that not all devices do that, as my Android phone sends an SDP connection response and after that a SDP connection request.

Yes I suspect that too. I still don't understand why you just send out the same package one more time: https://github.com/felis/USB_Host_Shield_2.0/blob/SPPClient/SPPClient.cpp#L574-L615? Normally you would use this to split up the request into two as I did here: https://github.com/felis/USB_Host_Shield_2.0/blob/SPPClient/SPPServer.cpp#L526-L624, but to be honest I still havn't understand how SDP works completely - I will read up on that.

magictaler commented 10 years ago

Lauszus: I still don't understand why you just send out the same package one more time.

Because we are talking about different sides here: my code is an SDP client, it is sending request, reply doesn't fit in one packet so my code is sending same request again (with the only difference in 'remaining length') so that we can get the remaining data;

Your piece of code is a reply from SDP service - and in your case you have two different packets as you simply couldn't fit the reply in a single response.

Have a look at this: http://atmega.magictale.com/1749/bluetooth-for-dummies-getting-data-from-sdp/

magictaler commented 10 years ago

Kristian, here are some results for today (using your updated library).

Arduino SPPClient + ELM327 gives the following output:

Luminardo USB Host Bluetooth SPPi Test
SPP Bluetooth Library Started
Enabling VBus... enabled
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:19:0E:12:65:6A
The name is set to: Luminardo
Pairing to 'Other' device with predefined address
Device: 40:22:11:00:69:58 has been found
Connecting to 'Other' device...
Connected to 'Other' device
Received Key Request
Bluetooth pin is set too: 1234
Pairing successful with 'Other' device
SDP Connection Request Sent
L2CAP Connection Response
SDP Connection Response
SDP Configuration Request Received
SDP Configuration Response Sent
SDP Configuration Request Sent
SDP Configuration Response Received
SDP Successfully Configured
SDP Service Search Attribute Request 1 Sent
Channel will be in the next packet
SDP Service Search Attribute Request 2 Sent
Channel found in second packet: 16
RFComm Connection Request Sent
L2CAP Connection Response
RFComm Connection Response
RFComm Configuration Request Received
RFComm Configuration Response Sent
RFComm Configuration Request Sent
RFComm Configuration Response Received
RFComm Successfully Configured
RFComm SABM Sent
Received UA RFComm Packet on channel: 00
Sent UAH RFComm Cmd BT_RFCOMM_PN_CMD (Parameter Negotiation Request) on channel: 00
Received UIH RFComm Packet on channel: 00 - BT_RFCOMM_PN_RSP (Parameter Negotiation Response)
RFComm 2-nd SABM Sent
Received UA RFComm Packet on channel: 10
Send UIH RFComm Cmd BT_RFCOMM_MSC_CMD (Modem Status Command)
Received UIH RFComm Packet on channel: 00 - BT_RFCOMM_MSC_CMD (Modem Status Cmd)
Send UIH RFComm Cmd BT_RFCOMM_MSC_RSP (Modem Status Response)
Received UIH RFComm Packet on channel: 00 - BT_RFCOMM_MSC_RSP (Modem Status Response)
RFComm Cmd with Credit Sent
Serial Bluetooth connected

However, the very last reply to our cmd 'ATZ' from ELM is missing - should be '>LM327 v1.5'

Arduino SPPClient + Lubuntu:

Luminardo USB Host Bluetooth SPPClient Test
SPP Bluetooth Library Started
Enabling VBus... enabled
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:19:0E:12:65:6A
The name is set to: Luminardo
Pairing with 'Other' device with predefined address
Device: 00:15:83:03:B1:B5 has been found
'Other' device found
Connecting to 'Other' device
Connected to 'Other' device
Received Key Request
Bluetooth pin is set too: 1234
Pairing successful with 'Other' device
SDP Connection Request Sent
Information request
L2CAP Unknown Signaling Command: 02

Arduino SPPClient + WinXP:

Luminardo USB Host Bluetooth SPPClient Test
SPP Bluetooth Library Started
Enabling VBus... enabled
Bluetooth Dongle Initialized
HCI Reset complete
Write class of device
Local Bluetooth Address: 00:19:0E:12:65:6A
The name is set to: Luminardo
Pairing with 'Other' device with predefined address
Device: 00:15:83:03:B1:B5 has been found
'Other' device found
Connecting to 'Other' device
Connected to 'Other' device
Received Key Request
Bluetooth pin is set too: 1234
Pairing successful with 'Other' device
SDP Connection Request Sent

That is it for today, will continue tomorrow.

Lauszus commented 10 years ago

I almost got it working by changing the L2CAP UUID: https://github.com/felis/USB_Host_Shield_2.0/blob/SPPClient/SPPClient.cpp#L589-L590 with the Serial port UUID (0x1101). Is there any reason why you don't just do that?

Lauszus commented 10 years ago

Also what is the reason for only setting this: https://github.com/felis/USB_Host_Shield_2.0/blob/SPPClient/SPPClient.cpp#L592-L593 to 0x0026? I believe we can just set this to:

l2capoutbuf[10] = 0x00;
l2capoutbuf[11] = BULK_MAXPKTSIZE - 14;
magictaler commented 10 years ago

Lauszus: I almost got it working by changing the L2CAP UUID with the Serial port UUID (0x1101).

You're right, this would be more appropriate as in this case we would request attributes for RFComm service instead of asking for all attributes of all supported profiles. And given that any operating system obviously supports plenty of them response will be multiple packets, not one or two.

magictaler commented 10 years ago

Lauszus: Also what is the reason for only setting this: https://github.com/felis/USB_Host_Shield_2.0/blob/SPPClient/SPPClient.cpp#L592-L593 to 0x0026? I

I got this number after studying your code: AttributeListsByteCount in SPP::serialPortResponse1 :)

Yes, we can set it to what you suggested.

Lauszus commented 10 years ago

I got this number after studying your code: AttributeListsByteCount in SPP::serialPortResponse1 :) Yes, we can set it to what you suggested.

Okay great :)

L2CAP Unknown Signaling Command: 02

This is caused by the fact that I haven't implemented "SDP_ServiceSearchRequest". I will look into that as well, as I have recently received some emails from people that had the same issue with the computer/smartphone.