Android Bluetooth - How to initiate Pairing - android

I want to initiate a pairing request (not connection at this point of time) to a non android device. This can be found working on Settings Application > Network > Bluetooth Settings > Click on a device after scan. A pop up appears on both the devices with a 6 digit pin.
As per Android's documentation this should have popped up in my application as well, if I do something like this
device.createRfcommSocketToServiceRecord(MY_UUID);
As per Android's API documentation
"Once a connection is made with a
remote device for the first time, a
pairing request is automatically
presented to the user."
And it is also mentioned that
"The current Android Bluetooth API's
require devices to be paired before an
RFCOMM connection can be established.
(Pairing is automatically performed
when you initiate an encrypted
connection with the Bluetooth APIs.)"
Even when I used the sample Bluetooth Chat application (only to test if it initiates a pairing on first connection) it didn't worked.
I also tried using a generic UDID like "00001101-0000-1000-8000-00805F9B34FB" but to no avail.
Couldn't find the source code of Settings Application (Android OS 2.1) to figure out myself. The sources available in Android GIT were for Android 2.3
Even if you cannot answer the question, only pointing out to Android Packages Settings App sources for Android 2.1 might do the trick for me.

It works for me with this UUID : "00001101-0000-1000-8000-00805F9B34FB".
The intent for pairing request is launched when you call socket.connect() if I remember correctly.

Related

Why does Android bond even when asked not to bond?

I have developed an application on an on-board device which allows a smartphone to be able to connect in secure BLE.
Everything works fine but I notice that every time I do a "pairing" via an Android smartphone, that the smartphone "bonds" the device even though I specified in the request that I did not want bondage. I don't have this problem on IOS.
Here are the requests that are sent by my on-board device to the smartphone:
Security request
Pairing response
Is it possible that Android does not support "pairing only"? If it is paired, it does a "boding" automatically even if it has received a request asking it not to do a bond?
Thank you
"Non-bondable mode" is mandatory to support in the Bluetooth Core specification. If Android makes a permanent bond, even though you say to not bond in the "Pairing response", it's a bug in Android. Please file a bug report to Android Open Source Project (AOSP).

HM-10 as BLE Listener: setting up a brief connection from it

I need to estabilish a brief communication between a BLE master and a BLE peripherical device.
Until I have to use the mobile phone I have no problems, since I select the bluetooth device from Android, I can connect to it and then I can receive data simply writing them to HM-10.
BUT, this approach is battery expensive, since the BLE scanning is more battery expensive than the BLE advertising (right?). So I want that the mobile becames an advertisor and the HM-10 takes the ROLE of Slave.
BUT, I still don't understand if it is possible to connect to other devices: I tried AT+CONN and it always fails (AT+CONF). Is there any way to send brief data from the HM-10 to the advertisor, even without connecting?
Even my firmware looks slightly different from the readme! For example,AT+LN is present in the documentation but it appears to not exist. Does a better version of the firmware for this device exist, or, are sources available somewhere? I installed the HM-Soft V705 (lastest).
Thanks
UPDATE 1:
#ChrisStratton Ok, I have some intresting news: I was already following your idea (phone advertise sometimes, while HM-10 always scan and tries to connect to the service if it sees it, then HM-10 send some data, phone answer). I managed to make the phone visible from HM-10 (don't ask me how!). I am using the BLE Tool Android app to test everything. Now, the central problem now is that HM-10 won't connect to the android service even if the advertisor is connectable (see attached images).
I am sospicious that the AT+CO (that I discovered is a kind a replacement of AT+LN, but I am not sure) can help, but i don't know how.

How to use android as ANCS content provider?

