Skip to content

Instantly share code, notes, and snippets.

View peterfoot's full-sized avatar

Peter Foot peterfoot

View GitHub Profile
@peterfoot
peterfoot / ActivityHelper.cs
Created February 10, 2023 13:40
Get current Android Activity for a cross-platform .NET library
public static bool TryGetCurrentActivity(out Activity activity)
{
activity = null;
#if NET6_0_OR_GREATER
// check for Uno without taking a hard dependency
var t = Type.GetType("Uno.UI.ContextHelper, Uno, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null", false, true);
if (t != null)
{
@peterfoot
peterfoot / NavigationViewHelper.cs
Created March 10, 2022 10:01
Add AccessKeys to standard NavigationView elements
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace InTheHand.UI.Xaml.Controls
{
public static class NavigationViewHelper
{
/// <summary>
/// Add standard access keys to the NavigationView control.
/// </summary>
@peterfoot
peterfoot / NotificationMessageGeneric.cs
Created July 14, 2021 11:29
MvvmLight style NotificationMessage<T> for Microsoft.Toolkit.Mvvm
namespace Microsoft.Toolkit.Mvvm.Messaging.Messages
{
public sealed class NotificationMessage<T>
{
public NotificationMessage(T content, string notification)
{
Content = content;
Notification = notification;
}
@peterfoot
peterfoot / BluetoothTest.cs
Created April 23, 2018 09:34
Sample Bluetooth printing from Unity
var devices = DeviceInformation.FindAll(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var deviceInfo = devices[0]; // this makes some assumptions about your paired devices so really the results should be enumerated and checked for the correct device
var device = BluetoothDevice.FromDeviceInformation(deviceInfo);
var serResults = device.GetRfcommServices(BluetoothCacheMode.Cached);
foreach(RfcommDeviceService serv in serResults.Services)
{
if(serv.ServiceId == RfcommServiceId.SerialPort)
{
var stream = serv.OpenStream();
byte[] buff = System.Text.Encoding.ASCII.GetBytes("Testing\r\n");
@peterfoot
peterfoot / BluetoothComPort.cs
Last active January 17, 2022 06:31
enumerate the Bluetooth COM ports on desktop Windows. Determine the mappings between Bluetooth addresses and COM port names.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace BluetoothDiagnostics
{
public sealed class BluetoothComPort
{
@peterfoot
peterfoot / App.xaml.cs
Created May 26, 2017 14:56
iBeacons in UWP
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
_watcher = new BluetoothLEAdvertisementWatcher();
_watcher.Received += _watcher_Received;
_watcher.Start();
}
@peterfoot
peterfoot / BrightnessHelper.cs
Last active July 23, 2023 22:09
Set the display brightness from a Win32 application
public static class BrightnessHelper
{
public static void SetBrightness(uint brightness)
{
if (brightness > 100)
{
throw new ArgumentOutOfRangeException("brightness");
}
// get handle to primary display
@peterfoot
peterfoot / BluetoothImagingMinorClass.cs
Created March 27, 2017 21:08
Get the minor Class of Device value for a Bluetooth Imaging device.
using System;
using Windows.Devices.Bluetooth;
namespace InTheHand.Devices.Bluetooth
{
/// <summary>
/// Defines missing values in the <see cref="BluetoothMinorClass"/> enumeration for devices with a <see cref="BluetoothClassOfDevice.MajorClass"/> of Imaging.
/// </summary>
[Flags]
public enum BluetoothImagingMinorClass
@peterfoot
peterfoot / MainPage.xaml
Created March 1, 2017 22:17
MediaPlayerSample
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MediaPlayerSample"
xmlns:forms="clr-namespace:InTheHand.Forms;assembly=InTheHand.Forms"
x:Class="MediaPlayerSample.MainPage">
<StackLayout>
<forms:MediaElement HorizontalOptions="Fill" VerticalOptions="Center" HeightRequest="240" x:Name="Media" AreTransportControlsEnabled="true" Source="http://video.ch9.ms/ch9/334f/891b78a5-642d-40b4-8d02-ff40ffdd334f/LoginToLinkedinUSingXamarinAuth_mid.mp4"/>
</StackLayout>
</ContentPage>
@peterfoot
peterfoot / MainPage.xaml
Created November 11, 2016 00:34
DialASketch
<Page
x:Class="DialASketch.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DialASketch"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.TopAppBar>
<CommandBar>