Giter Site home page Giter Site logo

Comments (9)

arnleal09 avatar arnleal09 commented on May 27, 2024 2

from bluetooth-library.

badarshahzad avatar badarshahzad commented on May 27, 2024

Hi have you implement communication callbacks? If yes did you first paired the device before connection? If yes then share the snippet code you are facing error to let us help you. Moreover, please don't forget to share your logcat error. It help us to understand quickly.

from bluetooth-library.

arnleal09 avatar arnleal09 commented on May 27, 2024

Hi, thanks for your reply, yes the callbacks are implemented and they work in fact the message "read failed, socket might closed or timeout, read ret: -1" its coming from @OverRide public void onConnectError(BluetoothDevice device, String message), this is the code:

package com.example.aleal.app_bt_1;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import me.aflak.bluetooth.Bluetooth;
import me.aflak.bluetooth.BluetoothCallback;
import me.aflak.bluetooth.DeviceCallback;
import me.aflak.bluetooth.DiscoveryCallback;

import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity {

Bluetooth bluetooth = new Bluetooth(this);
private ListView lv;
ArrayList<String> devices_list = new ArrayList<>();
String name;
BluetoothDevice device_dongle;
String mensaje;
int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION=1;

@Override
protected void onStart() {
    super.onStart();
    bluetooth.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    bluetooth.onStop();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView lv1 = (ListView)findViewById(R.id.element_list);
    bluetooth.setBluetoothCallback(new BluetoothCallback() {
        @Override
        public void onBluetoothTurningOn() {


        }

        @Override
        public void onBluetoothOn() {

        }

        @Override
        public void onBluetoothTurningOff() {


        }

        @Override
        public void onBluetoothOff() {

        }

        @Override
        public void onUserDeniedActivation() {
            bluetooth.showEnableDialog(MainActivity.this);
            bluetooth.onActivityResult(1,1);
            // when using bluetooth.showEnableDialog()
            // you will also have to call bluetooth.onActivityResult()
        }
    });

    bluetooth.setDiscoveryCallback(new DiscoveryCallback() {

        @Override
        public void onDiscoveryStarted() {
            // scan finished

        }

        @Override
        public void onDiscoveryFinished() {
            // scan finished

        }

        @Override
        public void onDeviceFound(BluetoothDevice device) {
            // device found
            lv = (ListView) findViewById(R.id.element_list);
            devices_list.add(device.getName() + "\n" + device.getAddress());
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1,devices_list);
            lv.setAdapter(arrayAdapter);
            name= device.getName().toString();
            if(name.equals("BLE-SPP"))
            {
                device_dongle=device;
            }

        }

        @Override
        public void onDevicePaired(BluetoothDevice device) {
            // device paired
            device_dongle=device;
        }

        @Override
        public void onDeviceUnpaired(BluetoothDevice device) {
            // device unpaired
        }

        @Override
        public void onError(String message) {
            // error occurred
        }
    });

    bluetooth.setDeviceCallback(new DeviceCallback() {
        @Override public void onDeviceConnected(BluetoothDevice device)
        {
            device_dongle=device;
        }
        @Override public void onDeviceDisconnected(BluetoothDevice device, String message)
        {


        }
        @Override public void onMessage(String message)
        {

        }
        @Override public void onError(String message)
        {

            mensaje=message;

        }
        @Override public void onConnectError(BluetoothDevice device, String message)
        {

            device_dongle=device;
        }
    });

  lv1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if(bluetooth.isEnabled())
            {
                if (!bluetooth.isConnected())
                {
                    bluetooth.stopScanning();
                    name = ((TextView) view).getText().toString();
                    CharSequence dev = name;
                    int endIndex = name.lastIndexOf("\n");
                    bluetooth.pair(device_dongle);
                }
            }
        }
    });

}

