locked
Bluetooth - Connect device(s) RRS feed

  • Question

  • User390328 posted

    Hi,

    I am new in develop for mobile but already program for desktop.

    The program that i am trying to do is connect to bluetooth. I read a lot of forum pages with the problem that i am facing, but for me didn´t resolve. If someone has any functional example will be very good.

    Action:

    Check BT ON : ok Check BT Connected : ok List paired devices: ok Manually connect BT ok

    Layout\activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="25px" android:minHeight="25px" android:id="@+id/btn1" /> </RelativeLayout>

    File : MainActivity.cs ` using Android.App; using Android.OS; using Android.Support.V7.App; using Android.Runtime; using Android.Widget; using Android.Bluetooth; using Java.Util; using System;

    namespace BT3 { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity { private BluetoothAdapter mBluetoothAdapter = null; private BluetoothDevice mBluetoothDevice; private BluetoothSocket mBluetoothSocket = null; private UUID mUuid;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            Click();
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    
        public void Click()
        {
            Button btn1 = FindViewById<Button>(Resource.Id.btn1);
            btn1.Click += Btn1_Click;
        }
    
        private void Btn1_Click(object sender, System.EventArgs e)
        {
            mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
    
            foreach (BluetoothDevice btpaired in mBluetoothAdapter.BondedDevices)
            {
                if (btpaired.Name.Contains("A-1668")) // its device is my earphone
                {
    
                    Console.WriteLine("NAME: " + btpaired.Name.ToString() + " ADDRESS: " + btpaired.Address.ToString());
                    mBluetoothDevice = mBluetoothAdapter.GetRemoteDevice(btpaired.Address);
                    mUuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
    
                    //mBluetoothSocket = mBluetoothDevice.CreateInsecureRfcommSocketToServiceRecord(mUuid);
                    mBluetoothSocket = mBluetoothDevice.CreateRfcommSocketToServiceRecord(mUuid);
    
    
                    try { mBluetoothSocket.Connect(); } //Connect();
                    catch (System.Exception erro) { Console.WriteLine("RESULT : " + erro.Message); }
    
                }
    
    
            }
    
        }
    }
    

    }`

    OUTPUT

    `11-11 17:26:33.773 D/ViewRootImpl@6ff0b2MainActivity: ViewPostIme pointer 0 11-11 17:26:33.872 D/ViewRootImpl@6ff0b2MainActivity: ViewPostIme pointer 1 NAME: A-1668 ADDRESS: FC:27:45:1D:F8:4F11-11 17:26:41.675 I/mono-stdout(30655): NAME: A-1668 ADDRESS: FC:27:45:1D:F8:4F

    11-11 17:26:46.691 D/BluetoothUtils(30655): isSocketAllowedBySecurityPolicy start : device null 11-11 17:26:46.691 W/BluetoothAdapter(30655): getBluetoothService() called with no BluetoothManagerCallback RESULT : read failed, socket might closed or timeout, read ret: -111-11 17:26:55.109 I/mono-stdout(30655): RESULT : read failed, socket might closed or timeout, read ret: -1

    11-11 17:27:12.516 I/Choreographer(30655): Skipped 2318 frames! The application may be doing too much work on its main thread.`

    Monday, November 11, 2019 9:09 PM

Answers

  • User390328 posted

    All my tests are in real device.

    I fixed the problem, I was using a UUID recommended in lot of forums mUuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");

    So I decided check the value, I got the value from the device and the information are different in some devices.

    Then we need get the information direct from the device like this

    mUuid = UUID.FromString(btpaired.GetUuids()[0].Uuid.ToString());

    some devices has more than one UUID in this case just continue the looping

    Now I am trying get the information from the stream, I will open another question in the forum.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 14, 2019 11:32 AM

All replies

  • User92365 posted

    hELLO @wilmors

    execute your action on a runUIThread ->

    this.RunOnUiThread(ConnectBT);

    private void ConnectBT() { //YOUR CODE }

    Tuesday, November 12, 2019 6:33 AM
  • User390328 posted

    Thank you for your reply, but the problem persist, the error messagem is the same

    Tuesday, November 12, 2019 6:55 PM
  • User382871 posted

    The bluetooth test code shoule be deployed on a real device instead of emulator. And a similar issue, you can refer to: https://forums.xamarin.com/discussion/169803/help-connecting-via-bluetooth

    Wednesday, November 13, 2019 7:30 AM
  • User390328 posted

    All my tests are in real device.

    I fixed the problem, I was using a UUID recommended in lot of forums mUuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");

    So I decided check the value, I got the value from the device and the information are different in some devices.

    Then we need get the information direct from the device like this

    mUuid = UUID.FromString(btpaired.GetUuids()[0].Uuid.ToString());

    some devices has more than one UUID in this case just continue the looping

    Now I am trying get the information from the stream, I will open another question in the forum.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 14, 2019 11:32 AM
  • User382871 posted

    Congrats! Please mark your solution as the answer. It'll help othes who face the similar problem.

    Thursday, November 14, 2019 3:34 PM