-
Notifications
You must be signed in to change notification settings - Fork 173
macos: initial support for scanning/connecting/write characteristics/notifications #7
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
Conversation
Latest commit builds and can scan on macOS.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few initial comments on the draft.
Too bad CoreBluetooth doesn't expose MAC addresses, that makes the whole API less uniform. I had made the (apparently incorrect) assumption that a MAC is so basic that it'll be exposed everywhere.
I have now marked this PR as ready for review. I think it wold be good to get any other changes and then get it merged into |
I added the last functionality needed to declare this PR "done" which was to implement |
OK, just two more commits now with write characteristics. |
So, if I understand correctly the methods such as |
In order to take advantage of a bunch of the predemined functionality by using https://github.com/tinygo-org/bluetooth/blob/macos/adapter_darwin.go#L12 you need to implement those methods. I think without this there would be a lot more of them! macOS is macOS, we just do what they say. |
} | ||
|
||
// Connect starts a connection attempt to the given peripheral device address. | ||
func (a *Adapter) Connect(address Addresser, params ConnectionParams) (*Device, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be a good idea to use some sort of check or mutex, right now it appears that it is not possible to connect to more than one device at once (because of a.connectChan
for example).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial implementation only allows for one device, I was not planning on working on that now.
p.SetDelegate(d) | ||
return d, nil | ||
case <-time.NewTimer(10 * time.Second).C: | ||
return nil, errors.New("timeout on Connect") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation says:
After successfully establishing a local connection to a peripheral, the central manager object calls the centralManager:didConnectPeripheral: method of its delegate object. If the connection attempt fails, the central manager object calls the centralManager:didFailToConnectPeripheral:error: method of its delegate object instead. Attempts to connect to a peripheral don’t time out.
Ideally we'd use these instead of laying our own error handling on top of this.
(Of course, didFailToConnectPeripheral
is secretly just a timeout but it's a timeout on a much lower level).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just a simple implementation, but can probably connect that easily enough.
case <-time.NewTimer(10 * time.Second).C: | ||
return nil, errors.New("timeout on DiscoverServices") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this doesn't really time out either, it either succeeds or the entire connection is broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above comment.
case <-time.NewTimer(10 * time.Second).C: | ||
return nil, errors.New("timeout on DiscoverCharacteristics") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as my previous comment.
return errors.New("must provide a callback for EnableNotifications") | ||
} | ||
|
||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to leave this function unimplemented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I am working on it, and left it unimplemented for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has now been implemented.
I see. However, they don't need to be on the type Adapter struct {
delegate centralManagerDelegate
}
type centralManagerDelegate struct {
cbgo.CentralManagerDelegateBase
a *Adapter
}
// CentralManagerDidUpdateState when central manager state updated.
func (delegate *centralManagerDelegate) CentralManagerDidUpdateState(cmgr cbgo.CentralManager) {
// powered on?
if cmgr.State() == cbgo.ManagerStatePoweredOn {
close(delegate.a.poweredChan)
}
// TODO: handle other state changes.
} This provides the same functionality without cluttering the API. |
I discovered I can remove a bunch of the implementation that is not being used, coming in future commit. Also, I was trying to have a simpler implementation with less indirection, since there is already a lot of complexity in the macOS CB code itself. |
Added some delegate types, and have otherwise handled all outstanding feedback items, I think. |
Added characteristic notifications. No more features in this PR, I mean it! 😺 |
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
…ead of MAC as the BLE address for a peripheral Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
…ntation Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: deadprogram <ron@hybridgroup.com>
I squashed the commits in this branch down based on the functionality, and rebased against Basically, we can now finally ready to merge! Thanks for all your help on this @aykevl |
@aykevl any more feedback on this PR? |
I'll try to get to this PR tomorrow. I was a bit busy with unrelated things (which is now mostly done). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Feel free to merge, unless you want to fix the nit.
Signed-off-by: deadprogram <ron@hybridgroup.com>
Now merging, thank you very much @aykevl for your help landing this PR! |
This PR adds the initial support for macOS with nothing more than scanning implemented.
It is currently WIP so I am making it draft.