goble icon indicating copy to clipboard operation
goble copied to clipboard

Connect requests are not responding

Open magallardo opened this issue 4 years ago • 20 comments

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

magallardo avatar Mar 27 '20 01:03 magallardo

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.

raff avatar Mar 27 '20 02:03 raff

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

magallardo avatar Mar 27 '20 13:03 magallardo

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).

raff avatar Mar 27 '20 17:03 raff

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

magallardo avatar Mar 27 '20 18:03 magallardo

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

raff avatar Mar 27 '20 19:03 raff

@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 avatar Mar 27 '20 19:03 magallardo

@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

magallardo avatar Mar 27 '20 20:03 magallardo

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.

raff avatar Mar 27 '20 21:03 raff

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 avatar Mar 27 '20 21:03 magallardo

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

magallardo avatar Mar 27 '20 21:03 magallardo

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.

raff avatar Mar 27 '20 21:03 raff

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.

magallardo avatar Mar 27 '20 21:03 magallardo

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.

raff avatar Mar 27 '20 21:03 raff

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 avatar Mar 27 '20 22:03 magallardo

@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.

magallardo avatar Mar 29 '20 01:03 magallardo

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).

raff avatar Mar 29 '20 01:03 raff

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?

andrewarrow avatar Oct 01 '20 16:10 andrewarrow

You might find the following repo useful: https://github.com/JuulLabs-OSS/ble

On Thu, 1 Oct 2020 at 12:26, Andrew Arrow [email protected] wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/raff/goble/issues/8#issuecomment-702250323, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALLO6XYQYJAQGIOLJUMLP3SISUSBANCNFSM4LUU6LRQ .

alexhowarth avatar Oct 01 '20 17:10 alexhowarth

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

raff avatar Oct 01 '20 17:10 raff

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

raff avatar Oct 01 '20 17:10 raff