gatt icon indicating copy to clipboard operation
gatt copied to clipboard

Unhandled event

Open rabarar opened this issue 10 years ago • 23 comments
trafficstars

Not sure if this is an issue or the way the stack is supposed to work - but I get the following output and don't see how a callback would respond to it.

2015/06/22 10:39:37 Unhandled event: xpc.Dict{"kCBMsgId":53, "kCBMsgArgs":xpc.Dict{"kCBMsgArgDeviceUUID":xpc.UUID{0x4c, 0x33, 0x9e, 0x37, 0x99, 0x2a, 0x42, 0xb2, 0x9d, 0xaf, 0xf2, 0x8e, 0x3, 0xc6, 0xbf, 0x5e}, "kCBMsgArgATTMTU":104}}

rabarar avatar Jun 22 '15 14:06 rabarar

Have same problem with discovered iOS device

mark2b avatar Jul 27 '15 05:07 mark2b

Same here with my Mac. I don't know where I can find the meaning of all the MsgId. Would anyone know? GATT doesn't handle the id 53 in function HandleXpcEvent (device_darwin.go).

However, the bluetooth indicator on my mac shows that it is connected to my board!

flemay avatar Aug 04 '15 12:08 flemay

It's a MTU exchange request, which we currently don't handle on the OS X port. In this case, the client (central) should fall back with the default MTU (23) or might drop the connection.

roylee17 avatar Aug 04 '15 18:08 roylee17

@roylee17 Thanks! Would you know where I could find all the MTUs?

flemay avatar Aug 04 '15 23:08 flemay

@roylee17 it looks like @flemay has made some progress on this on his fork. I have an iDevices iGrill2 device that never fully connects on paypal/gatt (halting at the unhandled event warning), but is able to fully discover services and characteristics with flemay/gatt. Would be nice to see that stuff merged in!

bgentry avatar Aug 28 '15 17:08 bgentry

@bgentry That's sound nice. I looked at @flemay's branch, and it seems that some event IDs have been changed since Maverick.

@flemay, are you interested in sending a rebased PR for merging?

roylee17 avatar Aug 28 '15 17:08 roylee17

Yeah, I think looking at noble you'll see that they have different files for Mavericks vs Yosemite support. I guess you'd need to switch on Darwin version for the various event IDs.

bgentry avatar Aug 28 '15 18:08 bgentry

I wonder if there's any OS X C constants we can match against that are more stable across OS X versions.

bgentry avatar Aug 28 '15 18:08 bgentry

@bgentry @roylee17 My changes are mainly based on Noble. He told me the codes change whenever Apple releases a new os version. So he does reverse engineering to maintain Noble. I wish we could specify a version of os in golang the same way we target platform.

At the moment I am not 100% sure if the codes are exact. I am still trying to understand how I can subscribe to notify service using p.SetNotifyValue. Any clue?

flemay avatar Aug 29 '15 06:08 flemay

Hi Guys,

Those event codes are reverse engineered by Noble/Bleno, and we're in debt to them :-)

Not sure what's the best way to tell the OS X version yet, but it seems the following should do the work, and we can replace it later when we find a better (canonical) way to do it.

        osver, err := syscall.Sysctl("kern.osrelease")
        if err != nil {
                return
        }
        fmt.Printf("%v\n", osver) // 14.5.0 on Yosemite

Are you interested in having those event codes merge back with support of Yosemite and Mavericks? It's fine to me even it's not 100% exact. Once we have the support in place, other OS X users would be easier to find a place to keep it update.

Regarding p.SetNotifyValue(), the easiest way is to modify the examples/explorer.go and give it a try: The p.SetNotifyValue() has to be called after descriptors are discovered, or it returns "no cccd" error.

Roy

diff --git a/examples/explorer.go b/examples/explorer.go
index a25fa2f..ad2048a 100644
--- a/examples/explorer.go
+++ b/examples/explorer.go
@@ -8,6 +8,7 @@ import (
        "log"
        "os"
        "strings"
+       "time"

        "github.com/paypal/gatt"
        "github.com/paypal/gatt/examples/option"
@@ -116,9 +117,22 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
                                }
                                fmt.Printf("    value         %x | %q\n", b, b)
                        }
