Guest User

BluetoothCommService

a guest
Feb 7th, 2012
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6.  
  7. using Android.App;
  8. using Android.Content;
  9. using Android.OS;
  10. using Android.Runtime;
  11. using Android.Views;
  12. using Android.Widget;
  13. using Android.Bluetooth;
  14. using Java.Util;
  15. using System.Threading;
  16. using System.IO;
  17.  
  18. namespace MonoAndroidApplication1.Services
  19. {
  20.     public class BluetoothCommService
  21.     {
  22.         private UUID ServiceUUID = UUID.FromString( "00001101-0000-1000-8000-00805F9B34FB" );
  23.  
  24.         protected BluetoothAdapter _Adapter;
  25.         protected Handler _Handler;
  26.  
  27.         protected Thread _ReadingThread = null;
  28.         private bool _AbortReadingThread = false;
  29.  
  30.         public BluetoothCommService( Context context, Handler handler )
  31.         {
  32.             _Adapter = BluetoothAdapter.DefaultAdapter;
  33.             _Handler = handler;
  34.         }
  35.  
  36.         public const int STATE_NONE = 0;       // we're doing nothing
  37.         public const int STATE_LISTEN = 1;     // now listening for incoming connections
  38.         public const int STATE_CONNECTING = 2; // now initiating an outgoing connection
  39.         public const int STATE_CONNECTED = 3;  // now connected to a remote device
  40.  
  41.         public void ConnectAsync( BluetoothDevice device, Action<BluetoothSocket> completedCallback )
  42.         {
  43.             _Adapter.CancelDiscovery();
  44.  
  45.             Thread thread = new Thread( new ParameterizedThreadStart( ConnectThreadWorker ) );
  46.             thread.Start( new RequestState() { Device = device, Callback = completedCallback } );
  47.         }
  48.  
  49.         private void ConnectThreadWorker( object objState )
  50.         {
  51.             RequestState state = (RequestState)objState;
  52.  
  53.             BluetoothSocket socket = TryConnect( state.Device, 0 );
  54.  
  55.             if ( state.Callback != null )
  56.             {
  57.                 state.Callback( socket );
  58.             }
  59.  
  60.             if ( socket != null )
  61.             {
  62.                 // start reading thread
  63.                 _ReadingThread = new Thread( new ParameterizedThreadStart( ReadingThreadWorker ) );
  64.                 _ReadingThread.Start( socket );
  65.             }
  66.         }
  67.  
  68.         private void ReadingThreadWorker( object objSocket )
  69.         {
  70.             _AbortReadingThread = false;
  71.             BluetoothSocket socket = (BluetoothSocket)objSocket;
  72.             StreamReader reader = new StreamReader( socket.InputStream );
  73.             char[] buffer = new char[ 1024 ];
  74.  
  75.             //SendText( socket, "$PFAL,MSG.VERSION.COMPLETE" );
  76.  
  77.             while ( !_AbortReadingThread )
  78.             {
  79.                 try
  80.                 {
  81.                     int n = reader.ReadBlock( buffer, 0, buffer.Length );
  82.  
  83.                     _Handler.ObtainMessage( 1, n, -1, buffer ).SendToTarget();
  84.                 }
  85.                 catch ( Java.IO.IOException )
  86.                 {
  87.                     Android.Util.Log.Error( "BTCOMM", "disconnected" );
  88.                     break;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         private BluetoothSocket TryConnect( BluetoothDevice device, int retryCounter )
  94.         {
  95.             BluetoothSocket socket = null;
  96.  
  97.             try
  98.             {
  99.                 socket = device.CreateRfcommSocketToServiceRecord( ServiceUUID );
  100.             }
  101.             catch ( Java.IO.IOException )
  102.             {
  103.                 if ( retryCounter < 5 )
  104.                 {
  105.                     return ( TryConnect( device, retryCounter + 1 ) );
  106.                 }
  107.             }
  108.  
  109.             return ( socket );
  110.         }
  111.  
  112.         public void Disconnect( BluetoothSocket socket )
  113.         {
  114.             _AbortReadingThread = true;
  115.             socket.Close();
  116.         }
  117.  
  118.         public void SendText( BluetoothSocket socket, string text )
  119.         {
  120.             try
  121.             {
  122.                 StreamWriter writer = new StreamWriter( socket.OutputStream );
  123.                 writer.Write( text );
  124.             }
  125.             catch ( Java.IO.IOException )
  126.             {
  127.             }
  128.         }
  129.  
  130.         private class RequestState
  131.         {
  132.             public BluetoothSocket Socket
  133.             {
  134.                 get;
  135.                 set;
  136.             }
  137.  
  138.             public BluetoothDevice Device
  139.             {
  140.                 get;
  141.                 set;
  142.             }
  143.  
  144.             public Action<BluetoothSocket> Callback
  145.             {
  146.                 get;
  147.                 set;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment