Connect to Bluetooth Device / how to set the rfcomm capability - c#

I'm trying to connect to a BlueTooth device
I have paired it and when I search for it I find it :
private async void Grid_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
ListBox1.Items.Clear();
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var device = devices.FirstOrDefault(c => c.Name.Contains("BMMTCA32"));
foreach (var element in device.Properties)
{
var strMessage = element.Key + (element.Value == null ? "" : " = " + element.Value.ToString());
ListBox1.Items.Add(strMessage);
}
}
Here is the output in my ListBox:
System.ItemNameDisplay = BMMTCA32-01
System.Devices.DeviceInstanceId = BTHENUM\{00001101-0000-1000-8000-00805f9b34fb}_LOCALMFG&0048\8&f358302&0&0012F31DECF3_C00000000
System.Devices.Icon = C:\Windows\System32\DDORes.dll,-2001
{51236583-0C4A-4FE8-B81F-166AEC13F510} 123 = C:\Windows\SYSTEM32\DDORes.dll,-3001
System.Devices.InterfaceEnabled = True
System.Devices.IsDefault = False
System.Devices.PhysicalDeviceLocation
But my problem is how to connect to to it?
When I try Googeling for it I get answers like Did you set the rfcomm capability? see http://msdn.microsoft.com/en-us/library/windows/apps/dn263090.aspx for some details.
But when I look at that page I get lost because I don't what to write in the manifest file.
so in short: How do I connect to the device?
PS: It is a Windows Tablet program.

So you want to know what you have to write in the manifest file, as well as how to connect?
Manifest file:
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>
</m2:Device>
</m2:DeviceCapability>
You can keep Id at "any".
Function type could either be "name:serialPort" or the serviceId specified in the example.
Connecting:
StreamSocket _socket;
RfcommDeviceService service = await RfcommDeviceService.FromIdAsync(device.id);
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
Should be able to do the trick.

Related

How can I connect to a Bluetooth service on Gear S3?