+
+                       // Subscribe the characteristic, if possible.
+                       if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 {
+                               f := func(c *gatt.Characteristic, b []byte, err error) {
+                                       fmt.Printf("notified: % X | %q\n", b, b)
+                               }
+                               if err := p.SetNotifyValue(c, f); err != nil {
+                                       fmt.Printf("Failed to subscribe characteristic, err: %s\n", err)
+                                       continue
+                               }
+                       }
+
                }
                fmt.Println()
        }
+       time.Sleep(5 * time.Second)
 }

roylee17 avatar Sep 04 '15 18:09 roylee17

@roylee17 Thanks for the reply. I have tried your code but couldn't get it work on my mac. Following the noble package, I made the SetNotifyValue to send 68 and then the response number to 74.

So It goes inside the loop() and the rsp.id is 74 but it does not have kCBMsgArgIsNotification field so it never executes the subscribed function. This happens one time.

The loop() will wait indefinitely. It feels like the peripheral never sends notifications.

flemay avatar Sep 27 '15 07:09 flemay

So the above code subscribed to your peripheral's characteristic, but you're not sure does your peripheral actually send notification?

You can test your peripheral with your phone (I use LightBlue on iPhone). Subscribe to the characteristic, and see if that peripheral does send some notification.

roylee17 avatar Sep 27 '15 08:09 roylee17

It does work with noble and lightblue from my Mac without any problems!

flemay avatar Sep 27 '15 08:09 flemay

Not sure this is due to the event ID changes between OS X version, or it's because didn't handle "Indication" correctly.

Since now I only have Yosemite, I'll see what's happening on Yosemite, and get back to you.

roylee17 avatar Sep 27 '15 08:09 roylee17

It also works on my iphone with lightblue.

I don't call SetIndicateValue in my code. I might have to push my latest modification of flemay/gatt so you can play with it!

flemay avatar Sep 27 '15 08:09 flemay

Pushed! :)

flemay avatar Sep 27 '15 08:09 flemay

The rsp.id == 74 inside the loop() now should be changed to 71. It meant to tell weather a "read response" is a response to earlier read request, or is an asynchronous notification.

Now the problem is - there is no further report of event 71, even we seem to successfully subscribe to the characteristic. I can see my peripheral sending notifications after the subscription. But they are not reported. Will look into it.

roylee17 avatar Sep 28 '15 07:09 roylee17

Thank you so much for investigating! :)

flemay avatar Sep 28 '15 10:09 flemay

Sorry for the late response , and taking so long to resolve this (hopefully)

There are actually two issues here:

  1. The device is not initialized correctly. Though we did specify the MacDeviceRole for NewDevice(), I failed to use the specified value to initialize the device. In this case, the corebluetooth (OS X) doesn't report the "peripheralConnected:38" (and also characteristicRead :71) events. So the initialization code for the connection handling is not called.

9a3c51a7f30a07900479ab1b82a839968a4610b5 should fix this.

  1. The event ID are changed.

The event IDs were ported from last year for Mavericks, so we need to update it for Yosemite. Thanks for @flemay for porting the changes.

Let me know how it works for you :)

roylee17 avatar Oct 11 '15 22:10 roylee17

What's the status of this issue, is there anything I can do to help? (I'm on Mavericks and have an example code + BLE device)

mattetti avatar Nov 23 '15 04:11 mattetti

I will try the fix tonight and let you know! I don't know about Mavericks as I am working with yosemite/el-capitain :)

flemay avatar Nov 23 '15 05:11 flemay

For what it's worth, I think it's totally fine to only target the latest 2 releases of OS X

mattetti avatar Dec 01 '15 15:12 mattetti

hello with go 1.10 and when i build server.go i have this message "undefined: central" any help please !!?

barustnt avatar Aug 09 '18 12:08 barustnt