using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using VolChanger.View;
using VolChanger.ViewModel;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
// using Reactive.Bindings;// using Reactive.Bindings.Extensions;namespace VolChanger.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
publicpartialclass SearchDevicePage : ContentPage
{
privatereadonly SearchDeviceViewModel vm;
public SearchDevicePage ()
{
InitializeComponent ();
this.vm = new SearchDeviceViewModel();
BindingContext = vm;
// ButtonSearch.Clicked += ButtonSearch_Clicked;
}
}
}
■SearchDeviceViewModel.cs(VolChanger.ViewModel)
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using VolChanger.View;
using VolChanger.ViewModel;
using VolChanger.Model;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
using Xamarin.Forms;
using Plugin.BLE.Abstractions.EventArgs;
namespace VolChanger.ViewModel
{
publicclass SearchDeviceViewModel : INotifyPropertyChanged
{
public ObservableCollection<BTDevice> DeviceList { get; set; }
public Command GetDevicesCommand { get; }
publicevent PropertyChangedEventHandler PropertyChanged;
privatestatic PropertyChangedEventArgs SearchPropertyChangedEventArgs = new PropertyChangedEventArgs(nameof(IsSearching));
bool isSearching = false;
publicbool IsSearching
{
get { return isSearching; }
set
{
if(isSearching == value) { return; }
isSearching = value;
OnPropertyChanged(nameof(IsSearching));
// CanExecute (このコマンドが実行可能かどうか)を更新
GetDevicesCommand.ChangeCanExecute();
}
}
public SearchDeviceViewModel()
{
DeviceList = new ObservableCollection<BTDevice>();
GetDevicesCommand = new Command(
() => GetDevices(), // 実行する処理
() => !IsSearching); // このコマンドが実行可能の時にtrueを返す
}
privatevoid GetDevices()
{
if (IsSearching)
return;
try
{
IsSearching = true;
DeviceList.Clear();
IBTManager manager = DependencyService.Get<IBTManager>();
var list = manager.GetBondedDevices();
// await Adapter.StartScanningForDevicesAsync();if(list != null && list.Count > 0)
{
foreach(var dev in list)
{
DeviceList.Add(dev);
}
}
else
{
Application.Current.MainPage.DisplayAlert("VolChanger", "ペアリング済デバイスが見つかりませんでした。", "OK");
}
}
catch (Exception e)
{
Application.Current.MainPage.DisplayAlert("Error!", e.Message, "OK");
}
finally
{
IsSearching = false;
}
}
void OnPropertyChanged([CallerMemberName] string name = null) =>
PropertyChanged?.Invoke(this, SearchPropertyChangedEventArgs);
}
}
■BTDevice.cs(VolChanger.Model)
using System;
using System.Collections.Generic;
using System.Text;
namespace VolChanger.Model
{
publicclass BTDevice
{
publicstring Name { get; set; }
publicstring Uuid { get; set; }
publicstring Adress { get; set; }
publicstring Detail { get; set; }
}
}
■IBTManager.cs(VolChanger.Model)
using System;
using System.Collections.Generic;
using System.Text;
using VolChanger.Model;
namespace VolChanger.Model
{
publicinterface IBTManager
{
List<BTDevice> GetBondedDevices();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using VolChanger.Model;
using Android.Bluetooth;
using Xamarin.Forms;
[assembly: Dependency(typeof(VolChanger.Droid.BTManager))]
namespace VolChanger.Droid
{
publicclass BTManager: IBTManager
{
public List<BTDevice> GetBondedDevices()
{
// アダプター作成
BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
// ペアリング済みデバイスの取得
var listBondedDevices = adapter.BondedDevices;
var result = new List<BTDevice>();
if (listBondedDevices != null && listBondedDevices.Count > 0)
{
foreach (var device in listBondedDevices)
{
var btDevice = new BTDevice();
btDevice.Uuid = device.GetUuids().FirstOrDefault().ToString();
// Bluetoothスピーカーのみ取得する。if (btDevice.Uuid.Substring(4, 4).ToUpper() == "110A" ||
btDevice.Uuid.Substring(4, 4).ToUpper() == "110B")
{
btDevice.Name = device.Name;
btDevice.Adress = device.Address;
btDevice.Detail = string.Format("MACアドレス:{0}", btDevice.Adress);
result.Add(btDevice);
}
}
}
return result;
}
}
}
※DVDでもUSBでも「起動ディスク」として利用できるならどちらでもOK。(僕は対象PCにDVDドライブがなかったので、USBにしました。)
※USBへのイメージファイル書き込みソフトは、「USB ISO 書き込み」等でググればたくさん出てくるので、お好きなものを。
※BIOS・UEFIなどの起動設定を変えるのを忘れないように。