I am writing a web app for the Gear S3 and a companion Win-App for UWP in C#.
What I want it to do: S3 acting as a BT server, sending data, and the Win-App as a client, which is supposed to receive the data.
In Tizen I used the registerRfcommServiceByUUID() function as below:
function publishTransferService() {
if(remoteDevice.isConnented){
console.log("Connection Status: " + remoteDevice.isConnected);
var TRANS_SERVICE_UUID = '5BCE9431-6C75-32AB-AFE0-2EC108A30860';
adapter.registerRFCOMMServiceByUUID(TRANS_SERVICE_UUID, 'My Service', ServiceSuccessCb,
function(e) {
console.log( "Could not register service record, Error: " + e.message);
});
}else{
console.error("Connection Status: " + remoteDevice.isConnected);
}
In C# i try to connect to the service as follows:
//to ulong converted MAC-Address of the Gear
ulong gearBtAddress = 145914022804881;
//create device-instance of the gear
BluetoothDevice gearDevice = await BluetoothDevice.FromBluetoothAddressAsync(gearBtAddress);
//list the services found on the gear and connect to the one with
//the same uuid as in tizen
var gearServices = await gearDevice.GetRfcommServicesAsync();
foreach (var service in gearServices.Services)
{
if(service.ServiceId == RfcommServiceId.FromUuid(Guid.Parse("5BCE9431-6C75-32AB-AFE0-2EC108A30860")))
{
_service = await RfcommDeviceService.FromIdAsync(service.ToString());
}
}
The Problem I am facing is, that the C# won't find the wanted Uuid, although it finds several other uuids. I don't see where it gets lost?
Thanks in advance.
I solved it myself, the problem was, that the UUID was uncached. Adding the following was all to do:
var cacheMode = BluetoothCacheMode.Uncached;
var gearServices = await gearDevice.GetRfcommServicesAsync(cacheMode);

UWP/C# - Issue with AQS and USB Devices

I have an issue with a UWP app that I am trying to write. I am connecting to a custom embedded USB Bulk device that I have programmed (it is actually an out of the box example from Cypress Semiconductor). I am using the WinUSB.sys driver using the embedded MS OS string in the device to allow the device to be used with out having to write a custom INF file to call the WinUSB.sys driver.
In my code, I am using the UsbDevice.GetDeviceSelector method to return an AQS that can then be passed into DeviceInformation.FindAllAsync to begin communicating with the device in my app. I have confirmed that the device shows up in the device manager without any issues, and I have checked in the registry to ensure that it has an Interface GUID. I have a screenshot from USBViewer to show the configuration of the device. This method for finding and connecting with USB devices is from this MSDN example found here.
When I use the UsbDevice.GetDeviceSelector method, it returns a GUID that is not associated with this device. The GUID that it returns is actually associated with Lumia Phones (DEE824EF-729B-4A0E-9C14-B7117D33A817). Because of this, it does not find my device connected to the system.
To troubleshoot, I have both called the DeviceInformation.FindAllAsync with out any arguments to see if my device is listed, and it does find the device (amongst over 1000 other devices that have been connected ever to my machine). I then wrote a custom AQS string without the help of the GetDeviceSelector method, starting with just the GUID. Doing this returned 27 devices, but when I tried to add the VID and PID to this AQS string, nothing returned.
I have also made sure that the device that I want to use is listed in the app manifest by its appropriate VID and PID as this is required for a device with a Custom Class of 0xFF. I have used the Custom USB UWP device example and it can find the device, though it uses a completely different method with a device picker, which I will go to if needed, but this is not my desire as it makes that part of the app not as clean of a solution.
I have posted this question over in the MSDN forums here with more information, but I have not gotten a lot of engagement there. Any help would be appreciated. I know that I must be missing something simple.
Adam
private async void button_Click(object sender, RoutedEventArgs e)
{
//UInt32 vid = 0x04B4;
//UInt32 pid = 0x00F0;
UInt32 vid = uint.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);
UInt32 pid = UInt32.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
Guid winusbInterfaceGuid = new Guid("a5dcbf10-6530-11d2-901f-00c04fb951ed");
//string aqs = UsbDevice.GetDeviceSelector(vid, pid);
string aqs = UsbDevice.GetDeviceSelector(winusbInterfaceGuid);
var myDevices = await DeviceInformation.FindAllAsync(aqs, null);
//var myDevices = await DeviceInformation.FindAllAsync();
var myDevicesCount = myDevices.Count;
if (myDevicesCount >= 1)
{
textBlock2.Text = "Device Found";
} else
{
textBlock2.Text = "Searching";
await Task.Delay(1000);
textBlock2.Text = "looking for device";
}
}
just dropped you a mail asking about progress (I think, had to guess your mail address), but now it seems I found a solution myself. Please see my answer on UWP app cannot find/connect to USB device
In short, you have to create an inf for installing the winusb driver. I have no clue why, but that did the trick for me (and someone else, see Cannot create UsbDevice from DeviceInformation.Id)
The Guid DEE824EF-729B-4A0E-9C14-B7117D33A817 is actually the standard WinUSB Guid. I don't think it has anything to do with Lumia Phones. I don't know why it is not documented anywhere. I think that the Guid a5dcbf10-6530-11d2-901f-00c04fb951ed you specified is actually a red herring. I mistakenly used that as well, but it just led me down the garden path. It shows up USB interfaces, but I can't connect to them.
You might want to try this class https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Usb.Net.UWP/UWPUsbDevice.cs .
Here is how it gets the device:
public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(uint? vendorId, uint? productId)
{
var aqsFilter = "System.Devices.InterfaceClassGuid:=\"{DEE824EF-729B-4A0E-9C14-B7117D33A817}\" AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True AND " + $" System.DeviceInterface.WinUsb.UsbVendorId:={vendorId.Value} AND System.DeviceInterface.WinUsb.UsbProductId:={productId.Value}";
var deviceInformationCollection = await wde.DeviceInformation.FindAllAsync(aqsFilter).AsTask();
//TODO: return the vid/pid if we can get it from the properties. Also read/write buffer size
var deviceIds = deviceInformationCollection.Select(d => new DeviceDefinition { DeviceId = d.Id, DeviceType = DeviceType.Usb }).ToList();
return deviceIds;
}
This sample connects to a device and I think you'll be able to connect to the device in the same way:
private static async Task InitializeTrezor()
{
//Register the factory for creating Usb devices. This only needs to be done once.
UWPUsbDeviceFactory.Register();
//Register the factory for creating Usb devices. This only needs to be done once.
UWPHidDeviceFactory.Register();
//Note: other custom device types could be added here
//Define the types of devices to search for. This particular device can be connected to via USB, or Hid
var deviceDefinitions = new List<DeviceDefinition>
{
new DeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x" },
new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, ReadBufferSize=64, WriteBufferSize=64, Label="Trezor One Firmware 1.7.x" },
new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, ReadBufferSize=64, WriteBufferSize=64, Label="Model T" }
};
//Get the first available device and connect to it
var devices = await DeviceManager.Current.GetDevices(deviceDefinitions);
var trezorDevice = devices.FirstOrDefault();
await trezorDevice.InitializeAsync();
//Create a buffer with 3 bytes (initialize)
var buffer = new byte[64];
buffer[0] = 0x3f;
buffer[1] = 0x23;
buffer[2] = 0x23;
//Write the data to the device
await trezorDevice.WriteAsync(buffer);
//Read the response
var readBuffer = await trezorDevice.ReadAsync();
}
If you connect to the device in this way, you'll get Windows classic, and Android support for free with Device.Net (https://github.com/MelbourneDeveloper/Device.Net)
With Device.net's DeviceManager.Current.GetDevices(deviceDefinitions) using .NET 5 I can't find any device connected to my win10, which can be easily selected by ManagementObjectSearcher:
public List<ManagementBaseObject> GetLogicalDevices()
{
List<ManagementBaseObject> devices = new List<ManagementBaseObject>();
ManagementObjectCollection collection;
ManagementObjectSearcher seacher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM CIM_LogicalDevice");
collection = seacher.Get();
foreach (var device in collection)
{
devices.Add(device);
}
return devices;
}

