Share text using NFC in Windows Phone 8 - c#

I am trying to create an app for learning purpose in which one windows phone 8 user sends the text and other user receives it. And the text is shared through NFC. But the problem is the other user is not able to receive the text.
Here is the code=>
Receiver's Code:
ProximityDevice device;
long subscribedMessageId;
private void receive_Click(object sender, RoutedEventArgs e)
{
device = ProximityDevice.GetDefault();
if (device != null)
{
subscribedMessageId = device.SubscribeForMessage("Windows.SampleMessage", messageReceivedHandler);
}
}
private void messageReceivedHandler(ProximityDevice sender, ProximityMessage message)
{
rtextbox.Text = message.DataAsString;
device.StopSubscribingForMessage(subscribedMessageId);
}
Sender's Code:
ProximityDevice device;
long publishedMessageId;
private void send_Click(object sender, RoutedEventArgs e)
{
device = ProximityDevice.GetDefault();
device.StopPublishingMessage(publishedMessageId);
if (device != null)
{
publishedMessageId = device.PublishMessage("Windows.SampleMessage", textbox1.Text);
textbox1.Text = "";
}
}
Both the codes are present on different page. Code is executed when the user clicks on send or receive button respectively.
I am new to NFC so any help will be appreciated.

Unfortunately, NFC gets complicated. There's quite a bit of plumbing involved to handle peer-to-peer communication seamlessly. It's far too much to put into an answer here on the site, so I will have to resort to links.
You can check out this Nokia article and project to work through your understanding of the plumbing and get an application going that will exchange text. http://developer.nokia.com/Resources/Library/Lumia/#!code-examples/nfc-talk.html
Then, if you want to take it up a notch, you can work through this article to upgrade your app to allow image transfer functionality as well. http://developer.nokia.com/Community/Wiki/Transfer_an_Image_with_NFC

Related

Bluetooth Pairing (SSP) on Windows 10 with 32feet.NET

