使用 Xamarin Android 连接到蓝牙扫描仪

2024-04-09

我正在开发一个项目,需要连接到蓝牙扫描仪(摩托罗拉 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"。 我还尝试用我发现的一些示例创建一个后备套接字在 StackOverflow 上这里 https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3但这对我没有帮助。有人可以帮我解决这个问题吗?

Thanks

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

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

格雷厄姆对我的评论Xamarin 论坛主题 http://forums.xamarin.com/discussion/comment/94845#Comment_94845帮助了我并推动我走向正确的方向。 我的代码的问题是我假设第一个可用的 Uuid 将是我需要连接到设备的 Uuid

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

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Xamarin Android 连接到蓝牙扫描仪 的相关文章

随机推荐