Closed Captions resolving error System.UnauthorizedAccessException

So, I'm trying to implement closed captions support to my UWP video player (using MediaElement), I've followed this example to do so.
I'm getting an error when resolving it called "Error resolving track due to error NetworkError System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
I do it like this: I open a file using filepicker and then get the SRT of the video that was picked. After that I show it. Unfortunately, nothing appears.
Here is my OpenButton function:
private async void BtnOpenMedia_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.ViewMode = PickerViewMode.Thumbnail;
filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
filePicker.FileTypeFilter.Add(".mp4");
filePicker.FileTypeFilter.Add(".wmv");
filePicker.FileTypeFilter.Add(".mpg");
filePicker.FileTypeFilter.Add(".mpeg");
filePicker.FileTypeFilter.Add("*");
StorageFile storageFile = await filePicker.PickSingleFileAsync();
if (storageFile != null && mElement != null)
{
string strSource = Path.GetDirectoryName(storageFile.Path) + #"\" + storageFile.DisplayName + ".srt";
var mediaSource = MediaSource.CreateFromStorageFile(storageFile);
var ttsStream = TimedTextSource.CreateFromUri(new Uri(strSource));
ttsStream.Resolved += TtsStream_Resolved;
mediaSource.ExternalTimedTextSources.Add(ttsStream);
var mediaPlayback = new MediaPlaybackItem(mediaSource);
mElement.SetPlaybackSource(mediaPlayback);
}
}
Here is my resolve function:
private void TtsStream_Resolved(TimedTextSource sender, TimedTextSourceResolveResultEventArgs args)
{
if (args.Error != null)
{
var ignoreAwaitWarning = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var msg = new MessageDialog("Error resolving track " + " due to error " + args.Error.ErrorCode + " " + args.Error.ExtendedError);
await msg.ShowAsync();
});
return;
}
}
P.S: Also, I don't know if this is duplicated or not, that's why I'm adding it in this but I've done my research and found nothing. How to preview frames of MediaElement ? For example like YouTube you can preview thumbnails in the slider, I don't know how to achieve that, thanks!
You used a FileOpenPicker to select the Video file, but use a Path to access the .srt file. The .srt file is also in the Video file's folder. I reproduced your problem here:
The error message is clear, you have no access to this file, this file indicates the .srt file, so the problem is where did you store this .srt file. Just have a test, seems TimedTextSource.CreateFromUri(Uri) | createFromUri(Uri) method does not support to access the files in the local machine, but you can use TimedTextSource.CreateFromStream(IRandomAccessStream) | createFromStream(IRandomAccessStream) method for example like this:
if (storageFile != null && mElement != null)
{
//string strSource = Path.GetDirectoryName(storageFile.Path) + #"\" + storageFile.DisplayName + ".srt";
var fileSource = await KnownFolders.VideosLibrary.GetFileAsync(storageFile.DisplayName + ".srt");
IRandomAccessStream strSource = await fileSource.OpenReadAsync();
var mediaSource = MediaSource.CreateFromStorageFile(storageFile);
//var ttsStream = TimedTextSource.CreateFromUri(new Uri(strSource));
var ttsStream = TimedTextSource.CreateFromStream(strSource);
ttsStream.Resolved += TtsStream_Resolved;
mediaSource.ExternalTimedTextSources.Add(ttsStream);
var mediaPlayback = new MediaPlaybackItem(mediaSource);
mediaPlayback.TimedMetadataTracksChanged += (sender1, args) =>
{
mediaPlayback.TimedMetadataTracks.SetPresentationMode(0, TimedMetadataTrackPresentationMode.PlatformPresented);
};
mElement.SetPlaybackSource(mediaPlayback);
}
When using this code, the .srt file and video file should in the Video lib and the capability "Videos Library" should be enabled in the manifest.
In an UWP app, you can only access the files in known folder like picture lib, music lib and video lib and doc lib or local folder of your app, if your video is not in these folders, you should also handle the exception when access is denied in this scenario.
How to preview frames of MediaElement ? For example like YouTube you can preview thumbnails in the slider.
For this question, I can't find any ready-made sample for you, but I think the scenario 4 of official Media editing sample can be a direction, it shows a overlay layer on MediaElement, maybe you can set the "baseVideoFile" and the "overlayVideoFile" with the same source. The problem is when and where to show this overlay layer, it's related to the transport control of MediaElement. This is for now just a mind, you can have a try.

