使用Xamarin Android连接蓝牙扫描仪

时间:2014-12-29 08:08:55

标签: c# android bluetooth xamarin

我正在开发一个需要连接蓝牙扫描仪(Motorola CS3070)的项目。我需要捕获输入流并使用它来填充带有扫描条形码的列表框。我试图创建一个安全的套接字并连接到它,但套接字无法连接。 (设备已打开并配对。如果光标位于可编辑字段中,则它可用作物理键盘并填充扫描的代码)。这是我的代码:

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

代码在行mySocket.Connect();处提供异常。例外是Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1"。 我还尝试创建一个回退套接字,其中有一些示例我找到on StackOverflow here,但这对我没有帮助。有人可以帮我解决这个问题。

由于

编辑* 我的应用程序具有以下蓝牙权限:

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH

1 个答案:

答案 0 :(得分:2)

格雷厄姆对我Xamarin Forum Thread的评论帮助了我,并把我推向了正确的方向。 我的代码的问题是我假设第一个可用的Uuid将是我需要连接到设备的那个

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

但我需要做的是首先检查Uuids是否与蓝牙串行端口配置文件匹配,从而过滤支持SPP的设备。

BluetoothService.SerialPort是所有SPP设备的标准字符串00001101-0000-1000-8000-00805f9b34fb

它还帮助我弄清楚我的设备配置为使用HID蓝牙配置文件,而我需要在SPP模式下使用它。所以我更新了我的代码以仅过滤支持SPP的设备,然后使用ConnectAsync()连接到设备。现在我能够读取InputStream。