dotintent / react-native-ble-plx

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

All devices scanned have name equals to null #481

Closed TailNest closed 5 years ago

TailNest commented 5 years ago

Expected Behavior

Scanning devices should return a name in order to understand which device is which.

Current Behavior

Scanning devices return objects with name and localName equals null:

Device {
    id : "2C:F0:EE:19:27:CF"
    isConnectable : null 
    localName : null
    manufacturerData : "TAAQBQsUluNJ"
    mtu : 23
    name : null
    overflowServiceUUIDs : null
    rssi : -62 
    serviceData : null 
    serviceUUIDs : null 
    solicitedServiceUUIDs : null 
    txPowerLevel : null
    _manager : BleManager
    __proto__ : Object
}

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions. Both JS and platform logs can be enabled via setLogLevel function call.

import React from "react";
import { PermissionsAndroid, View, Text, Button } from "react-native";
import { BleManager } from 'react-native-ble-plx';    

const ProfileScreen = props => {
  async function requestCameraPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
        {
          title: 'Cool Photo App Camera Permission',
          message: 'Cool Photo App needs access to your camera',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('You can use the camera');
        return true;
      } else {
        console.log('Camera permission denied');
        return false;
      }
    } catch (err) {
      console.warn(err);
      return false;
    }
  }

  scanAndConnect = () => {
    const permission = requestCameraPermission();

    if (permission) {
      const bluetoothInstance = new BleManager();

      bluetoothInstance.startDeviceScan(null, null, (error, device) => {
        if (error) {
          console.log(error);
          return
        } else {
          console.log(device);
        }
      });
    } else {
      console.log("permission not granted")
    }
  };

  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
      <Text>Profile</Text>
      <Button 
        title="Start scanning"
        onPress={scanAndConnect}/>
      </View>
  )
}
dariuszseweryn commented 5 years ago

Devices do not have to advertise name as per Core Bluetooth Specification

dariuszseweryn commented 5 years ago

For identifying a device use it's id. Keep in mind that the id is different on iOS and on Android — see https://github.com/Polidea/react-native-ble-plx/wiki/Device-Connecting

RWOverdijk commented 5 years ago

@dariuszseweryn Sorry but that's a bit confusing, could you confirm my understanding is correct?

You have to use the id to identify the device but it's different on iOS and android, so we should then store two ids and compare them based on the OS?

dariuszseweryn commented 5 years ago

You can compare ids between Android devices if peripheral uses a static MAC address*. iOS ids are not comparable between devices i.e. the same peripheral will have different ids on two iOS devices.

* core bluetooth specification can teach you more about MAC addresses in Bluetooth.

RWOverdijk commented 5 years ago

i.e. the same peripheral will have different ids on two iOS devices

If this is true I'm really screwed because my entire product requires this. How can I identify a beacon if the ID is different on every device? Are you sure about this?

If this is true, do I have to use the device name instead?

dariuszseweryn commented 5 years ago

Name is usually quite wobbly. It could be major, minor, region, uuid or other unique things that beacons usually advertise. Go figure what is the best idea for your case

RWOverdijk commented 5 years ago

Why is name wobbly? I get localName on iOS and Android. It blows my mind that there's no unique identifier that's consistent 😄

So, I guess I'll have to learn about major and minor then? Is there a guide on how to identify the beacons consistently? All I need is a unique identifier and the rssi, nothing more.

dariuszseweryn commented 5 years ago

It is not easy to filter by name simply because it is a string and it is hard to make it readable and unique. Unique ID is bad because of privacy and ability to locate the user. Good luck on learning how to solve your use case

RWOverdijk commented 5 years ago

Thanks. And if you ever find out how beacons work do share! I really want to solve this. If this doesn't work I see no use for beacons other than spam devices.

sudomann commented 4 years ago

I don't understand how this library reports device name and localName as null. I simply logged all detected devices, and every single one, null. Using my smartphone to see all bluetooth devices in the area reveals them with human readable names. Therefore, they are reporting them. I instantiated a manager and scanned for devices. Is there a step i missed?

tyrone2kx commented 4 years ago

@sudomann Did you find your way around this? I have the same exact issue. It also happens that when there's no available bluetooth device, my bluetooth scan still prints a device id.

sudomann commented 4 years ago

@tyrone2kx oddly enough, it started displaying showing the names soon after. I also noticed sometimes the scan results had duplicates. One might show the name, while the other might not.

It might help to log all the scan results and closely inspect.

SatyaGanesh399 commented 1 year ago

does anyone figuredout how to handle the problem. because i could nt find the problem in the whole internet.. all the names and localnames of the devices are returning null. Please can one tell me how to display the names of the devices

aliwaqar981 commented 4 months ago

Does anyone found a solution for this? All the names and localnames are null for all devices.

aliwaqar981 commented 4 months ago

@SatyaGanesh399 have you found solution for this?

SatyaGanesh399 commented 4 months ago

@aliwaqar981 nope. i'm not woring on react native anymore

aliwaqar981 commented 4 months ago

Ah, have you moved to flutter? A general feedback 😄