diff --git a/src/NmeaParser/BluetoothDevice.Android.cs b/src/NmeaParser/BluetoothDevice.Android.cs new file mode 100644 index 0000000..eb34568 --- /dev/null +++ b/src/NmeaParser/BluetoothDevice.Android.cs @@ -0,0 +1,101 @@ +// +// Copyright (c) 2014 Morten Nielsen +// +// Licensed under the Microsoft Public License (Ms-PL) (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://opensource.org/licenses/Ms-PL.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#if __ANDROID__ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Threading.Tasks; +using Android.Bluetooth; + +namespace NmeaParser +{ + /// + /// A Bluetooth NMEA device + /// + public class BluetoothDevice : NmeaDevice + { + private static Java.Util.UUID SERIAL_UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"); + private Android.Bluetooth.BluetoothDevice m_device; + private BluetoothSocket m_socket; + + /// + /// Gets a list of bluetooth devices that supports serial communication + /// + /// + public static IEnumerable GetBluetoothSerialDevices() + { + var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter; + 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())) + yield return b; + } + } + + /// + /// Initializes a new instance of the class. + /// + /// The Android Bluetooth Device. + public BluetoothDevice(Android.Bluetooth.BluetoothDevice device) + { + m_device = device; + } + + /// + /// Creates the stream the NmeaDevice is working on top off. + /// + /// + protected override Task OpenStreamAsync() + { + var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter; + if (adapter?.IsEnabled != true) + throw new InvalidOperationException("Bluetooth Adapter not enabled"); + var d = adapter.GetRemoteDevice(m_device.Address); + var socket = d.CreateRfcommSocketToServiceRecord(SERIAL_UUID); + socket.Connect(); + m_socket = socket; + return Task.FromResult(socket.InputStream); + } + + /// + /// Closes the stream the NmeaDevice is working on top off. + /// + /// The stream. + /// + protected override Task CloseStreamAsync(System.IO.Stream stream) + { + if (stream == null) + throw new ArgumentNullException("stream"); + stream.Dispose(); + m_socket.Dispose(); + m_socket = null; + return Task.FromResult(true); + } + + /// + public override bool CanWrite => true; + + /// + public override Task WriteAsync(byte[] buffer, int offset, int length) + { + if (m_socket == null) + throw new InvalidOperationException("Device not open"); + return m_socket.OutputStream.WriteAsync(buffer, offset, length); + } + } +} +#endif \ No newline at end of file