Automatic bluetooth connection to a certain device - bluetooth

I am working on an application (windows phone 8.1), which is the following: if my phone is already connected to a bluetooth device then it should connect in parralel to another device which is already paired("cc2541"). I would appreciate a little help with the coding.
My problem is i don't know how to connect to a device automatically. I only found code samples that are only scans and lists devices and it only connects when I touch the screen ( approve the connectivity ).

You can get the RSSI value (signal strength) for each device, then you can implement a dispatcherTimer, where you start the scanning as the timer starts and then connect to the closest device on a timeout.
This is a basic example of automatic connection establishment.

Related

Establish a connection between smartphone and PC via Bluetooth automatically

I'm trying to establish a connection between my PC running Ubuntu and my iPhone via Bluetooth automatically when it becomes available, after being manually paired beforehand. I've seen this to be possible with certain peripherals, mainly audio. For example, my phone will automatically connect to a Bluetooth speaker when it is turned on and Bluetooth is active on my phone; another example is my phone automatically connects to my car's radio system via Bluetooth when I turn the car on.
I'm not able to connect my phone to my PC without first initiating the connection from the smartphone's Bluetooth menu. I'm thinking that I could possibly write an application for the PC to attempt to connect to the device every few minutes or something, but it seems that the phone needs to be the device to initiate the connection.
The only information that I need for what I'm trying to do ultimately is that the devices can pair successfully. Essentially I'm trying to build a sort of proximity trigger between my phone and my PC without using Wi-Fi and GPS - I can't use these for some specific reasons.
Is there any way to make this happen?
Yes this should be doable as long as you use the Background Processing feature for iOS apps. In the example I'll give below, we'll have the PC be the peripheral and the phone be the central, but you can really have it working either way. You will need to do the following:-
First initial connection needs to be performed in the foreground (this is due to iOS's background limitations).
On the iOS side, you need an application that acts as a central that scans and connects to the remove device (check this example as a starting point).
Upon connection, you need to bond with the PC. Bonding is important as it will prevent you from having to do the pairing again in the future. However, pairing/bonding is managed by the iPhone's OS so you cannot write it in your application, so the workaround is to have an encrypted characteristic on the PC side that will force the iPhone to bond (this is covered later).
On the PC side, you need to have a BlueZ script that acts as a peripheral that is always advertising. You can do this using bluetoothctl (check the examples here and here).
Before you start advertising, you need to have a GATT server on the PC side (to do this, check this example).
When registering characteristics, ensure that one of them has the encrypt-read property (you can find a full list of the properties here).
Now when you attempt to read this characteristic from the iOS side, the two devices should bond (make sure that your PC is bondable which you can do this via these commands).
Once the devices are paired, your iOS app needs to be working in the background constantly scanning and attempting to connect to the same peripheral (have a look at this and this example).
You can find more useful information at the links below:-
Getting started with Bluetooth Low Energy
The Ultimate Guide to CoreBluetooth Development
How to manage Bluetooth devices on Linux using bluetoothctl

How to p2p_connect to device with WiFi Direct without MAC Address? (Raspberry Pi and Android)

I have an Android tablet and A Raspberry Pi and I want to established a connection between them automatically when the tablet sends a request to the Pi.
I followed an Android application example here and start discovering any nearby devices. (https://www.youtube.com/watch?v=qnY97iBxp30)
At the same time i run sudo wpa_cli and p2p_find 20. The Android application detects the Pi, and I try to establish connection with the Pi which will display
<3>P2P-GO-NEG-REQUEST TABLET_MAC_ADDRESS dev_passwd_id=4
Normally I would just p2p_connect TABLET_MAC_ADDRESS pbc to successfully connect them together but I find it inefficient if I were to swap to another mobile device.
Are there any other ways to connect the tablet without writing the tablet mac address? For example connecting to that specific device ssid when they send a P2P-GO-NEG-REQUEST to the pi?
TL;DR Nope.
If we look at the OSI ISO 7 layer model for network communication we can see that the Media Access Control (MAC) address is vital for identifying which device is which within a wifi network.
You could try setting up a bluetooth connection or a token-ring, but I suspect that would be more effort than you are looking for.
With IPv6 your devices could use neighbour discovery to automate past the MAC entry to the Internet Protocol, and its possible to connect between devices using their link-local address (fe80::some:thing)
Wifi carries packets of data, that have addresses. By analogy, if I tell you which town I live in, but don't write my building address on the packet, you are going to have a hard time delivering it.

Windows 10 IoT and Adafruit M0 BLE comminication [duplicate]

I got an issue with reconnection to my BLE device.
Senario that works
- BLE Device is not paired
- Pi boots and start my App, find's the BLE device, pair and connect, receiving data
- Boot Pi and start my App, it will not pair as its already paired, it connects fine and receiving data.
Senario that does not work
- BLE Device is not pair or pair, does not matter
- If my BLE device disconnect due to power loss or out of range and gets powered up or back in range the Pi will reconnect and throw:
'System.Exception' in mscorlib.ni.dll
There is no user session key for the specified logon session. (Exception from HRESULT: 0x80070572)
If I try to reboot the Pi, the same message is thrown when I try to execute: await _readCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
The only thing that works is if I unpair the device every time it disconnect.
This is the same issue that this guy has: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b98d77f2-bf5e-45fc-9495-1c444b54450e/uwpreconnecting-to-a-ble-csac-device-causes-exception?forum=wpdevelop
Maybe there is anyone here that has seen the same issue and solved it ?
Thanks
The reason for this particular error you are receiving is almost certainly that the BLE device itself is not storing the bonding information that is negotiated during the pairing process. For an existing BLE pairing to be re-used, the device must retain the bonding with its partner for future communication sessions.
Developers can sometimes work around this device shortcoming in their (non-Windows) PC or phone application by having the PC or Mobile app delete any previous pairings with the device already retained, and negotiate a new pairing each time it sees the Bluetooth device. This is possible because many Bluetooth devices have a static PIN, require no interaction for pairing, and remain in open pairing mode. So the app creates a new device pairing upon each communication. The Mac BLE API requires no user interaction for pairing and un-pairing as well.
But this solution is unworkable under Windows BLE API, because unlike regular Bluetooth, the Bluetooth Low Energy API requires user interaction during both the pairing, and un-pairing "ceremonies". The user must agree to every pairing, and must agree to every un-pairing. This makes this workaround solution a non-starter from a UX standpoint on Windows. My guess is the BLE device manufacturer isn't storing the bonding.
We ran into such a situation ourselves developing our own BLE driver to speak with our own BLE board; we had to re-do the firmware running our BLE device because while it would work under Mac OS and Android with the method described above, the BLE API available on the Microsoft UWP requires user interaction during the pairing ceremony. This seems the right way to do things, anyway.

Windows 10 IoT Bluetooth LE

I got an issue with reconnection to my BLE device.
Senario that works
- BLE Device is not paired
- Pi boots and start my App, find's the BLE device, pair and connect, receiving data
- Boot Pi and start my App, it will not pair as its already paired, it connects fine and receiving data.
Senario that does not work
- BLE Device is not pair or pair, does not matter
- If my BLE device disconnect due to power loss or out of range and gets powered up or back in range the Pi will reconnect and throw:
'System.Exception' in mscorlib.ni.dll
There is no user session key for the specified logon session. (Exception from HRESULT: 0x80070572)
If I try to reboot the Pi, the same message is thrown when I try to execute: await _readCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
The only thing that works is if I unpair the device every time it disconnect.
This is the same issue that this guy has: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b98d77f2-bf5e-45fc-9495-1c444b54450e/uwpreconnecting-to-a-ble-csac-device-causes-exception?forum=wpdevelop
Maybe there is anyone here that has seen the same issue and solved it ?
Thanks
The reason for this particular error you are receiving is almost certainly that the BLE device itself is not storing the bonding information that is negotiated during the pairing process. For an existing BLE pairing to be re-used, the device must retain the bonding with its partner for future communication sessions.
Developers can sometimes work around this device shortcoming in their (non-Windows) PC or phone application by having the PC or Mobile app delete any previous pairings with the device already retained, and negotiate a new pairing each time it sees the Bluetooth device. This is possible because many Bluetooth devices have a static PIN, require no interaction for pairing, and remain in open pairing mode. So the app creates a new device pairing upon each communication. The Mac BLE API requires no user interaction for pairing and un-pairing as well.
But this solution is unworkable under Windows BLE API, because unlike regular Bluetooth, the Bluetooth Low Energy API requires user interaction during both the pairing, and un-pairing "ceremonies". The user must agree to every pairing, and must agree to every un-pairing. This makes this workaround solution a non-starter from a UX standpoint on Windows. My guess is the BLE device manufacturer isn't storing the bonding.
We ran into such a situation ourselves developing our own BLE driver to speak with our own BLE board; we had to re-do the firmware running our BLE device because while it would work under Mac OS and Android with the method described above, the BLE API available on the Microsoft UWP requires user interaction during the pairing ceremony. This seems the right way to do things, anyway.

Bluetooth connection close itself right after start

my problem is the connection with a Bluetooth device. I have a Netbook with an integrated bluetooth device. I want to connect my netbook with an OBD-II interface and write a software to read out the stats of my car. To test the connection, I started by adding the OBD device via the bluetooth menu. The manager connects to the interface and it wants me to wait while the configurations are taking place. This state lasts "forever". After 60-Minutes of waiting, I closed the window. However, the device is shown in the list of available devices. Because I'm not able to switch the button to initiate the connection, I thought about using minicom (the OBD-II interface uses SPP for communicating).
As far as I did understand, I need an initiated connection with a bluetooth device and with it a virtual serial port to connect to with minicom. After reading some postings, I found out that the hcitool should be able to create the bluetooth session. With this I was able to connect to the device and the "connected" button turned on. Unfortunately it switches back after 2 secs.
hcitool scan is able to find the device
hcitool cc 00:11:22:33:44:55 is able to connect for the already mentioned 2 secs
hcitool con list the connection if it's called within 2 sec after initiating
I also tried to connect via a Windows PC using putty. Windows connected with the device without a problem and after that, I was able to send requests and get the appropriate answers. So it seems like the interface is working correctly.
I'm using Fedora 17 with gnome3. If you need any output or more information, please let me know.
Thanks in advance

Resources