I'm trying to connect a Windows Phone 8 phone with a generic bluetooth headset using some information stored on a NFC tag. As for bluetooth part, it should be possible to find paired devices, which are active, using
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var peers = await PeerFinder.FindAllPeersAsync();
and call
await socket.ConnectAsync(peer.HostName,
serviceName);
to connect to the particural device. However, I am unable to get the serviceName. According to documentation, the device should return advertised service in PeerInformation.ServiceName, but I am only getting blank string.
Is there some workaround for how to get nams of supported services (bluetooth profiles)? I specifically need to connect the bt device using A2DP profile, but I am unable to get (or even find on the internet) the correct service name string.
ConnectAsync opens a socket for the calling program to send/receive data over. That's not what you want, instead you want the OS to connect to the Headset service. In Win32 that's BluetoothSetServiceState but I don't know an equivalent 'modern' API.
Also in Win32 one could use BluetoothAuthenticateDeviceEx which would cause pairing, and *probably* connect the headset services -- and with full Out-Of-Band authentication if supplied by the NFC channel. Again I don't know of an equivalent.
I suppose there's a possibility that doing a ConnectAsync to an arbitrary endpoint (e.g. "15") will cause pairing to start which will then complete and enable the Headset service... The ConnectAsync call itself will fail but it is its side-effect we're interested in. May be worth a try.
Presumably you're getting the headset device's device address from the NFC comms? If so, you can create the HostName object to use in the ConnectAsync as Peter describes: http://peterfoot.net/PersistBluetoothAddressesOnWindowsPhone8.aspx
Related
I need to develop an application which communicates with a device via bluetooth low energy. Once the application is connected to the device via bluetooth it receives and sends data by using a gatt service.
The application needs to run on a Windows 10 environment. So far I was able to develop and try the application by using the following UWP classes:
DeviceWatcher
BluetoothLEDevice
Once the device has been discovered and paired the communication starts. The main problem I have is that I am not able to control the connection/disconnection with the device, that is the connection starts automatically once the device has been properly and previously paired, but I haven't found any connect()/disconnect() method so far.
Is there a way to control the connection with a specific bluetooth LE device? Are there other APIs which allow to use the bluetooth without using the UWP framework and which offer more control over the bluetooth?
Once the device is paired, whenever it turns on close to the Windows 10 machine, it will try to connect. This is defined behavior in Bluetooth, as the peripheral will always send out a connection request when it is turned on.
There is a DeviceWatcher background task that you can register for to trigger your app when your Bluetooth device connects. You can find some sample code here.
Is there a way to control the connection with a specific bluetooth LE device?
Yes. To initiate a connection: when you create a BluetoothLEDevice via FromBluetoothAddressAsync or FromIdAsync the system will try to initiate a connection to that peripheral, if it does not already hold a connection.
// Connects to a Bluetooth device, given some string deviceId
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceId);
To dispose of a connection, call the close method on BluetoothLEDevice. If your app is the only entity with a handle to the peripheral, this will cause the system to disconnect. However, if another app or system service has a handle to the peripheral, the connection will not be closed.
// Will disconnect from the BTLE device, if you hold the only handle
bleDevice.close()
These are taken from the BluetoothLEDevice documentation here.
Are there other APIs?
There are not any other APIs built in to Windows 10 that offer more control over Bluetooth. The UWP APIs offer the most control that Windows 10 currently provides. You could use an alternate Bluetooth stack, but these would have to be installed separately and likely break other Bluetooth behavior on Windows 10.
I have a windows 10 UWP app that is able to pair with a bluetooth LE device programmatically. Once the pairing is successful, a connection to the device is also established.
If at some point, the device gets disconnected, I am not able to read any of the GattCharacteristics from the LE device. I'm able to check if the connection is present or not but I'm unable to re-establish the connection.
DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync("deviceId", "additionalProperties", "DeviceInformationKind");
if(deviceInfo.ConnectionStatus != BluetoothConnectionStatus.Connected) { // re-establish the connection }
Thanks.
The Problem
The Bluetooth LE device is not storing the bonding information created during the pairing process. Bonding information allows two previously paired devices to initiate new connections if they have become disconnected.
The Windows 10 Solution
Using the in-app pairing APIs, you can programmatically tell the system to pair with the Bluetooth LE device (it sounds like you are already doing this). To work around the bonding problem described above, the DevicePairingProtectionLevel must be set to None. So your in-app pairing code could look like:
var result = await someDevice.Pairing.PairAsync(DevicePairingProtectionLevel.None);
Setting the DevicePairingProtectionLevel to None tells the system to ignore bonding information and just look for a matching device.
The Peripheral Solution
Alternatively, if you have access to the peripheral's firmware, you can set it to remember the bonding information. Then your current pairing calls on Windows 10 should work.
The USB modem has default software with the driver for PC to make calls and to receive calls which work fine. I want to program it myself if possible with Telephony API so that I have more control and I can process the voice data.
Moreover, there is sample application in codeproject(http://www.codeproject.com/Articles/10994/TAPI-3-0-Application-development-using-C-NET) I tried too. However, the code gave an error saying a dll is too old.
Changing this line,
bcc = ln.CreateCall(textBox1.Text,TapiConstants.LINEADDRESSTYPE_PHONENUMBER|TapiConstants.LINEADDRESSTYPE_IPADDRESS,TapiConstants.TAPIMEDIATYPE_DATAMODEM|TapiConstants.TAPIMEDIATYPE_AUDIO);
to this,
bcc = ln.CreateCall(textBox1.Text, TapiConstants.LINEADDRESSTYPE_PHONENUMBER, TapiConstants.TAPIMEDIATYPE_AUDIO);
I had been able to compile and run the application. Whenever, I select the modem line and press call providing a number it gets disconnected just after dialing. Hence, I have misgivings whether this TAPI application had a different purpose and may not work with USB GSM Modems.
Does anybody know any online tutorials about receiving data between a desktop client (in c# mostly) and an iPhone with bluetooth?
How would a C# desktop client connect over bluetooth... by reading the COM Ports?
From what I understand the iPhone will not pair with a PC as a bluetooth device, as an iPhone will only connect to approved device types. There may be some way to "fake" this and have the PC show up as an approved device, though I'm not sure there's a BT stack to do this.
An easier solution (if it meets your requirements) may be to try using the WIFI connection, you can open up a TCP socket between the device and PC fairly easily and push data through it at a high rate. You should be able to do this with an ad-hoc connection (so you don't need a router) though I've never tried it. You might want to look into cocoaasyncsocket if you do this.
currently writes an application to connect to the device "BTLink Bluetooth to Serial Adapter"
More information about device: device specification
Have created such a code:
BluetoothAddress btAddress = null;
if (!BluetoothAddress.TryParse(comboBoxDevices.SelectedValue.ToString().Trim(), out btAddress))
throw new Exception(String.Format("Adress: {0} wrong !", comboBoxDevices.SelectedValue.ToString().Trim()));
BluetoothEndPoint endPoint = new BluetoothEndPoint(btAddress, BluetoothService.DialupNetworking);
_bluetoothClient.Connect(endPoint);
I have tested two types of GUIDs:BluetoothService.DialupNetworking, BluetoothService.SerialPort.
I received this message: "No connection could be made because the target machine actively refused it"
What is wrong ?
Best regards,
mykhaylo
I would first try to connect to the target machine using the device's built-in Bluetooth capabilities. Only after this succeeds would I try to connect to it programatically.
To be able to connect to a Bluetooth device you need to know the following:
The Bluetooth profile to use. You've tried both Serial and Dialup, but which of them is actually the desired one? Many devices only support a single profile and will refuse to connect to another device requesting a non supported service.
Authentication information. Some devices require a pre-defined password.
Many devices can only form a connection to a single BT device. Is the target device already connected somewhere else?
The error you are experiencing is most probably related to one of the above and has not to do with the use of the bluetooth library.
Have you checked with 32Feet.net or on their support Forums (the provider of the classes you're using)?
I made a similar setup and program using a standard serial Bluetooth adapter without problems. You need to make sure to use the correct Bluetooth profile, the port must be available and authentication code must be accepted. Just a few suggestions.