I've just started a project that will require me to pair a Windows 10 tablet with another bluetooth device.
I decided to start with a simple windows forms app to familiarise myself with the process. I added the 32feet.NET NuGet package to my solution, and quickly had success with searching for devices and populating a listbox.
client = new BluetoothClient();
devices = client.DiscoverDevices();
if (devices.Length > 0)
{
foreach (var device in devices)
{
lstBTDevices.Items.Add(device.DeviceName);
}
}
else
{
MessageBox.Show("Unable to detect any bluetooth devices");
}
I then added an event handler so I could select a detected device and attempt to pair with it.
private void LstBTDevices_SelectedIndexChanged(object sender, EventArgs e)
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, "123456"))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
}
On my Windows7 desktop PC with cheap Bluetooth 2.0 adaptor this causes a popup to appear on my phone requesting I enter the pincode. When I enter "123456" the pairing is successful.
However, this is where the problem starts. I then take my application and run it on my Windows10 tablet, and now when I select my phone it causes a popup to appear on my phone with a random 6 digit pincode, and a message that it should match what is displayed on my tablet screen, with pair/cancel buttons as the options. Pressing either button results in a fail.
Is this something i'm doing wrong? A driver not supported by 32feet.NET?
Any advice would be much appreciated.
UPDATE: The comment from bare_metal has helped me get a bit further
I added a BluetoothWin32Authentication event handler and added a button to initiate an SSP pairing:
EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
private void btnPairSSP_Click(object sender, EventArgs e)
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Task t = new Task(PairBluetoothTask);
t.Start();
}
}
private void PairBluetoothTask()
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, null))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
private void handleAuthRequests(object sender, BluetoothWin32AuthenticationEventArgs e)
{
switch (e.AuthenticationMethod)
{
case BluetoothAuthenticationMethod.Legacy:
MessageBox.Show("Legacy Authentication");
break;
case BluetoothAuthenticationMethod.OutOfBand:
MessageBox.Show("Out of Band Authentication");
break;
case BluetoothAuthenticationMethod.NumericComparison:
if(e.JustWorksNumericComparison == true)
{
MessageBox.Show("Just Works Numeric Comparison");
}
else
{
MessageBox.Show("Show User Numeric Comparison");
if (MessageBox.Show(e.NumberOrPasskeyAsString, "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Confirm = true;
}
else
{
e.Confirm = false;
}
}
break;
case BluetoothAuthenticationMethod.PasskeyNotification:
MessageBox.Show("Passkey Notification");
break;
case BluetoothAuthenticationMethod.Passkey:
MessageBox.Show("Passkey");
break;
default:
MessageBox.Show("Event handled in some unknown way");
break;
}
}
When I initiate pairing from my phone, this works fine, the event is triggered, the message box pops and pairing is successful.
However when I initiate pairing from the tablet, the event handler is never triggered, so pairing fails.
I believe the problem here is that the 32feet library is built around legacy pairing, so that you either need to know the pin of the device you are connecting to, or you supply it with a null to get a popup window to enter a pin. That dialog may not have made it through to the new version of windows -
Not sure on this, but the documentation for the native function that the 32feet library wraps, says to call another method if developing for newer than Vista.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa362770(v=vs.85).aspx
From my research browsing through the decompiled sources of 32feet, it may look like 32feet doesn't support SSP, just others - but that may only be that the supplied bluetooth stack implementations need updating - or you need to create your own - again I am not sure.
You may want to look into Microsoft supplied libraries for .NET instead of this 3rd party, I was able to use their example from Github to successfully connect and pair with all my devices.
https://msdn.microsoft.com/en-us/library/windows/apps/mt168401.aspx
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing/cs

Windows 10 Bluetooth Low Energy Connection c#

For a project I need to get some data from a Bluetooth device on windows 10 using C#. I'm not too familiar with the Bluetooth API and can't figure out why the following is not working:
Using the BluetoothLEAdvertisementWatcher I search for advertisements, which works fine. I do receive the advertisement from the device (local name fits) as well as it's ServiceUuids. Next I try to connect to the device using the BluetoothAddress received together with the advertisement:
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher,
BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
ulong blAdress = eventArgs.BluetoothAddress;
BluetoothLEDevice blDevice = await
Windows.Devices.Bluetooth.BluetoothLEDevice.FromBluetoothAddressAsync(blAdress);
}
However, doing so results in an exception:
Element not found. (Exception from HRESULT: 0x80070490).
Is this the correct way to read data from the device? Are other options available to read the data from the services? Manually pairing the device in windows is not really an option and also seems to fail.
/Edit 1: I check for the local name of the device to make sure I only try to connect to the right one. So I guess there is a problem with connecting to this specific device, still I have no idea how to work around that. The service data was succesfully read on iOS, so it should be possible.
Until MS fixes this problem the only reliable solution to this I have found to connect to a BLE device is to ask the registry for a list of paired BLE devices and compare the bluetooth address in the advert with with registry list of paired able devices. My experience is that when FromBluetoothAddressAsync is called on an unpaired device Windows throws an exception and kills the watcher thread. I have some C++ code that I am happy to share that reads the registry and creates a list of paired BLE devices.
Hopefully MS will take the time to fully support BLE in the same manner Apple does.
Here is a reference from MS (https://social.msdn.microsoft.com/Forums/vstudio/en-US/e321cb3c-462a-4b16-b7e4-febdb3d0c7d6/windows-10-pairing-a-ble-device-from-code?forum=wdk). It seems that to use this BluetoothLEDevice.FromBluetoothAddressAsync we have to handle the exception when the device is advertising and not yet paired.
I got the same issue when I using the BluetoothLEAdvertisementWatcher directly.
Then I tested the different addresses listed by the watcher. I found it is related to the Bluetooth devices.
After adding the filter as following, I can connect to GATT device (TI Sensor Tag) successfully.
public sealed partial class MainPage : Page
{
private BluetoothLEAdvertisementWatcher watcher;
public MainPage()
{
this.InitializeComponent();
// Create and initialize a new watcher instance.
watcher = new BluetoothLEAdvertisementWatcher();
// Part 1B: Configuring the signal strength filter for proximity scenarios
// Configure the signal strength filter to only propagate events when in-range
// Please adjust these values if you cannot receive any advertisement
// Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm
// will start to be considered "in-range".
watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70;
// Set the out-of-range threshold to -75dBm (give some buffer). Used in conjunction with OutOfRangeTimeout
// to determine when an advertisement is no longer considered "in-range"
watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75;
// Set the out-of-range timeout to be 2 seconds. Used in conjunction with OutOfRangeThresholdInDBm
// to determine when an advertisement is no longer considered "in-range"
watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000);
// By default, the sampling interval is set to zero, which means there is no sampling and all
// the advertisement received is returned in the Received event
// End of watcher configuration. There is no need to comment out any code beyond this point.
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;
App.Current.Suspending += App_Suspending;
App.Current.Resuming += App_Resuming;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
App.Current.Suspending -= App_Suspending;
App.Current.Resuming -= App_Resuming;
watcher.Stop();
watcher.Received -= OnAdvertisementReceived;
watcher.Stopped -= OnAdvertisementWatcherStopped;
base.OnNavigatingFrom(e);
}
private void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
// Make sure to stop the watcher on suspend.
watcher.Stop();
// Always unregister the handlers to release the resources to prevent leaks.
watcher.Received -= OnAdvertisementReceived;
watcher.Stopped -= OnAdvertisementWatcherStopped;
}
private void App_Resuming(object sender, object e)
{
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;
}
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
var address = eventArgs.BluetoothAddress;
BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(address);
var cnt =device.GattServices.Count;
watcher.Stop();
}
/// <summary>
/// Invoked as an event handler when the watcher is stopped or aborted.
/// </summary>
/// <param name="watcher">Instance of watcher that triggered the event.</param>
/// <param name="eventArgs">Event data containing information about why the watcher stopped or aborted.</param>
private void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
}
private void start_Click(object sender, RoutedEventArgs e)
{
watcher.Start();
}
}
Just a guess, but maybe you need this:
watcher.ScanningMode = BluetoothLEScanningMode.Active;
and in the OnAdvertisementReceived event
if (e.AdvertisementType == BluetoothLEAdvertisementType.ScanResponse)
{
BluetoothLEDevice blDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(e.BluetoothAddress);
}
If this is a UWP project, ensure you enable Bluetooth capabilities.
To do so in Visual Studio solution explorer double click the *.appxmanifest, choose the 'Capabilities' tab and ensure that 'Bluetooth' is checked.
It will add some xml not unlike the following;
<Capabilities>
<Capability Name="internetClientServer" />
<DeviceCapability Name="bluetooth" />
</Capabilities>
This Question is over 3 years old, but because it has over 13000 views, I will answer.
The reason for Element not found is that Windows.Devices is not aware of advertising Ble-devices until they are paired or connected.
Instead in the OnAdvertisementReceived just use:
var device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
I also have a very simple uwp example on github, it has no controls to keep it as simple as possible. All the results are shown in the debug output window.
The most usefull info is in MainPage.xaml.cs
check it out: https://github.com/GrooverFromHolland/SimpleBleExample_by_Devicename

