//private BluetoothChatService chatService = null; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); if (Debug) { Log.Error(TAG, "+++ ON CREATE +++"); } SetContentView(Resource.Layout.Main); // Get local Bluetooth adapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter; //Console.WriteLine ("bbb"); if (savedInstanceState == null) { var fm = FragmentManager; FragmentTransaction transaction = fm.BeginTransaction(); CardEmulationFragment fragment = new CardEmulationFragment(); transaction.Replace(Resource.Id.sample_content_fragment, fragment); transaction.Commit(); } // If the adapter is null, then Bluetooth is not supported if (bluetoothAdapter == null) { Toast.MakeText(this, "Bluetooth is not available", ToastLength.Long).Show(); Finish(); return; } }
public override void HandleMessage(Message msg) { switch (msg.What) { case MESSAGE_STATE_CHANGE: if (Debug) { Log.Info(TAG, "MESSAGE_STATE_CHANGE: " + msg.Arg1); } switch (msg.Arg1) { case BluetoothChatService.STATE_CONNECTED: //bluetoothChat.title.SetText (Resource.String.title_connected_to); //bluetoothChat.title.Append (bluetoothChat.connectedDeviceName); Log.Info(TAG, "MESSAGE_STATE_CHANGE: STATE_CONNECTED"); bluetoothChat.conversationArrayAdapter.Clear(); break; case BluetoothChatService.STATE_CONNECTING: //bluetoothChat.title.SetText (Resource.String.title_connecting); Log.Info(TAG, "MESSAGE_STATE_CHANGE: STATE_CONNECTING"); break; case BluetoothChatService.STATE_LISTEN: case BluetoothChatService.STATE_NONE: //bluetoothChat.title.SetText (Resource.String.title_not_connected); Log.Info(TAG, "MESSAGE_STATE_CHANGE: STATE_LISTEN/None"); break; } break; case MESSAGE_WRITE: byte[] writeBuf = (byte[])msg.Obj; // construct a string from the buffer var writeMessage = new Java.Lang.String(writeBuf); bluetoothChat.conversationArrayAdapter.Add("Me: " + writeMessage); break; case MESSAGE_READ: byte[] readBuf = (byte[])msg.Obj; // construct a string from the valid bytes in the buffer var readMessage = new Java.Lang.String(readBuf, 0, msg.Arg1); bluetoothChat.conversationArrayAdapter.Add(bluetoothChat.connectedDeviceName + ": " + readMessage); Console.WriteLine("MESSAGE_READ:{0}", readMessage.ToString()); //AccountStorage.SetAccount (bluetoothChat, readMessage.ToString ()); //var account = (TextView)bluetoothChat.FindViewById (Resource.Id.card_account_field); //string s = AccountStorage.GetAccount (bluetoothChat); //account.Text = s; AccountStorage.SetAccount(bluetoothChat, readMessage.ToString()); var fm = bluetoothChat.FragmentManager; FragmentTransaction transaction = fm.BeginTransaction(); CardEmulationFragment fragment = new CardEmulationFragment(); transaction.Replace(Resource.Id.sample_content_fragment, fragment); transaction.Commit(); //************send result to real reader************** //var sintent = new Intent("CE2.cardservice.sendApdu"); //sintent.PutExtra("_apdu", s); //bluetoothChat.SendBroadcast(sintent); //cs.SendResponseApdu (Encoding.UTF8.GetBytes(s)); //************send result to real reader************** //bluetoothChat.OnAccountRecieved (s); //account.AddTextChangedListener(new CE2.CardEmulationFragment.AccountUpdater(bluetoothChat)); //EditText mAccountField = bluetoothChat.FindViewById<EditText> (Resource.Id.card_account_field); //mAccountField.Text = (string) readMessage; break; case MESSAGE_DEVICE_NAME: // save the connected device's name bluetoothChat.connectedDeviceName = msg.Data.GetString(DEVICE_NAME); Toast.MakeText(Application.Context, "Connected to " + bluetoothChat.connectedDeviceName, ToastLength.Short).Show(); break; case MESSAGE_TOAST: Toast.MakeText(Application.Context, msg.Data.GetString(TOAST), ToastLength.Short).Show(); break; } }