2

I am working on a project where I need to connect to a bluetooth scanner(Motorola CS3070). I need to capture the input stream and use it to populate a list box with the scanned barcode. I tried to create a secure socket and connect to it, but the socket fails to connect. (The device is ON and paired. Its working as a physical keyboard and populates the scanned code if cursor is in a editable field). This is my code:

 private static BluetoothAdapter bluetoothAdapter = null;
 private const int REQUEST_ENABLE_BT = 2;
 public static ParcelUuid UUID;
 public static Dictionary<string, string> deviceDictionary = new Dictionary<string, string>();
 public static int STATE = 0;
 public static BluetoothSocket mySocket;
 public static Activity _activity;
 private static BluetoothReceiver receiver;
 private static BluetoothDevice pairedBTDevice;

 public void InitializeBluetooth()
    {
        // Get local Bluetooth adapter
        bluetoothAdapter = BluetoothAdapter.DefaultAdapter;

        // 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;
        }

        // If bluetooth is not enabled, ask to enable it
        if (!bluetoothAdapter.IsEnabled)
        {
            var enableBtIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
            StartActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        // Get connected devices
        var listOfDevices = bluetoothAdapter.BondedDevices;
        if (listOfDevices.Count > 0)
        {
            foreach (var bluetoothDevice in listOfDevices)
            {
                UUID = bluetoothDevice.GetUuids().ElementAt(0);
                if (!deviceDictionary.ContainsKey(bluetoothDevice.Name))
                    deviceDictionary.Add(bluetoothDevice.Name, bluetoothDevice.Address);
            }
        }
        else
        {
            connectButton.Text = "Scanner Not connected";
            connectButton.Clickable = false;
        }

        if (bluetoothAdapter.IsEnabled && listOfDevices.Count > 0)
        {
            if (listOfDevices.ElementAt(0).BondState == Bond.Bonded)
            {
                pairedBTDevice = listOfDevices.ElementAt(0);
                mySocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid);
                var thread = new Thread(BTConnect);
                thread.Start();
            }
        }
    }

    public static void BTConnect()
    {
        try
        {
            mySocket.Connect();
            _activity.RunOnUiThread(() => connectButton.Text = " Scanner Connected");
            _activity.RunOnUiThread(() => connectButton.Clickable = false);
            receiver.loop = true;
            var thread = new Thread(BTRead, "BTRead");
            thread.Start();
        }
        catch (Exception e)
        {
            _activity.RunOnUiThread(() => Toast.MakeText(_activity.ApplicationContext, "Couldn't connect. Is the device on and paired?", ToastLength.Long).Show());
            _activity.RunOnUiThread(() => connectButton.Text = "Scanner Not connected");
            _activity.RunOnUiThread(() => connectButton.Clickable = true);
            receiver.loop = false;
        }
    }

The code gives an exception at the line mySocket.Connect(); The exception is Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1". I also tried to create a fallback socket with some example I found on StackOverflow here but that didn't help me. Can someone please help me resolve this.

Thanks

Edit* My application has the following bluetooth permissions:

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH
Community
  • 1
  • 1
Samy S.Rathore
  • 1,825
  • 3
  • 26
  • 43
  • can we see the code where you create your socket? – gimpycpu Dec 29 '14 at 08:12
  • The `BluetoothSocket mySocket` is declared as a class member, and the code to create the socket is there in the InitializeBluetooth() method `mySocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid);`. Am I doing something wrong here? – Samy S.Rathore Dec 29 '14 at 08:22

1 Answers1

2

Graham's comment on my Xamarin Forum Thread helped me and pushed me in the right direction. The problem with my code was that I was assuming that the first available Uuid would be the one that I need to connect to the device

UUID = bluetoothDevice.GetUuids().ElementAt(0);

But What I needed to do was to first check if the Uuids matches the Bluetooth Serial Port Profile and thus filter devices which support SPP.

BluetoothService.SerialPort is the standard string 00001101-0000-1000-8000-00805f9b34fb for all SPP devices.

It also helped me figure out that my device was configured to use the HID Bluetooth profile while I needed to use it in SPP mode. So I updated my code to filter only devices supporting SPP and then used ConnectAsync() to connect to the device. And now I'm able to read the InputStream.

Samy S.Rathore
  • 1,825
  • 3
  • 26
  • 43