Windows Phone 8.1: Save list in app settings/storage

I just started developing a Windows Phone 8.1 app.
The app allows the user to add 'data' (strings) to his favorites-list.
Now the question is:
What is the best way to save the list or the single entries of the list so I am able load these favorites again on the next app start?
I thought I should save the data to a (text-) file so I can read it line by line and put the list together again.
What do you think, is this the best way to handle something like this? I am new to the Windows Phone 8.1 platform and any help is really appreciated - thanks!
The best method depends on size of data and your needs. As for saving it in Settings you can try to make an array of string upon save and make list upon loading data. Simple example can look like this:
List<string> favourites = new List<string>();
protected void Method()
{
// save as array
ApplicationData.Current.LocalSettings.Values["myList"] = favourites.ToArray();
// retrive your array and make a list from it
favourites = ((string[])ApplicationData.Current.LocalSettings.Values["myList"]).ToList();
}
Remember that LocalSettings support only simple types and have other limits - for more information take a look at MSDN.
I think i finally got it to work. Should thought about something like that much earlier.
So I got my async Task SaveAppSettingsAsync() and just call it in the Suspending-Event and in my Hardwarebuttons_Pressed-Event when the app closes:
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await SaveAppSettingsAsync();
deferral.Complete();
}
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null)
{
e.Handled = true;
if (rootFrame.CurrentSourcePageType == typeof(MainPage))
{
await SaveAppSettingsAsync();
this.Exit();
}
else if (rootFrame.CanGoBack)
rootFrame.GoBack();
}
}
Thanks for helping. I think I got better at understanding how to handle async tasks now. As I said - I'm new to Windows Phone and never really used them before.

