2

Ok, I feel like this question is still the same old ****, but I really can't figure it out after trying all the available methods from Google. This are the errors that I got:

isSocketAllowedBySecurityPolicy start : device null
getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}

I am trying to write a simple android program that can be used to connect to a micro-controller. I used a BroadcastReceiver to receive the discovered device. The code looks like this:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (uuidExtra == null) {
                Log.d("Bluetooth", "=+=+=+=+ 1st return uuid is null.");
            }

            if (device.getName().equals("DeviceIWant")) {
                mDevice = device;
                mStatus.setText("");
                mStatus.append("\n" + mDevice.getName() + "\n" + mDevice.getAddress());
            }
            String str = device.getName() + "\n" + device.getAddress();
            Log.d("Bluetooth", "\n=+=+=+=+=+=+=+=+=+ Get in onReceive");
            Log.d("Bluetooth", str);
            // mBluetoothAdapter.startDiscovery();
        }

        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // Check if the desired device has been found yet.
            if (mDevice != null) {
                Log.d("Bluetooth", "\n=+=+=+=+ BT dev is not null.");
                // If the desired device has been found, start getting the   UUID
                mDevice.fetchUuidsWithSdp();
            }
        }

        if (BluetoothDevice.ACTION_UUID.equals(action)) {
            Log.d("Bluetooth", "\n=+=+=+=+ BT dev uuid returned.");
            // Observe the UUID
            Parcelable[] uuidExtra =         intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

            Parcelable uuidExtra[] = mDevice.getUuids();
            if (uuidExtra == null ) {
                Log.d("Bluetooth", "\n=+=+=+=+ return uuid is null.");
            }
            Log.d("Bluetooth", "\n=+=+=+=+ uuid Extra returned.");
            // Log.d("Bluetooth", "UUID: " + uuidExtra[0].toString());

            // Create the thread (it is not yet run)
            mThread = new ConnectThread(mDevice);
            // Start running the thread
            mThread.start();
            // Update the thread flag
            isThreadRunning = true;
        }

        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            // Observe the UUID
            Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps started.");
        }

        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // Observe the UUID
            Log.d("Bluetooth", "\n=+=+=+=+ BT dev discovery ps DONE!");
        }
    }
};

The error occur when I try to use mSocket.connect(), where the mSocket is an instance of BluetoothSocket. The socket was created by using this line of code:

device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

The device I use is returned by the broadcast that I saved as mDevice in the above code. The creation of the socket seems to be fine. I used the very famous SPP UUID (00001101-0000-1000-8000-00805F9B34FB) to establish the connection with no luck. I also tried to obtained the UUID from the device by using

mDevice.fetchUuidsWithSdp();

follows by

Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

but the problem is uuidExtra returned is always null. And when I connect the errors are the same.

I hope the problem description is clear. If any of you need more code to make the problem clearer, please let me know. Thanks for the time of reading the problems and the help!

Da Teng
  • 551
  • 4
  • 21
  • 2
    Ok, the problem seems to be exactly the same instance as the one described in http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3. The link provides a work around for Android version less than 4.3. The issue seems to be related to the port number specification of the new API. I will the work around and give update later. – Da Teng Jul 08 '15 at 03:11
  • So I've tried all the possible methods I could find, including the one listed in the above link. There was nothing work for me for Android Bluetooth. – Da Teng Jul 10 '15 at 07:14
  • Did you ever find a solution to this? – FreakyAli Dec 20 '21 at 21:23

0 Answers0