Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect requests are not responding #8

Open
magallardo opened this issue Mar 27, 2020 · 20 comments
Open

Connect requests are not responding #8

magallardo opened this issue Mar 27, 2020 · 20 comments

Comments

@magallardo
Copy link

Hello,

I was able to get the code compiled and seems to be working ok until I call ble.Connect. After calling connect I don't get any response from the peripheral.

Also, I have noticed that I am getting events of type 42 but the code does not handle that event type.
2020/03/26 20:51:29 event: 42 xpc.Dict{"kCBAdvDataDeviceAddress":[]uint8{0x6c, 0xf9, 0x45, 0xc7, 0x5c, 0xbb}}. These events are comming even after I called ble.StopScanning.

Following is a sequence of what I am doing:

  1. call ble.init
  2. get an stateChange event (6) for powerOn. I call ble.StartScanning
  3. get a discover event (57) for several peripherals. I select one peripheral based on the name and call ble.Connect(peripheral.Uuid)
  4. After the connect command, the peripheral does not respond. From time to time, the peripheral will issue a 42 event.

Please advise if something else is required to be sent for getting a connection response.

Thanks,
Marcelo

@raff
Copy link
Owner

raff commented Mar 27, 2020

The problem is that this implementation uses XPC (as the original Noble implementation for node.js did).

With XPC, some of the event codes seem to change on every MacOS release, and they are not documented. So at every releases Goble breaks until I figure out the new event codes and update.

I used to be able to look at the Node.js code and take the new codes from there, but they have updated to use the new MacOS Bluetooth APIs, and it would be a lot of work for me (almost a full rewrite) to update this package.

@magallardo
Copy link
Author

So, Node.js implementation is not using xpc anymore?
Can you advise what will be the best way to solve this issue? I am willing to help if you provide some initial guidance. Thanks

@raff
Copy link
Owner

raff commented Mar 27, 2020

Ok, I made some changes and now the discovery works. There may be other things that don't work, if you find anything let me know.

