I have an Android application that connects to Bluetooth devices somewhat freely. When I attempt to connect to a device that isn't paired with the phone, a dialog will prompt the user for a passkey. The passkey is known a priori, but as there is no method to programatically enter it (in the standard Android APIs at least), it must be shown to the user then entered.
What I have presently is a thread that initiates a connection by grabbing a BluetoothSocket
and calling .connect()
on it. If pairing is required, that call will block until the user enters a passkey in the dialog.
In order to preempt that, I added to my activity's handler someth开发者_如何学Going to display dialogs. Before sending the message I made a dummy object to pass along, and between sending the message and attempting to connect, called .wait()
on it, figuring that I could .notify()
after receiving an OnClick
from the dialog, however I'm unable to refer to the handled message in the inner AlertDialog
class, as is the problem in this question.
I could have it do a call back up to the class holding the thread, but it seems a little clunky, leading me to the core question: is my structure fundamentally broken? How should I go about it?
Another related question suggests to use the notification system, however it seems much better design here to mirror the passkey dialog with another dialog.
I think Romain Guy's answer in the link you provided might help. You can pass information between Activities as needed.
Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)
Don't block, just implement Interfaces
and Fire using them (interface = listener),
- search
- select a device
- connect:
- if the device is paired then connect.
- if not paired show dialog, and on positive click, connect.
BWT, i think you need to extend Dialog, and implements your own methods, to hold some information about the Device you are trying to pair with, and i prefer you implements Own Interface too.
精彩评论