I'm trying to demo android side service (content provider in ANCS lingo), that communicates with embedded device that supports Apple Notification Service (ANCS) notification consumer. Reason for this is, that the embedded device only supports ANCS. As the ANCS BLE side things are publiced by Apple, this should be possible and I've already added that service + associated characterics to android side gatt server. Embedded device is nordic nRF52 and SW is nordic ANCS demo. Demo works OK with iPhone and apparently with the desktop version of nRF control panel (if added with nordic supplied config and if you have windows machine, which I don't).
However I cannot seem to be able to bond the device with android, that's required before connection. The advertisement is in limited discovery mode (before bonding) so I cannot find it via regular scanning from settings. Also bonding from java code doesn't work. With mBluetoothDevice.createBond() I get ACTION_BOND_STATE_CHANGED callback with state 10 (BluetoothDevice.BOND_NONE) with old state 11(BluetoothDevice.BOND_BONDING). Apparently I would need to exchange keys here, but no idea how to do that.
Also dunno if anyone has done this same exercise before, but any other tips would also be appreciated! Thanks!
Problem was that the phone BLE stack was corrupted or in some invalid state. REboot and removing battery solved the issue with the bonding.

Programmatically connecting to a Bluetooth enabled barcode scanner in Android

I am working to create an Android App that connects to a bluetooth barcode scanner. I've been looking for code examples of how to do this but I can not find any dealing with connecting to a device. I see lots for connecting peer-to-peer with android devices but that doesn't seem to cover android to device.
This is the code I have so far. It fails on the call to connect with a Connection refused.
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = mDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
06-11 15:29:10.113: W/System.err(20018): java.io.IOException: Connection refused
06-11 15:29:10.133: W/System.err(20018): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
06-11 15:29:10.133: W/System.err(20018): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:212)
The device is paired with the android phone and I retrieve it from the phone using this.
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
I did find a fix to this issue, though I am not sure as to why this one works and the other did not.
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = mDevice.**createInsecureRfcommSocketToServiceRecord(uuid);**
socket.connect();
Changing to use the Insecure call allows the connect to work and I can receive data back from socket read calls.
This ID will only work if the barcode scanner offers a standard bluetooth SPP interface. Are you sure this device even uses that UUID?
Go and look for any documentation for connecting this scanner to something - even just a PC. And go through it, even though it's not what you're trying to do right now. You might learn something about the system or get ideas about how it actually works.
For instance, the presence of a special driver install for PC to make it do anything will point to it not being an SPP profile. Alternatively, if there's a PC application for working with the scanner that connects to a "serial port" to work, then it's pretty much definitely an SPP profile.
In the case of it not being an SPP profile, having a working connection to something like a PC at least gives you a chance to sniff the connection and maybe also data transfer, to reverse engineer something for android.
Also, with a baseline check that the hardware works for one specific application, you can rule out hardware fault. (check your android device's bluetooth link while you're at it!)
I had same problem and solved it.
First, change your barcode scanner mode to SSP from HID.
HID(Human Interface Mode) is just for hardware keyboard mode.
Next, turn off your bluetooth hardware from Input device.
Settings > Bluetooth > bluetooth barcode canner setting >
uncheck Input device
Use UUID
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
That's it.
createInsecureRfcommSocketToServiceRecord was a great hint.
I had problems connecting to a Bluegiga WT32-A Bluetooth module.
My old code was based on Bluetooth Chat example (which worked fine with an BTM 222 Bluetooth module) and used createRfcommSocketToServiceRecord, with the result of the connection being refused:
java.io.IOException: Connection refused
Weirdly, trying to connect by createRfcommSocketToServiceRecord again and again, in short intervals, did sometimes work.
Another thing to note: The use of createInsecureRfcommSocketToServiceRecord requires API level 10.
Another possibility - posting as a 2nd answer because it's very different from my last.
You've only given info on something you've planned to do technically so far, not the actual purpose of the app you are trying to write.
If the purpose of your app isn't just an exercise in bluetooth connectivity or just about using a particular barcode reader, and getting the barcode value into the android device is just one step in a bigger purpose - have you considered using the device's camera for that job instead of a barcode reader?
I believe there are libraries that can handle this, or DIYing your own image processing code could be fun (I know there's existing apps that do it already, so it's possible some way or other)

low level bluetooth broadcast

Since bluetooth is a broadcast protocol, is there some way to listen for broadcasts while not discoverable? The idea is to avoid making a device discoverable, and simply listen for broadcasts, check the devices for a particular device name, and then connecting to that device's mac address (followed by handshaking, verification, etc).
Specifically, I have a bluetooth device that uses the AT command set (user manual here), and I want my Android device to autoconnect to it when it gets into range. However, I want a solution that doesn't require my Android to be discoverable all the time, because I expect the Android to not be in range most of the time.
Is there a low-level bluetooth command that I can send from my first device, and listen for using the Android SDK that doesn't require pairing, connecting, or discoverability? Something like and ACL request, perhaps?
(Additionally, my reasons for not making my device discoverable are mainly power, and also slightly user privacy reasons. How significant is the battery drain of device discoverability? Next to nothing? Significant?)
Its called a Bluetooth PAGE command. Not sure how you'd use it from your device though, but it allows a Bluetooth device to (in effect) announce its presence to its configured master. You'd have to look up the Android response to a PAGE to see how well that's supported though.
If the Android is connecting to the device, then the Android never needs to be discoverable. Unless I'm misunderstanding... (The radio on the Android just needs to be powered-up and not "connectable" nor "discoverable")
The Android can just try to connect every so often (sending a PAGE), to see if the remote device has come back into range. Then even the remote device doesn't need to be discoverable, just 'connectable' -- assuming your Android program remembers the remote device's device address.
Or you could have the remote device try to reconnect ot the Android device every so often (presuming the remote device supports making connections). You would need to have the program on the Android be a server/listener, and the Android Bluetooth in 'connectable' mode.

Categories

Resources