【问题标题】:My app can no longer connect to Bluetooth devices我的应用程序无法再连接到蓝牙设备
【发布时间】:2018-01-27 14:38:10
【问题描述】:

我正在尝试从连接到 Android Galaxy Table 或 S6 手机的蓝牙设备读取简单的 ASCII。该应用程序大约在 3 个月前工作,现在即使使用以前已知的工作设备尝试连接到标准串行端口时也会超时。

mmSocket.connect(); throws"java.io.IOException: read failed, socket might closed, read ret: -1"

调试日志在连接超时之前显示:

D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
I/art: Starting a blocking GC Instrumentation
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback

我正在尝试新设备(扫描仪)。设备正确配对,我可以使用 Play 商店的蓝牙串行控制器应用程序连接/读取。 BT Controller 应用程序也无法像我的应用程序一样连接到任一设备,但是当我扫描条形码时,数据会传输并显示在 BT Controller 应用程序的控制台中。我在我的应用程序中添加了一个编辑字段,并且确定数据显示在编辑字段中,而无需运行我的任何代码。操作系统设置显示扫描仪已“连接”。

我注意到的一件事是,在 OS BT 设备设置中,有一个“用于输入设备”的开关处于打开状态。关闭它不会使设备连接并扫描到 BT 控制器以及我的应用程序停止工作。

值得注意的是,当我拿起项目时,Android u[约会了 2 或 3 次,目前处于 V NMF26X.P55。 SDK API 25 Android 7.1.1 构建工具 26.0.2

当前权限仅包括以下内容(我尝试了课程位置权利但没有运气):

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

我想知道是否没有其他方法可以以编程方式从串行端口读取或连接。任何帮助,将不胜感激。这是我尝试并正在工作的 sn-p:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothSocket  mmSocket;
BluetoothDevice  mmDevice;
OutputStream     mmOutputStream;
InputStream      mmInputStream;

    try
    {
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // Standard SerialPortService ID
//      UUID uuid = UUID.fromString(mmDevice.getUuids()[0].toString());
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

        if (!mmSocket.isConnected())
            mmSocket.connect();
    }
    catch (IOException e)
    {
        try
        {
            // Try an alternate way of connecting (mmPort==1 not standard -1)
            if (!mmSocket.isConnected())
            {
                mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1);
                mmSocket.connect();
            }
        }
        catch (Exception ce)
        {
            showExcept(ce);
        }
    }


    // Start reading if all went well
    if (mmSocket.isConnected())
    {
        mmOutputStream = mmSocket.getOutputStream();
        mmInputStream  = mmSocket.getInputStream();

        beginListenForData();
    }

【问题讨论】:

  • stackoverflow.com/questions/33045581/… 我不确定这是否适用于此,但您可能需要获得 ACCESS_COARSE_LOCATION 权限,甚至可能在搜索设备时激活 GPS
  • 感谢@lucidbrot 的回复,我尝试了 ACCESS_COARSE_LOCATION 没有成功。我没有使用 GPS,我可以毫无问题地找到 BT 设备。它连接到失败且以前工作过的设备
  • 更新 - 我取消了所有设备的配对,修复了原始工作设备并修改了代码以删除获取输出流。以前工作的设备再次工作,我不知道为什么。新的扫描仪仍然失败。两个设备都已配对,并且扫描仪的输入设备设置为关闭。我的猜测是这与我无法连接到它的原因有关。

标签: java android bluetooth connection


【解决方案1】:

事实证明,扫描仪不支持没有固件更新的直接连接。上面的代码很好。

【讨论】:

    猜你喜欢
    • 2016-07-08
    • 2014-02-16
    • 2018-07-07
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多