0
I’d like to send the number 1 via bluetooth on android, already made the part of connection to send
- this is the source code
public void conectar(ProgressBar loading, BluetoothSocket socket) {
loading.Visibility = Android.Views.ViewStates.Visible;
try {
socket.Connect();
}
catch (Exception)
{
loading.Visibility = Android.Views.ViewStates.Gone;
Toast.MakeText(this, "Não foi possivel conectar", ToastLength.Long).Show();
}
try
{
var output = socket.InputStream;
loading.Visibility = Android.Views.ViewStates.Gone;
Toast.MakeText(this, "pode escrever", ToastLength.Long).Show();
byte[] buffer = new byte[1024];
output.Write(buffer, 1, 1);
}
catch (Exception) {
Toast.MakeText(this, "Não foi possivel enviar a mensagem", ToastLength.Long).Show();
loading.Visibility = Android.Views.ViewStates.Gone;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.tela);
BluetoothAdapter adaptador = BluetoothAdapter.DefaultAdapter; // procura o adap. bluetooth
TextView aparelho1 = FindViewById<TextView>(Resource.Id.aparelho1);
TextView aparelhoconfig = FindViewById<TextView>(Resource.Id.aparelhoconfig);
TextView aparelho2 = FindViewById<TextView>(Resource.Id.aparelho2);
TextView aparelho2config = FindViewById<TextView>(Resource.Id.aparelho2config);
RelativeLayout layout1 = FindViewById<RelativeLayout>(Resource.Id.layoutaparelho1);
RelativeLayout layout2 = FindViewById<RelativeLayout>(Resource.Id.layoutaparelho2);
ProgressBar loading = FindViewById<ProgressBar>(Resource.Id.progressBar1);
loading.Visibility = Android.Views.ViewStates.Gone;
if (adaptador == null)
{
Toast.MakeText(this, "Esse aparelho não tem bluetooth", ToastLength.Long).Show();
}
else {
if (adaptador.IsEnabled == true)
{
ICollection<BluetoothDevice> aparelhos = adaptador.BondedDevices;
BluetoothDevice[] apa = new BluetoothDevice[aparelhos.Count];
ParcelUuid[] chaves;
BluetoothSocket[] socket = new BluetoothSocket[aparelhos.Count];
int i = 0;
foreach (BluetoothDevice aparelho in aparelhos)
{
apa[i] = aparelho;
chaves = aparelho.GetUuids();
socket[i] = aparelho.CreateInsecureRfcommSocketToServiceRecord(chaves[i].Uuid);
i++;
}
if (apa.Length > 0)
{
aparelho1.Append(apa[0].Name);
aparelhoconfig.Append(apa[0].Address);
/// <summary>
/// evento ao clicar no outro relative layout1
/// </summary>
layout1.Click += delegate
{
Toast.MakeText(this,"clicou",ToastLength.Short).Show();
conectar(loading,socket[0]);
};
//final
if (apa.Length > 1)
{
aparelho2.Append(apa[1].Name);
aparelhoconfig.Append(apa[1].Address);
/// <summary>
/// evento ao clicar no outro relative layout2
/// </summary>
layout2.Click += delegate
{
};
//final
}
}
}
else {
Toast.MakeText(this, "Ative o bluetooth para continuar", ToastLength.Long).Show();
}
}
}`