I have been reading the Bluetooth documentation on Android's developer site.
I want to pair a Bluetooth device and then connect to it, but I don't want the request dialog to pop up (or even if it does set the pin using some API to dismiss it). I want to do it in Android 4.3+.
So far I see this involving the following steps:
1) App discovers device : use BroadcastReceiver to get BluetoothDevice
2) App pairs the device : uses BluetoothDevice.createBond(), and registers a BroadcastReceiver to get confirmation of pairing completion
3) App connects to device : uses BluetoothDevice.createRfCommSocketToServiceRecord(UUID)
My understanding is that the pop dialog for pairing will be shown to the user after step 2.
I know there is a setPin() API (now public) in the BluetoothDevice class.
If I call it right after step 2, will the dialog still appear and wait for user input, or will it appear and then dismiss, resulting in pairing success ?
Also does step 3 involve any input from the user ?
Step 3 doesn't involve user input.
I've been trying to do exactly the same, I use setPin() and it works, but the user Input dialogue is still there, there is an option to use cancelPairingUserInput but i'm not sure if this is still valid for API19 (that is the one which uses setPin() as far as I know...) or if there is another way to do it.
Hope this helps...
Related
I have a list of BluetoothDevice and I want to pair programmatically with one of them that has a PIN.
I have read several posts here in which the subject is discussed, but I've found two very different approachs.
FIRST OPTION: You call the device.createBond() method. Then, on a BroadcastReceiver, you listen the BluetoothDevice.ACTION_PAIRING_REQUEST action and there you call
device.setPin(PIN_BYTES);
device.setPinConfirmation(true);
You can see the complete example & post here: How to pair Bluetooth device programmatically Android
SECOND OPTION: What if you call device.setPin(PIN_BYTES) and device.setPinConfirmation(true) first, and then device.createBond()? Eg:
if(connConfig!=null && connConfig.bluetooth!=null){
device.setPin(connConfig.bluetooth.pass);
device.setPairingConfirmation(true);
device.createBond();
}
And then you forget about listening the BluetoothDevice.ACTION_PAIRING_REQUEST action on your BroadcastReceiver and only pay attention to BluetoothDevice.ACTION_BOND_STATE_CHANGED events? Android + Pair devices via bluetooth programmatically
See in the first answer the code. As far as I get it, that dude isn't using any PIN for pairing, so I also need to use the setPin method.
Which of both you find better?
Am I missing something? BTW: I'm not using reflection because Im not targeting older platforms.
So I've research this subject and I didn't get much information, even though I did read a few threads here like Check SIM PIN with SDK Android and Ask user for SIM Card PIN but neither of them really answer the question - the app I'm trying to write is based on USSD and will run on devices with SIMCards.
I also read the thread Use system PIN dialog in Android application, but I'm not trying to overwrite the OS, I'm just trying to write a simple app that can check an entered String against the SIMCard PIN. On this last thread, a user (a.ch.) said that trying to access or even check the SIMCard PIN number is not allowed by the OS (at least without modifications, I suppose). Can you guys confirm or give me a link that mentions this prohibition?
Also, is it possible to, using only USSD, check the PIN? For example, I only know the MMI code to change PIN:
**04*Current PIN * New PIN * New PIN#.
And this actually returns fail or sucess messages, and if I make Current PIN = New PIN, I can actually identify if the PIN I introduced is correct or not, but I see this on a text message. Does the smartphone return this info in a attribute that I can check? How can I access the return code?
I need all the help I can get :)
I want to require entering a PIN when connecting two Android devices to ensure they are talking to each other.
There is a sample project in the Android SDK 17 called "WiFiDirectDemo". It establishes a Wi-Fi Direct connection but it lacks the PIN authentication.
I don't know how to specify WiFiP2pConfig.wps.setup in the class DeviceDetailFragment around line 71. Do I specify the action on the current device (I want to show a auto-generated four-digit PIN), i. e. config.wps.setup = WPSInfo.DISPLAY, or should I specify the action on the peer, i. e. config.wps.setup = WPSInfo.KEYPAD?
How can I detect the connection request on the other peer? I guess there is something I can listen to, e. g. in the BroadcastReceiver, but I cannot find it.
I'm an experienced iOS developer but Android is very new to me, maybe the the answer is obvious to you. Thanks for your help.
you need to specify the action on the device which sends the connect request ... and for pin i think this should work
p2pconfig.wps.setup = WpsInfo.DISPLAY;
p2pconfig.wps.pin = "0000";
On connect, PIN is displayed on local device and requests user to enter same PIN on remote device; once PIN is entered, connect completes successfully.
Check this https://groups.google.com/forum/#!topic/android-platform/hN5WfXRzXpI read the 5th post
p2pconfig.wps.setup=WpsInfo.KEYPAD(don't specify p2pconfig.wps.pin) works fine for me.It displays a dynamically generated key on the device from which we are trying to connect to be entered on the other device.
In this case you don't need to listen on any b'cast receiver to detect the connection.The wpa_supplicant takes care of it and prompts you to enter the pin.For a clear picture, look at the logcat(I use adb from ADT(sdk+eclipse) to see the logcat) of wpa_supplicant while connection is in progress.
These days , i implement a blue tooth paring function on android platform,say in details:
The master device is Google TV(3.2), the slave device is blue tooth keyboard,both are HID device.
The Keyboard can be easily put in to discoverable status, When the TV bootup first time ,In TV setup guide, i need to let the tv paring with keyboard,
As you know, in generall, enable bluetooth on TV side we need get the user permition, then can connect.Then my question happend is that i don't want to pop up a dialog to get permition, is there some body can tell me how to do ?
Thanks!
I found a simple way - in the Android code tree, in external\bluetooth\bluez\src\device.c:
/* If our IO capability is NoInputNoOutput use medium security
* level (i.e. don't require MITM protection) else use high
* security level */
if (capability == 0x03)
sec_level = BT_IO_SEC_MEDIUM;
else
sec_level = BT_IO_SEC_HIGH;
//david10000 add
if (device_is_david10000_BTDEV(device)) {
sec_level = BT_IO_SEC_MEDIUM;
}
This means we can low level the security to let your special device connect automatically.
Another way is you can program it to auto fill the passkey/pin etc. when you get the Android pair request.
If you have this problem, try this, it works for me.
I have an app where I am programmatically controlling Bluetooth pairing and unpairing. I can pair before connection and unpair afterwards. The reason I need to do this is specific to my application and not in the scope of my question.
Basically what I am doing is:
Get a reference ib to IBluetooth object as described in this answer
Register a BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
Call ib.createBond(address)
Wait for BroadcastReceiver to trigger
Convert user pin into bytes with convertPinToBytes()
Call ib.setPin(address, pinBytes) from within BroadcastReceiver
Anyways, this approach works great, except for the fact that when I do the pairing, I get a notification in the Status bar requesting that the user enter a PIN to complete the pairing. But this is in fact unnecessary, because by the time the user sees this, my app has already used setPin(). I'd really like for that notification to either a) not appear at all, or b) be dismissed automatically somehow.
I realize this may not even be possible, but I thought I would ask in case someone has a creative idea.
Try setting the confirmation first in the PAIRING_REQUEST
BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput").invoke(device);
This worked for me between two Android devices using RFCOMM but I'm not entering any PINs
Since Android API 19 Google switched these Methods to public Methods, so there is no need for Reflection any more. :)
Do this in the PAIRING_REQUEST notification event:
BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(paramBluetoothDevice, null)).booleanValue();
But you gotta tell me how did you pair your remote device without the user to enter Passkey/PIN? off course, you know the PIN for the remote device which is trying to pair to your device but how did you provide that PIN to the remote device.