Comments (9)
from bluetooth-library.
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.
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.
i have the same problem please if you come a cross any solution please post it here
from bluetooth-library.
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.
@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.
@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.
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.
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)
- String Translate ... HOT 2
- Multiple Bluetooth Client Connections HOT 1
- AutoConnect Error HOT 1
- Allow sending and receiving data (bytes) without with use of CR LF delimiters HOT 3
- NullPointerException when starting another activity HOT 3
- Add support for PIN HOT 2
- Multiple Clients. HOT 1
- About scanning the nearby bluetooth device HOT 7
- How to set baudrate? HOT 1
- onMessage method is not calling after device sending data to application HOT 2
- Android 9 device not visible in scan HOT 1
- Cannot connect after close an activity and reopen again. HOT 1
- Can not terminate socket on activity close. HOT 2
- startDiscovery impossible HOT 7
- Connected but chat not working HOT 1
- Connected but chat not working HOT 2
- SocketReader read() returning null HOT 2
- Android Bluetooth Classic RFCOMM connection failing, getUUIDs() returns no SPP UUID HOT 2
- Audio not working in bluetooth headphone when connected to bluetooth HOT 1
- My Application restart when I connect/disconnect bluetooth USB 5.0 Bluetooth Dongle
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bluetooth-library.