Get RSSI from an iBeacon estimote in c#

I'm trying to locate the distance from my computer and an iBeacon using bluetooth connection and getting the rssi. It's my first time using bluetooth in my apps and I'm something lost.
I downloaded the "32 feet" library (http://32feet.codeplex.com/) to use bluetooth functions, but I can't do this to work... This is the code I have:
BluetoothClient b = new BluetoothClient();
BluetoothDeviceInfo[] devices = b.DiscoverDevices();
BluetoothDeviceInfo info = devices.ElementAt(0);
// the first element it's the estimote
System.Windows.Forms.MessageBox.Show("Device name: " + info.DeviceName+"\n");
if (info.Connected)
{
System.Windows.Forms.MessageBox.Show("Connected\n");
}
else
{
System.Windows.Forms.MessageBox.Show("Not connected\n");
}
if (info.Authenticated)
{
System.Windows.Forms.MessageBox.Show("Authenticated\n");
}
else
{
System.Windows.Forms.MessageBox.Show("Not Authenticated\n");
}
System.Windows.Forms.MessageBox.Show("RSSI: " + info.Rssi + "\n");
The output is: Device name: estimote. Not connected. Authenticated. RSSI: -2147483648 (the minimum int?)
Thanks for your help.
This is Wojtek Borowicz, I'm a community evangelist at Estimote.
Unfortunately, RSSI is currently still not available for most of the Windows Bluetooth stacks.

Xamarin C# for Android : Searching for an App and path

GOAL : I need to find if an app is installed on a device AND find it's path..
I see using PackageManager, you can do this in general but I would like to refine it.
I know if you use com.google.chrome you can find chrome explorer installed,
but this fails when you look simply for Chrome.
On some devices Chrome (and other apps, like Opera, Mini,etc) is not installed as com.google.chrome.
So how would one find an app without the com.google and just use Chrome as the search criteria ?
Simple, just combine PackageManager and ApplicationInfo, and then check if app name contain your search string. Here is some example code:
var searchQuery = "chrome";
var flag = PackageInfoFlags.Activities;
var apps = PackageManager.GetInstalledApplications(flag);
foreach(var app in apps)
{
try
{
var appInfo = PackageManager.GetApplicationInfo(app.PackageName, 0);
var appLabel = PackageManager.GetApplicationLabel(appInfo);
if (appLabel.ToLower().Contains(searchQuery.ToLower()))
{
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Found it!");
builder.SetMessage(appLabel + " installed at: " + app.SourceDir);
builder.Show();
}
}
catch (PackageManager.NameNotFoundException e) { continue; }
}

Categories