I found a pull request for noble.js with some updates for catalina (see https://github.com/lzever/noble/blob/master/lib/mac/catalina.js) but I am not sure of how good they are (the message id for "start scanning" was wrong).

@magallardo
Copy link
Author

@raff The discovery was working for me before. What was not working is the connect. I will give it another try. Thanks

@raff
Copy link
Owner

raff commented Mar 27, 2020

There should be a change for the connect also. Let me know if it works.

@magallardo
Copy link
Author

@raff This is much better. Now I am getting a connect event back from the peripheral. Thank you so much for your help.
I am going to continue with services and characteristics now.
Thanks again!!!!

@magallardo
Copy link
Author

@raff Just wanted to give you an update. It looks like there is still a problem after the connect. When I send a DiscoverServices request, I am getting a response with code 58 which currently is being handled as a response to a connect. I think 58 needs to match with the ServiceDiscovery response handler.

Following is a trace of the events and you can see the issue:

2020/03/27 15:52:57 sendCBMsg xpc.Dict{"kCBMsgArgs":xpc.Dict{"kCBMsgArgName":"goble-1585338777", "kCBMsgArgOptions":xpc.Dict{"kCBInitOptionShowPowerAlert":0}, "kCBMsgArgType":0}, "kCBMsgId":1}
message delivered
2020/03/27 15:52:57 event: 42 xpc.Dict{"kCBAdvDataDeviceAddress":[]uint8{0x78, 0x22, 0x69, 0x64, 0x41, 0x52}}
2020/03/27 15:52:57 done event: 42

// Receive StateChange
2020/03/27 15:52:57 event: 6 xpc.Dict{"kCBMsgArgBTPowerState":1, "kCBMsgArgState":5}
2020/03/27 15:52:57 done event: 6

// Send StartScanning
2020/03/27 15:52:57 sendCBMsg xpc.Dict{"kCBMsgArgs":xpc.Dict{"kCBMsgArgOptions":xpc.Dict{}, "kCBMsgArgUUIDs":[]string{}}, "kCBMsgId":51}
message delivered

//Receive Discover
2020/03/27 15:52:57 event: 57 xpc.Dict{"kCBMsgArgAdvertisementData":xpc.Dict{"kCBAdvDataChannel":37, "kCBAdvDataIsConnectable":1, "kCBAdvDataLocalName":"STWIN12", "kCBAdvDataManufacturerData":[]uint8{0x1, 0x2, 0x20, 0xfd, 0x0, 0x0, 0xc0, 0x70, 0x1f, 0x32, 0x1f, 0x33}, "kCBAdvDataTxPowerLevel":0}, "kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}, "kCBMsgArgName":"STWIN12", "kCBMsgArgRssi":-79}
2020/03/27 15:52:57 done event: 57

// Send Stop Scanning
2020/03/27 15:52:57 sendCBMsg xpc.Dict{"kCBMsgArgs":xpc.Dict(nil), "kCBMsgId":52}
message delivered

// Send Connect
2020/03/27 15:52:57 sendCBMsg xpc.Dict{"kCBMsgArgs":xpc.Dict{"kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}, "kCBMsgArgOptions":xpc.Dict{"kCBConnectOptionNotifyOnDisconnection":1}}, "kCBMsgId":53}
message delivered

// Receive Connect
2020/03/27 15:52:58 event: 86 xpc.Dict{"kCBMsgArgATTMTU":220, "kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}}
2020/03/27 15:52:58 done event: 86

// Send DiscoverServices
2020/03/27 15:52:58 sendCBMsg xpc.Dict{"kCBMsgArgs":xpc.Dict{"kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}, "kCBMsgArgUUIDs":[]string{}}, "kCBMsgId":77}
message delivered

// Receive 58 (This event is going to Connect response handler again)
2020/03/27 15:52:58 event: 58 xpc.Dict{"kCBMsgArgATTMTU":220, "kCBMsgArgConnectionRole":0, "kCBMsgArgConnectionTransport":2, "kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}}
2020/03/27 15:52:58 done event: 58

Please let me know what you thing.

Thanks,
Marcelo

@raff
Copy link
Owner

raff commented Mar 27, 2020

You can try to do what I did for descriptorDiscover (case 75). In connect (case 58):

if id == 58 && ble.utsname.Release >= "19." { id = 54 // or 82 goto retry_switch }

I guess I could also move the connect case right before the serviceDiscover case and do a "followthrough".

Try it out and if it works, I'll update.

@magallardo
Copy link
Author

That fixed the flow but somehow I was thinking that either the discover or the connect responses will provide the UUIDs for the services available. Right now, when I call DiscoverServices with empty list (as I did not get anything from the discover or connect commands), I am getting back empty list so I can't proceed from there.

Thanks

@magallardo
Copy link
Author

Also, I noticed that right after recieving the 58 event (which was rerouted to 82), I also received an event with id 60 which is being handled as a Discover event but because it does not have advertisement data, it is ignored.
The message looks like the following:

2020/03/27 17:13:02 done event: 82
2020/03/27 17:13:02 done event: 58
2020/03/27 17:14:46 event: 60 xpc.Dict{"kCBMsgArgDeviceUUID":xpc.UUID{0xab, 0x53, 0xb5, 0xb1, 0x6f, 0x8f, 0x4f, 0x88, 0x89, 0xb2, 0xcb, 0x89, 0x95, 0x95, 0x75, 0x1c}, "kCBMsgArgResult":307}
2020/03/27 17:14:46 done event: 60

Is this event something that is returning the service uuid?

Thanks

@raff
Copy link
Owner

raff commented Mar 27, 2020

I really don't know. Unfortunately none of this is documented, and I don't know BLE that much. I was mainly going with what noble.js was doing.

Anyway, I'll update the code to support the connect and see if I find anything else.

@magallardo
Copy link
Author

Thanks so much. Can you please point to what version of noble.js you are looking at? I found a few of them and I could not figure it out which one to look for guidance.

@raff
Copy link
Owner

raff commented Mar 27, 2020

This https://github.com/lzever/noble/ has a branch with Catalina support (https://github.com/lzever/noble/blob/master/lib/mac/catalina.js) but the message codes seem to be wrong.

I used to look at https://github.com/noble/noble, but it seems that is not mantained anymore. And this one https://github.com/abandonware/noble has switched to use the new MacOS BLE APIs. I thought of doing a wrapper for their C code (it's actually ObjectiveC - see https://github.com/abandonware/noble/tree/master/lib/mac/src) but I don't really have time at this point.

@magallardo
Copy link
Author

I was looking at some go implementations and it looks like the Service and Characteristics requests are treated differently than scanning and discover which are asynchronous. The Services and Characteristics requests seem to be done synchronously. I will continue digging a little bit more. Thanks

@magallardo
Copy link
Author

@raff I was able to get the sample underhttps://github.com/abandonware/noble working and I am getting the correct results. Is anyway to figure it out the correct codes from this? Thanks and sorry for so many questions.

@raff
Copy link
Owner

raff commented Mar 29, 2020

Again, this version uses the new MacOS Bluetooth APIs. Completely different than the original version. To make it accessible to Go it would require a complete rewrite of goble (that I can't do at this time).

@andrewarrow
Copy link

andrewarrow commented Oct 1, 2020

just wondering if there is a current golang mac Catalina 10.15.7 ble package or are the node.js ones the only working at this point?

@alexhowarth
Copy link

alexhowarth commented Oct 1, 2020 via email

@raff
Copy link
Owner

raff commented Oct 1, 2020

Somebody else suggested to have a look at this: go-ble/ble#67
I didn't try it yet, but if you really need something you may want to check it out.

@raff
Copy link
Owner

raff commented Oct 1, 2020

ok, I guess we were looking at the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants