开发者

Blocking an Android thread with a dialog

开发者 https://www.devze.com 2023-01-29 23:43 出处:网络
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.Th

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

  1. search
  2. select a device
  3. connect:
  4. if the device is paired then connect.
  5. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消