public void eneable_BT(View view) {
    // Do something in response to button click
    if(!bluetooth.isEnabled())
        bluetooth.enable();
    else {
    //    Toast.makeText(this, "EL Bluetooth ya esta encendido!",
    //            Toast.LENGTH_LONG).show();
    }
}

public void find_list(View view) {
    // Do something in response to button click
    if(bluetooth.isEnabled())
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
        bluetooth.startScanning();
}

public void disconnect(View view) {
    // Do something in response to button click
    if(bluetooth.isEnabled())
        bluetooth.disable();
    else {
     //   Toast.makeText(this, "EL Bluetooth ya esta deshabilitado!",
     //           Toast.LENGTH_LONG).show();
    }
}

public void open_drawer_1(View view) {
    // Do something in response to button click
    if (!bluetooth.isConnected())
    {
        bluetooth.connectToDevice(device_dongle);
    //    bluetooth.send("A");
    }
}

public void open_drawer_2(View view) {
    // Do something in response to button click
    if (!bluetooth.isConnected())
    {
        bluetooth.connectToDevice(device_dongle);
     //   bluetooth.send("B");
    }
}

}

this is the logcat

03-13 08:47:54.853 20238-20238/com.example.aleal.app_bt_1 D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-13 08:47:54.913 20238-20238/com.example.aleal.app_bt_1 D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-13 08:47:54.983 20238-20238/com.example.aleal.app_bt_1 D/BluetoothAdapter: cancelDiscovery
03-13 08:47:54.983 20238-20238/com.example.aleal.app_bt_1 D/BluetoothAdapter: cancelDiscovery = true
03-13 08:47:55.003 20238-20238/com.example.aleal.app_bt_1 D/BLE: PAIR RECEIVER
03-13 08:47:55.913 20238-20238/com.example.aleal.app_bt_1 D/BLE: PAIR RECEIVER
03-13 08:48:05.763 20238-20238/com.example.aleal.app_bt_1 D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-13 08:48:05.843 20238-20238/com.example.aleal.app_bt_1 D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-13 08:48:05.913 20238-20308/com.example.aleal.app_bt_1 D/BluetoothAdapter: cancelDiscovery
03-13 08:48:05.923 20238-20308/com.example.aleal.app_bt_1 D/BluetoothAdapter: cancelDiscovery = true
03-13 08:48:05.923 20238-20308/com.example.aleal.app_bt_1 D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
03-13 08:48:05.923 20238-20308/com.example.aleal.app_bt_1 D/BluetoothSocket: connect(): myUserId = 0
03-13 08:48:05.923 20238-20308/com.example.aleal.app_bt_1 W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback

from bluetooth-library.

basel93natour avatar basel93natour commented on May 27, 2024

i have the same problem please if you come a cross any solution please post it here

from bluetooth-library.

Amirhgh74 avatar Amirhgh74 commented on May 27, 2024

did anyone find the solution for this issue ? I'm having this error too I'm using the library now @basel93natour @arnleal09

from bluetooth-library.

 avatar commented on May 27, 2024

@omaraflak Is there any version of this library which solves this issue?
OnconnectError: "read failed, socket might closed or timeout, read ret: -1"

from bluetooth-library.

omaraflak avatar omaraflak commented on May 27, 2024

@proazfar @badarshahzad @basel93natour @Amirhgh74 All my apologies for the very late response. As said @arnleal09, this library does not support BLE devices. If time allows it, I will try to implement BLE.
Meanwhile, you can check the Android Documentation :

https://developer.android.com/guide/topics/connectivity/bluetooth-le

Thank you all for the interest you give to this repository.

from bluetooth-library.

amon-shadow avatar amon-shadow commented on May 27, 2024

By adding filter action my problem resolved

 // Register for broadcasts when a device is discovered
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver, intentFilter);

from bluetooth-library.

umamakhalid avatar umamakhalid commented on May 27, 2024

If you have tried connecting devices once or more then your socket might be already connected. try restarting devices.

from bluetooth-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.