Windows Phone, pick file using PickSingleFileAndContinue or PickMultipleFilesAndContinue

I got stuck trying to implementing file picker for windows phone app. I need to choose files from gallery using FileOpenPicker. I didn't get how it works. Here is my code:
private readonly FileOpenPicker photoPicker = new FileOpenPicker();
// This is a constructor
public MainPage()
{
// < ... >
photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
photoPicker.FileTypeFilter.Add(".jpg");
}
// I have button on the UI. On click, app shows picker where I can choose a file
private void bChoosePhoto_OnClick(object sender, RoutedEventArgs e)
{
photoPicker.PickMultipleFilesAndContinue();
}
So, what to do next? I guess I need to get a file object or something.
I found this link. It is msdn explanation where custom class ContinuationManager is implemented. This solution looks weird and ugly. I am not sure if it is the best one. Please help!
PickAndContinue is the only method that would work on Windows Phone 8.1. It's not so weird and ugly, here goes a simple example without ContinuationManager:
Let's assume that you want to pick a .jpg file, you use FileOpenPicker:
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
picker.ContinuationData.Add("keyParameter", "Parameter"); // some data which you can pass
picker.PickSingleFileAndContinue();
Once you run PickSingleFileAndContinue();, your app is deactivated. When you finish picking a file, then OnActivated event is fired, where you can read the file(s) you have picked:
protected async override void OnActivated(IActivatedEventArgs args)
{
var continuationEventArgs = args as IContinuationActivatedEventArgs;
if (continuationEventArgs != null)
{
switch (continuationEventArgs.Kind)
{
case ActivationKind.PickFileContinuation:
FileOpenPickerContinuationEventArgs arguments = continuationEventArgs as FileOpenPickerContinuationEventArgs;
string passedData = (string)arguments.ContinuationData["keyParameter"];
StorageFile file = arguments.Files.FirstOrDefault(); // your picked file
// do what you want
break;
// rest of the code - other continuation, window activation etc.
Note that when you run file picker, your app is deactivated and in some rare situations it can be terminated by OS (little resources for example).
The ContinuationManager is only a helper that should help to make some things easier. Of course, you can implement your own behaviour for simpler cases.

Windows Phone 8 canĀ“t call AudioVideoCaptureDevice second time

In my App you can open a Site where you can switch on and off the Flashlight.
The first time it works, but if I try to switch the flashlight on a second time the App crashes.
I think this is a Problem with AudioVideoCaptureDevice.OpenAsync. If I call it a second time the App crashes with a System.Reflection.TargetInvocationException WinRT-Informationen: Unable to acquire the camera. You can only use this class while in the foreground.
Someone know this Problem?
protected AudioVideoCaptureDevice Device { get; set; }
public Page10()
{
InitializeComponent();
}
async void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
if (this.Device == null)
{
// get the AudioVideoCaptureDevice
this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
}
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
// set flash power to maxinum
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
this.tglSwitch.Content = "Light on";
this.tglSwitch.SwitchForeground = new SolidColorBrush(Colors.Green);
}
}
void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
sensorLocation = CameraSensorLocation.Back;
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
this.tglSwitch.Content = "Light off";
}
}
I would recommend to initialize the camera with OpenAsync ONE TIME in page lifecycle, for example in OnNavigatedTo event. And only makeSetProperty() methods calls code in your checkbox events to control light. It is also very important to dispose camera correctly then leaving the page, for example in OnNavigatedFrom event, by calling device.Dispose(). This option also make your flashlight to work faster.
Keep in mind that Windows Phone 8.1 now has dedicated API for torch, which works great and the code is more beautiful. You can use in Silverlight project as well, but you have to migrate your project. Here is more about this http://developer.nokia.com/community/wiki/Using_the_camera_light_in_Windows_Phone_7,_8_and_8.1 and https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.devices.torchcontrol.

Categories