0

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();
    }
glez
  • 1,170
  • 3
  • 16
  • 42
  • https://stackoverflow.com/questions/33045581/location-needs-to-be-enabled-for-bluetooth-low-energy-scanning-on-android-6-0 I'm not sure if this applies here, but you might need to have the ACCESS_COARSE_LOCATION permission and possibly even activate GPS while searching for devices – lucidbrot Jan 27 '18 at 15:44
  • Thanks for the response @lucidbrot, I tried ACCESS_COARSE_LOCATION without success. I'm not using GPS and I can find the BT devices without problems. Its connecting to the device that fails and previously worked – glez Jan 27 '18 at 15:51
  • 1
    Update - I unpaired all devices, repaired the original working device and modified the code to remove getting the output stream. The previously working device works again and I don't know why. The new scanner still fails. Both devices are paired and the scanner's input device setting off. My guess is that has something to do with why I can't connect to it. – glez Jan 28 '18 at 15:55

1 Answers1

0

So as it turns out the scanner does not support direct connections without a firmware update. Above code is good.

glez
  • 1,170
  • 3
  • 16
  • 42