Reputation: 1
I wanted to get Bluetooth devices using UWP. I really want to use Devicewatcher
as I should be continued looking for new devices.
The result I get in Added
event of the DeviceWatcher
is DeviceInformation
and does not have all the properties I need in my app, hence I should be calling something to get the BluetoothDevice
object. The problem is when I call the following methods with the Device.ID
that I got from DeviceWatcher
have different errors:
BluetoothDevice.fromBluetoothAddressAsync()
with device ID the done
method is never called.BluetoothDevice.fromIdAsync()
raises an Element not Found error.As I read in another post, I also tried getting the devices from DeviceInformation.findAllAsync()
all calling both the above methods but same result. (winjs-and-bluetooth-connection-error)
Can Anyone suggest what the issue is or another approach? My Codes are as below.
ondeviceadd = function (args) {
//my code here
};
deviceWatch = Windows.Devices.Enumeration.DeviceInformation.createWatcher(selector, null);
deviceWatch.addEventListener("added", ondeviceadd);
deviceWatch.addEventListener("removed", ondeviceRemove);
deviceWatch.addEventListener("updated", ondeviceUpdate);
deviceWatch.addEventListener("stopped", onStopped);
deviceWatch.addEventListener("enumerationcompleted", ondeviceComplete);
deviceWatch.start();
var selector = Windows.Devices.Bluetooth.BluetoothDevice.getDeviceSelector();
Windows.Devices.Enumeration.DeviceInformation.findAllAsync(selector, null).done(function (data) {
for (var i = 0; i < data.length; i++) {
//My code here
}
});
Windows.Devices.Bluetooth.BluetoothDevice.fromBluetoothAddressAsync(devid).done(
function(devinfo) {
res = devinfo;
})
Windows.Devices.Bluetooth.BluetoothDevice.fromIdAsync(data.id).done(function(result){
//my code here
});
Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService.fromIdAsync(devId).done(function (result) {
//my code here
})
Upvotes: 0
Views: 1279
Reputation: 15758
BluetoothDevice.FromIdAsync | fromIdAsync method is the right method to get the BluetoothDevice object with the given Device Id. And in DeviceWatcher.Added | added event, we can get the DeviceInformation object which contains DeviceInformation.Id | id property but no Bluetooth Address info. So BluetoothDevice.FromBluetoothAddressAsync | fromBluetoothAddressAsync method can't be used here.
However while using BluetoothDevice.fromIdAsync
method, we need note that this method needs bluetooth capability. See Requirements → Capabilities and also Device capabilities. So to use this method, we need add Bluetooth Capability in Package.appxmanifest like:
And then when we first call this method, the system will request user to give permission to access the Bluetooth device. If the user chooses Yes, this method will return a BluetoothDevice
object represents the device. But if the use chooses No then this method will return null
.
Upvotes: 0