简体   繁体   中英

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. 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.

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. 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. 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. Turning it off doesn't make the device connect and scanning into the BT Controller as well as my app stops working.

Of note is that Android u[dated 2 or 3 times when I picked the project up and is currently at V NMF26X.P55. SDK API 25 Android 7.1.1 Build Tools 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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