|
|
using CameraPreview;
|
|
|
using Plugin.LocalNotifications;
|
|
|
using Plugin.Permissions;
|
|
|
using Plugin.Permissions.Abstractions;
|
|
|
using System;
|
|
|
using System.IO.Ports;
|
|
|
using System.Threading.Tasks;
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
namespace Demo
|
|
|
{
|
|
|
public partial class MainPage : ContentPage
|
|
|
{
|
|
|
private SerialDevice _sp;
|
|
|
|
|
|
public MainPage()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
NavigationPage.SetHasNavigationBar(this, false);
|
|
|
this.webView.Navigating += WebView_Navigating;
|
|
|
this.webView.Navigated += WebView_Navigated;
|
|
|
this.webView.Source = "http://localhost:5000/index.html";
|
|
|
}
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
{
|
|
|
base.OnAppearing();
|
|
|
this.OpenSerialPort();
|
|
|
}
|
|
|
|
|
|
protected override void OnDisappearing()
|
|
|
{
|
|
|
base.OnDisappearing();
|
|
|
this.CloseSerialPort();
|
|
|
}
|
|
|
|
|
|
private void OpenSerialPort()
|
|
|
{
|
|
|
if (Device.RuntimePlatform == Device.Android)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
this._sp = new SerialDevice("/dev/ttyS4", BaudRate.B9600, Parity.None, 8, StopBits.One, Handshake.None);
|
|
|
this._sp.DataReceived += (s, e) =>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (e.Length > 0)
|
|
|
{
|
|
|
var message = BitConverter.ToString(e);
|
|
|
this.Alert(BitConverter.ToString(e));
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
this.Alert(ex.Message);
|
|
|
}
|
|
|
};
|
|
|
this._sp.Open();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine(ex.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void CloseSerialPort()
|
|
|
{
|
|
|
if (Device.RuntimePlatform == Device.Android)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (this._sp != null)
|
|
|
{
|
|
|
this._sp.Close();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine(ex.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
|
|
|
{
|
|
|
Console.WriteLine(e.Url);
|
|
|
if (e.Url.Contains("notify.html"))
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
CrossLocalNotifications.Current.Show("title", e.Url);
|
|
|
}
|
|
|
else if (e.Url.Contains("qr.html"))
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
OpenQrScanPage();
|
|
|
}
|
|
|
else if (e.Url.Contains("face.html"))
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
FaceAsync();
|
|
|
}
|
|
|
else if (e.Url.Contains("rtmp://") || e.Url.Contains(".flv") || e.Url.Contains(".m3u8"))
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
this.Navigation.PushAsync(new OnvifPage(e.Url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
|
|
|
{
|
|
|
Console.WriteLine(e.Result + ":" + e.Url);
|
|
|
if (e.Url.EndsWith("/index.html"))
|
|
|
{
|
|
|
(sender as WebView)?.EvaluateJavaScriptAsync("isApp=true;");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//private void Scan()
|
|
|
//{
|
|
|
// var scanPage = new ZXingScannerPage();
|
|
|
// scanPage.OnScanResult += (result) =>
|
|
|
// {
|
|
|
// scanPage.IsScanning = false;
|
|
|
// Device.BeginInvokeOnMainThread(async () =>
|
|
|
// {
|
|
|
// await Navigation.PopAsync();
|
|
|
// });
|
|
|
// };
|
|
|
// this.Navigation.PushAsync(scanPage);
|
|
|
//}
|
|
|
private async Task OpenQrScanPage()
|
|
|
{
|
|
|
if (await CheckCameraPermisstions())
|
|
|
{
|
|
|
var scanPage = new QrScanPage();
|
|
|
await this.Navigation.PushAsync(scanPage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async Task FaceAsync()
|
|
|
{
|
|
|
if (await CheckCameraPermisstions())
|
|
|
{
|
|
|
var facePage = new FacePage(new ScanningOptionsBase { UseFrontCameraIfAvailable = true }, new ZXingOverlay());
|
|
|
await Navigation.PushAsync(facePage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Alert(object message, string title = "<22><>ʾ", string button = "ȷ<><C8B7>")
|
|
|
{
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
|
{
|
|
|
this.DisplayAlert(title, message.ToString(), button);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private async Task<bool> CheckCameraPermisstions()
|
|
|
{
|
|
|
var mediaPlugin = Plugin.Media.CrossMedia.Current;
|
|
|
await mediaPlugin.Initialize();
|
|
|
|
|
|
if (!mediaPlugin.IsCameraAvailable || !mediaPlugin.IsTakePhotoSupported)
|
|
|
{
|
|
|
await DisplayAlert("No Camera", ":( No camera available.", "OK");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
|
|
|
|
|
|
if (cameraStatus != PermissionStatus.Granted)
|
|
|
{
|
|
|
var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera });
|
|
|
cameraStatus = results[Permission.Camera];
|
|
|
}
|
|
|
|
|
|
if (cameraStatus != PermissionStatus.Granted)
|
|
|
{
|
|
|
await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
} |