dotintent / react-native-ble-plx

React Native BLE library
Apache License 2.0
3.05k stars 513 forks source link

startDeviceScan Unexpected Behavior. #667

Closed 0anw0 closed 4 years ago

0anw0 commented 4 years ago

Prerequisites

Expected Behavior

A scan should be starting.

Current Behavior

What is the current behaviour?

startDeviceScan isn't even run, When I try to put a [console.log() command]. It doesn't recognize when interpreted.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Install react-native-ble-plx
  2. link it to the project. and update SDK minimum version, Adding required permissions to AndroidManifest.xml
  3. editing App.js file.

Context

startDeviceScan is not responding. The commands that are inside its scope aren't reached. Interesting thing is that when I disable the Bluetooth, the function runs automatically. And Produces a BLE error!

React-Native-cli LOGS:-

[Fri May 08 2020 02:32:19.430]  LOG      Checking Bluetooth
[Fri May 08 2020 02:32:19.473]  LOG      Trying to Scan
[Fri May 08 2020 02:32:19.544]  LOG      Failed to Scan
* Contents of the `package.json` file:

{ "name": "TVB2", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest", "lint": "eslint ." }, "dependencies": { "react": "16.11.0", "react-native": "0.62.2", "react-native-ble-plx": "^2.0.0" }, "devDependencies": { "@babel/core": "7.9.6", "@babel/runtime": "7.9.6", "@react-native-community/eslint-config": "0.0.5", "babel-jest": "24.9.0", "eslint": "6.8.0", "jest": "24.9.0", "metro-react-native-babel-preset": "0.58.0", "react-test-renderer": "16.11.0" }, "jest": { "preset": "react-native" } }


This is my code: 

export default class App extends React.Component {

constructor() { super(); this.manager = new BleManager(); this.manager.setLogLevel(LogLevel.Verbose); this.state = { failed: true } }

manualMount() { console.log("Checking Bluetooth") const subscription = this.manager.onStateChange((state) => { if (state === 'PoweredOn') { this.scanAndConnect(); subscription.remove(); } }, true);

if(this.state.failed){
  console.log("Failed to Scan")
}

}

scanAndConnect() { console.log("Trying to Scan") this.manager.startDeviceScan(null, null , (error ,scannedDevice) => { console.log("In scan") if (error) { console.log(JSON.stringify(error)) return } if (scannedDevice) { console.log("scanned device:", scannedDevice); } this.setState({failed: false}) console.log("Scan Finished") }); }

render() { return (

{this.state.attampt}
0anw0 commented 4 years ago

Solutions That didn't work with me:- 1- Restarting the device after installing the App. (didn't work). 2- Try another device (I used 3 different devices, different manufacture. but didn't work). 3- Remove ComponentDidMount and mount it manually. (didn't work). 4- Disable the Bluetooth. and Enable It again. (The Function runs and produces a BLE error after disabling the Bluetooth). 5- calling the function multiple times in a short time (ex: 10 sec.). Produces a BLE error. error code:600. (Although that all the app has access to the GPS ).

dariuszseweryn commented 4 years ago

your platform logs were gathered without setting log level on the library. please set it and update the logs

0anw0 commented 4 years ago
this.manager.setLogLevel(LogLevel.Verbose);

this line should set the log level, shouldn't it?

dariuszseweryn commented 4 years ago

Yup, my bad. What device are you running on? OS version? Are Location Services turned on? (do not mistake with Location Permissions)

0anw0 commented 4 years ago

The Devices are ( Lenovo MediaPad 7 [Android 7], HTC Desire 820 [Android 4.4] [ with SensorTag App ], Realme 5 [Android 9] ).

edit: I installed the app on Samsung galaxy j6+ [Android 9], And I faced the same error.

The app has access to location Services { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION }. And I gave the app access to them manually after installing the application.

I'm wondering if Gradle version, Or React Native version can solve my problem? My Gradle version is 6.0.1/ React Native Version is 0.62.2. If I downgraded any of them. Will this solve my problem?

dariuszseweryn commented 4 years ago

The app has access to location Services { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION }. And I gave the app access to them manually after installing the application.

These are permissions not services. "GPS" must be turned on.

dariuszseweryn commented 4 years ago

And I faced the same error.

You did not mention any error before. What exact error is that?

0anw0 commented 4 years ago

The App does not preform any scans. The function itself does not run. console.log('' In scan'' ) which is inside the function does not appear in react native cli logs.

0anw0 commented 4 years ago

I didn't get any useful answers. I'm closing this issue.