简体   繁体   English

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

[英]My app can no longer connect to Bluetooth devices

I am trying to read simple ASCII from a Bluetooth device connected to an Android Galaxy Table or S6 Phone. 我正在尝试从连接到Android Galaxy Table或S6手机的蓝牙设备读取简单的ASCII。 The app was working about 3 months ago and now it times out when trying to connect to the standard serial port even with a previously known working device. 该应用程序已运行约3个月前,现在即使使用以前已知的工作设备尝试连接到标准串行端口时也会超时。

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

Debug log shows this before the connect timesout: 调试日志在连接超时之前显示以下内容:

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

I'm trying a new device (a scanner). 我正在尝试新设备(扫描仪)。 The devices pair properly and I can connect/read using the play store's BlueTooth Serial Controller app. 设备正确配对,我可以使用Play商店的蓝牙串行控制器应用程序进行连接/阅读。 The BT Controller app also can't connect to either device just like my app but when I scan a bar code the data is transmitted and displayed in the BT Controller app's console. BT Controller应用程序也无法像我的应用程序那样连接到任何一个设备,但是当我扫描条形码时,数据将传输并显示在BT Controller应用程序的控制台中。 I added an edit field to my app and sure enough the data showed up in the edit field without running any of my code. 我在应用程序中添加了一个编辑字段,并确保足够的数据显示在编辑字段中,而无需运行任何代码。 The OS settings show the scanner is "connected". 操作系统设置显示扫描仪已“连接”。

One thing I noticed is that in the OS BT device settings there is a switch to “Use For Input Device” which is on. 我注意到的一件事是,在OS BT设备设置中,有一个切换到“用于输入设备”。 Turning it off doesn't make the device connect and scanning into the BT Controller as well as my app stops working. 关闭它并不能使设备连接并扫描到BT Controller中,并且我的应用程序停止工作。

Of note is that Android u[dated 2 or 3 times when I picked the project up and is currently at V NMF26X.P55. 值得注意的是,当我选择该项目时,Android的版本是2到3倍,当前版本为V NMF26X.P55。 SDK API 25 Android 7.1.1 Build Tools 26.0.2 SDK API 25 Android 7.1.1构建工具26.0.2

Current permissions include only the following (I tried course location entitlements without luck): 当前权限仅包括以下内容(我尝试过课程位置的权利并不算幸运):

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

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

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

I'm wondering if there isn't a different method to programmatically read from the serial port or way to connect. 我想知道是否没有其他方法可以通过编程方式从串行端口读取或进行连接。 Any help would be appreciated. 任何帮助,将不胜感激。 Here's a snippet of what I tried and was working: 这是我尝试过并正在工作的片段:

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

So as it turns out the scanner does not support direct connections without a firmware update. 因此,事实证明,如果不进行固件更新,扫描仪将不支持直接连接。 Above code is good. 上面的代码是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM