我的应用无法再连接到蓝牙设备

时间:2018-01-27 14:38:11

标签: java android bluetooth connection

我正在尝试从连接到Android Galaxy Table或S6 Phone的蓝牙设备读取简单的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应用程序的控制台中。我在我的应用程序中添加了一个编辑字段,确保数据显示在编辑字段中,而不运行任何代码。操作系统设置显示扫描仪已连接"。

我注意到的一件事是在操作系统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" />

我想知道是否有不同的方法以编程方式从串口读取或连接方式。任何帮助,将不胜感激。这是我尝试和工作的片段:

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

1 个答案:

答案 0 :(得分:0)

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

相关问题