Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.Bluetooth;
- using Java.Util;
- using System.Threading;
- using System.IO;
- namespace MonoAndroidApplication1.Services
- {
- public class BluetoothCommService
- {
- private UUID ServiceUUID = UUID.FromString( "00001101-0000-1000-8000-00805F9B34FB" );
- protected BluetoothAdapter _Adapter;
- protected Handler _Handler;
- protected Thread _ReadingThread = null;
- private bool _AbortReadingThread = false;
- public BluetoothCommService( Context context, Handler handler )
- {
- _Adapter = BluetoothAdapter.DefaultAdapter;
- _Handler = handler;
- }
- public const int STATE_NONE = 0; // we're doing nothing
- public const int STATE_LISTEN = 1; // now listening for incoming connections
- public const int STATE_CONNECTING = 2; // now initiating an outgoing connection
- public const int STATE_CONNECTED = 3; // now connected to a remote device
- public void ConnectAsync( BluetoothDevice device, Action<BluetoothSocket> completedCallback )
- {
- _Adapter.CancelDiscovery();
- Thread thread = new Thread( new ParameterizedThreadStart( ConnectThreadWorker ) );
- thread.Start( new RequestState() { Device = device, Callback = completedCallback } );
- }
- private void ConnectThreadWorker( object objState )
- {
- RequestState state = (RequestState)objState;
- BluetoothSocket socket = TryConnect( state.Device, 0 );
- if ( state.Callback != null )
- {
- state.Callback( socket );
- }
- if ( socket != null )
- {
- // start reading thread
- _ReadingThread = new Thread( new ParameterizedThreadStart( ReadingThreadWorker ) );
- _ReadingThread.Start( socket );
- }
- }
- private void ReadingThreadWorker( object objSocket )
- {
- _AbortReadingThread = false;
- BluetoothSocket socket = (BluetoothSocket)objSocket;
- StreamReader reader = new StreamReader( socket.InputStream );
- char[] buffer = new char[ 1024 ];
- //SendText( socket, "$PFAL,MSG.VERSION.COMPLETE" );
- while ( !_AbortReadingThread )
- {
- try
- {
- int n = reader.ReadBlock( buffer, 0, buffer.Length );
- _Handler.ObtainMessage( 1, n, -1, buffer ).SendToTarget();
- }
- catch ( Java.IO.IOException )
- {
- Android.Util.Log.Error( "BTCOMM", "disconnected" );
- break;
- }
- }
- }
- private BluetoothSocket TryConnect( BluetoothDevice device, int retryCounter )
- {
- BluetoothSocket socket = null;
- try
- {
- socket = device.CreateRfcommSocketToServiceRecord( ServiceUUID );
- }
- catch ( Java.IO.IOException )
- {
- if ( retryCounter < 5 )
- {
- return ( TryConnect( device, retryCounter + 1 ) );
- }
- }
- return ( socket );
- }
- public void Disconnect( BluetoothSocket socket )
- {
- _AbortReadingThread = true;
- socket.Close();
- }
- public void SendText( BluetoothSocket socket, string text )
- {
- try
- {
- StreamWriter writer = new StreamWriter( socket.OutputStream );
- writer.Write( text );
- }
- catch ( Java.IO.IOException )
- {
- }
- }
- private class RequestState
- {
- public BluetoothSocket Socket
- {
- get;
- set;
- }
- public BluetoothDevice Device
- {
- get;
- set;
- }
- public Action<BluetoothSocket> Callback
- {
- get;
- set;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment