// 5. Set BT Operation state as Activated
internal void Pair()
{
- mBluetoothDevice.BondCreated += BluetoothDeviceBondCreated;
- mBluetoothDevice.CreateBond();
- mDeviceState = BtDeviceState.Pairing;
- BtModel.NotifyDeviceChanged(this);
- BtModel.NotifyOperationStateChanged(BtOperationState.Pairing);
+ try
+ {
+ mBluetoothDevice.BondCreated += BluetoothDeviceBondCreated;
+ mBluetoothDevice.CreateBond();
+ mDeviceState = BtDeviceState.Pairing;
+ BtModel.NotifyDeviceChanged(this);
+ BtModel.NotifyOperationStateChanged(BtOperationState.Pairing);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "Pair failed: " + e.ToString());
+ }
}
private void BluetoothDeviceBondDestroyed(object obj, BondDestroyedEventArgs ev)
// 5. Set BT Operation state as Activated
internal void Unpair()
{
- mBluetoothDevice.BondDestroyed += BluetoothDeviceBondDestroyed;
- mBluetoothDevice.DestroyBond();
- mDeviceState = BtDeviceState.Unpairing;
- BtModel.NotifyDeviceChanged(this);
- BtModel.NotifyOperationStateChanged(BtOperationState.Pairing);
+ try
+ {
+ mBluetoothDevice.BondDestroyed += BluetoothDeviceBondDestroyed;
+ mBluetoothDevice.DestroyBond();
+ mDeviceState = BtDeviceState.Unpairing;
+ BtModel.NotifyDeviceChanged(this);
+ BtModel.NotifyOperationStateChanged(BtOperationState.Pairing);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "Unpair failed: " + e.ToString());
+ }
}
private void AudioConnectionStateChanged(object obj, AudioConnectionStateChangedEventArgs ev)
return;
}
- mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
- mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
- mBluetoothAudio.Connect(BluetoothAudioProfileType.AdvancedAudioDistribution);
- mDeviceState = BtDeviceState.Connecting;
- BtModel.NotifyDeviceChanged(this);
- //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ try
+ {
+ mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
+ mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
+ mBluetoothAudio.Connect(BluetoothAudioProfileType.AdvancedAudioDistribution);
+ mDeviceState = BtDeviceState.Connecting;
+ BtModel.NotifyDeviceChanged(this);
+ //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "ConnectAudio failed: " + e.ToString());
+ }
}
// TODO
return;
}
- mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
- mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
- mBluetoothAudio.Disconnect(BluetoothAudioProfileType.AdvancedAudioDistribution);
- mDeviceState = BtDeviceState.Disconnecting;
- BtModel.NotifyDeviceChanged(this);
- //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ try
+ {
+ mBluetoothAudio = mBluetoothDevice.GetProfile<BluetoothAudio>();
+ mBluetoothAudio.AudioConnectionStateChanged += AudioConnectionStateChanged;
+ mBluetoothAudio.Disconnect(BluetoothAudioProfileType.AdvancedAudioDistribution);
+ mDeviceState = BtDeviceState.Disconnecting;
+ BtModel.NotifyDeviceChanged(this);
+ //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "DisconnectAudio failed: " + e.ToString());
+ }
}
private void HidConnectionStateChanged(object obj, HidConnectionStateChangedEventArgs ev)
return;
}
- mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
- mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
- mBluetoothHid.Connect();
- mDeviceState = BtDeviceState.Connecting;
- BtModel.NotifyDeviceChanged(this);
- //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ try
+ {
+ mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+ mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
+ mBluetoothHid.Connect();
+ mDeviceState = BtDeviceState.Connecting;
+ BtModel.NotifyDeviceChanged(this);
+ //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "ConnectHid failed: " + e.ToString());
+ }
}
internal void DisconnectHid()
return;
}
- mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
- mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
- mBluetoothHid.Disconnect();
- mDeviceState = BtDeviceState.Disconnecting;
- BtModel.NotifyDeviceChanged(this);
- //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ try
+ {
+ mBluetoothHid = mBluetoothDevice.GetProfile<BluetoothHid>();
+ mBluetoothHid.HidConnectionStateChanged += HidConnectionStateChanged;
+ mBluetoothHid.Disconnect();
+ mDeviceState = BtDeviceState.Disconnecting;
+ BtModel.NotifyDeviceChanged(this);
+ //BtModel.NotifyOperationStateChanged(BtOperationState.Connecting);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "DisconnectHid failed: " + e.ToString());
+ }
}
}
}
internal void Enable()
{
- BluetoothAdapter.Enable();
- mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activating));
+ try
+ {
+ BluetoothAdapter.Enable();
+ mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Activating));
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "Enable failed: " + e.ToString());
+ }
}
internal void Disable()
{
- BluetoothAdapter.Disable();
- mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Deactivating));
+ try
+ {
+ BluetoothAdapter.Disable();
+ mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Deactivating));
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "Disable failed: " + e.ToString());
+ }
}
internal void StartDiscovery()
{
- BluetoothAdapter.StartDiscovery();
- mIsScanning = true;
- mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searching));
+ try
+ {
+ BluetoothAdapter.StartDiscovery();
+ mIsScanning = true;
+ mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searching));
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "StartDiscovery failed: " + e.ToString());
+ }
}
internal void StopDiscovery()
{
- BluetoothAdapter.StopDiscovery();
- mIsScanning = false;
- mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched));
+ try
+ {
+ BluetoothAdapter.StopDiscovery();
+ mIsScanning = false;
+ mOperationStateChanged?.Invoke(null, new BtOperationStateChangedEventArgs(BtOperationState.Searched));
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "StopDiscovery failed: " + e.ToString());
+ }
}
internal void GetBondedDevices()
{
- IEnumerable<BluetoothDevice> devices;
- BtDevice btDevice;
- BtDeviceChangedEventArgs args;
- devices = BluetoothAdapter.GetBondedDevices();
-
- foreach (BluetoothDevice device in devices)
+ try
{
- Log.Info(SettingBluetooth.LogTag, "GetBondedDevices. Address: " + device.Address + ", Name: " + device.Name);
- if (device.IsConnected)
- {
- btDevice = new BtDevice(device, BtDeviceState.Connected);
- }
- else
+ IEnumerable<BluetoothDevice> devices;
+ BtDevice btDevice;
+ BtDeviceChangedEventArgs args;
+ devices = BluetoothAdapter.GetBondedDevices();
+ foreach (BluetoothDevice device in devices)
{
- btDevice = new BtDevice(device, BtDeviceState.Paired);
+ Log.Info(SettingBluetooth.LogTag, "GetBondedDevices. Address: " + device.Address + ", Name: " + device.Name);
+ if (device.IsConnected)
+ {
+ btDevice = new BtDevice(device, BtDeviceState.Connected);
+ }
+ else
+ {
+ btDevice = new BtDevice(device, BtDeviceState.Paired);
+ }
+ args = new BtDeviceChangedEventArgs(btDevice);
+ mDeviceChanged?.Invoke(null, args);
}
- args = new BtDeviceChangedEventArgs(btDevice);
- mDeviceChanged?.Invoke(null, args);
+ }
+ catch (Exception e)
+ {
+ Log.Error(SettingBluetooth.LogTag, "GetBondedDevices failed: " + e.ToString());
}
}