Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

bluetooth - Specifying a link key in android without pairing

I am trying to determine if there is a way in android to associate a specific link key with an already existing remote bluetooth device instance.

Essentially what I want to do is create a connection with a non-discoverable bluetooth device without going through the pairing or re-pairing procedure.

I am not able to establish the link key with the device in a standard pairing procedure because I am working with a custom proprietary pairing mechanism. I would prefer to accomplish this task without using native code, but if I have to then I will.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I needed to solve this problem myself. The crucial step was finding this code, which told me to add the android.bluetooth package to my project, and add the files IBluetooth.aidl and IBluetoothCallback.aidl (which you'll find at the link).

Once you instantiate the IBluetooth object, you have access to the BluetoothService class, and can use any of the methods in IBluetooth.aidl. The method I was interested in was

setPin(String address, byte[] pin)

The problem with using it is the other Bluetooth code expects the pairing dialog to have been called already, and keeps track of that in a HashMap in the BluetoothEventLoop class. If you try calling setPin() without having initiated a pairing request, you'll see an error like this:

setPin(<address>) called but no native data available, ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device or by bluez.

So the workaround for me (using the Chat example) was starting the connect thread to initiate the pairing request, then sleeping 500 ms to ensure the thread had started, then calling setPin().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...