mirror of
https://github.com/dotMorten/NmeaParser.git
synced 2025-12-06 07:12:04 +01:00
Added android null checks
This commit is contained in:
parent
0fd8349d98
commit
62c20a6bfd
|
|
@ -41,7 +41,7 @@ namespace NmeaParser
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class BluetoothDevice : NmeaDevice
|
public class BluetoothDevice : NmeaDevice
|
||||||
{
|
{
|
||||||
private static Java.Util.UUID SERIAL_UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
|
private static Java.Util.UUID SERIAL_UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB")!;
|
||||||
private Android.Bluetooth.BluetoothDevice m_device;
|
private Android.Bluetooth.BluetoothDevice m_device;
|
||||||
private BluetoothSocket? m_socket;
|
private BluetoothSocket? m_socket;
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ namespace NmeaParser
|
||||||
var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
|
var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
|
||||||
if (adapter != null && adapter.IsEnabled)
|
if (adapter != null && adapter.IsEnabled)
|
||||||
{
|
{
|
||||||
foreach (var b in adapter.BondedDevices.Where(d => d.GetUuids().Where(t => t.Uuid.ToString().Equals("00001101-0000-1000-8000-00805F9B34FB", StringComparison.InvariantCultureIgnoreCase)).Any()))
|
foreach (var b in adapter.BondedDevices.Where(d => d.GetUuids().Where(t => t.Uuid != null && t.Uuid.ToString()!.Equals("00001101-0000-1000-8000-00805F9B34FB", StringComparison.InvariantCultureIgnoreCase)).Any()))
|
||||||
yield return b;
|
yield return b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -75,9 +75,15 @@ namespace NmeaParser
|
||||||
if (adapter?.IsEnabled != true)
|
if (adapter?.IsEnabled != true)
|
||||||
throw new InvalidOperationException("Bluetooth Adapter not enabled");
|
throw new InvalidOperationException("Bluetooth Adapter not enabled");
|
||||||
var d = adapter.GetRemoteDevice(m_device.Address);
|
var d = adapter.GetRemoteDevice(m_device.Address);
|
||||||
|
if( d == null)
|
||||||
|
throw new InvalidOperationException($"Failed to get Remove device '{m_device.Address}'");
|
||||||
var socket = d.CreateRfcommSocketToServiceRecord(SERIAL_UUID);
|
var socket = d.CreateRfcommSocketToServiceRecord(SERIAL_UUID);
|
||||||
|
if (socket == null)
|
||||||
|
throw new InvalidOperationException($"Failed to create socket");
|
||||||
socket.Connect();
|
socket.Connect();
|
||||||
m_socket = socket;
|
m_socket = socket;
|
||||||
|
if (socket.InputStream == null)
|
||||||
|
throw new InvalidOperationException($"Failed to create socket input stream");
|
||||||
return Task.FromResult<Stream>(socket.InputStream);
|
return Task.FromResult<Stream>(socket.InputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,6 +106,8 @@ namespace NmeaParser
|
||||||
{
|
{
|
||||||
if (m_socket == null)
|
if (m_socket == null)
|
||||||
throw new InvalidOperationException("Device not open");
|
throw new InvalidOperationException("Device not open");
|
||||||
|
if (m_socket.OutputStream == null)
|
||||||
|
throw new InvalidOperationException("Device does not support writes");
|
||||||
return m_socket.OutputStream.WriteAsync(buffer, offset, length);
|
return m_socket.OutputStream.WriteAsync(buffer, offset, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,22 +95,23 @@ namespace NmeaParser
|
||||||
private bool _isNmeaSupported = false;
|
private bool _isNmeaSupported = false;
|
||||||
|
|
||||||
#if API_LEVEL_24
|
#if API_LEVEL_24
|
||||||
void IOnNmeaMessageListener.OnNmeaMessage(string message, long timestamp)
|
void IOnNmeaMessageListener.OnNmeaMessage(string? message, long timestamp)
|
||||||
#else
|
#else
|
||||||
void GpsStatus.INmeaListener.OnNmeaReceived(long timestamp, string message)
|
void GpsStatus.INmeaListener.OnNmeaReceived(long timestamp, string? message)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
_isNmeaSupported = true;
|
_isNmeaSupported = true;
|
||||||
NmeaMessage?.Invoke(this, message);
|
if (message != null)
|
||||||
|
NmeaMessage?.Invoke(this, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public event EventHandler<string>? NmeaMessage;
|
public event EventHandler<string>? NmeaMessage;
|
||||||
|
|
||||||
public float Accuracy = float.NaN;
|
public float Accuracy = float.NaN;
|
||||||
|
|
||||||
void ILocationListener.OnLocationChanged(Location location)
|
void ILocationListener.OnLocationChanged(Location? location)
|
||||||
{
|
{
|
||||||
if (location.Provider != LocationManager.GpsProvider)
|
if (location?.Provider != LocationManager.GpsProvider)
|
||||||
{
|
{
|
||||||
Accuracy = float.NaN;
|
Accuracy = float.NaN;
|
||||||
return;
|
return;
|
||||||
|
|
@ -164,11 +165,11 @@ namespace NmeaParser
|
||||||
NmeaMessage?.Invoke(this, string.Join(",", values) + "\n");
|
NmeaMessage?.Invoke(this, string.Join(",", values) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ILocationListener.OnProviderDisabled(string provider) { }
|
void ILocationListener.OnProviderDisabled(string? provider) { }
|
||||||
|
|
||||||
void ILocationListener.OnProviderEnabled(string provider) { }
|
void ILocationListener.OnProviderEnabled(string? provider) { }
|
||||||
|
|
||||||
void ILocationListener.OnStatusChanged(string provider, Availability status, Bundle extras) { }
|
void ILocationListener.OnStatusChanged(string? provider, Availability status, Bundle? extras) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue