Git Product home page Git Product logo

react-native-headphone-detection's Introduction

Hello 👋

class Tintef {
  constructor() {
    this.username = 'tintef';
    this.name = 'Nicolás Tinte';
    this.web = 'https://tintef.uy';
    this.languages = {
      'frontend': ['HTML', 'CSS', 'JavaScript', 'ReactJS', 'VueJS'],
      'backend': ['NodeJS', 'Ruby on Rails'],
      'database': ['PostgreSQL', 'SQL'],
      'devops': ['GitHub Actions', 'AWS', 'Heroku'],
      'tools': ['GIT', 'GitHub', 'Redis', 'Redux', 'Redux-Saga', 'Vuex'],
      'misc': ['Firebase', 'TDD', 'SCRUM', 'SOLID', 'GNU/Linux'],
    };
    this.mainEditor = 'neovim';
  }
}

tintef

Medium posts

react-native-headphone-detection's People

Contributors

aaronfg avatar michaelkoelewijn avatar tintef avatar vadympetryshyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-headphone-detection's Issues

Used depracated method for RN > 0.47

HI.
Could you please remove overriding of createJSModules in ReactNativeSwitchAudioOutputPackage.java, because it's not incompatible with RN 47+.

Type definitions and option to use as hook

Would be nice to have type defs for this and also an option to use it as a hook.

I wrapped the event listener stuff in a hook for myself and declared my own interface type for the listener event return value, but it would be nice to not have the linter complain about missing type defs for this module.

In case anyone else is looking for a simple solution here's what I did in my own code:

// in my hooks file
import HeadphoneDetection from 'react-native-headphone-detection';

export interface IHeadphonesDetectionResult {
  audioJack: boolean;
  bluetooth: boolean;
}

export const useHeadphonesDetection = () => {
  const [result, setResult] = useState<IHeadphonesDetectionResult>({
    audioJack: false,
    bluetooth: false,
  });
  useEffect(() => {
    function _hl(event: IHeadphonesDetectionResult) {
      setResult(event);
    }
    HeadphoneDetection.addListener(_hl);
    return () => {
      if (HeadphoneDetection.remove) {
        HeadphoneDetection.remove();
      }
    };
  });
  return result;
};

// in my components elsewhere
const headphonesDetection = useHeadphonesDetection();

This won't silence your linter from complaining about missing typedefs but at least you can use it with type-safety and also as a hook.

Not working on newer android devices

Hi , the buildconfig file is pretty old and throws errors , please update it to this one to support newer versions

buildscript {
repositories {
google()
jcenter()
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.2'
}

}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
}
lintOptions {
    abortOnError false
}

}

repositories {
mavenCentral()
google()
}

dependencies {
implementation 'com.facebook.react:react-native:+'
}

Cannot find symbol device.getType() == AudioDeviceInfo.TYPE_USB_HEADSET

After installing your package, I see this error after running npx react-native run-android

node_modules\react-native-headphone-detection\android\src\main\java\com\tintef\RNHeadphoneDetection\RNHeadphoneDetectionModule.java:110: error:
cannot find symbol
device.getType() == AudioDeviceInfo.TYPE_USB_HEADSET
^
symbol: variable TYPE_USB_HEADSET
location: class AudioDeviceInfo`

android - USB C headphones not detected

My wired usb c pixel buds and my other generic usb c headphones don't trigger a "true" on the "audioJack" prop.

Is audioJack literally just the audiojack?

And if so, adding in usb c headphone detection is needed in this module as most android phones are leaving out headphone jacks now in favor of usb c and bt headsets.

This module doesn't work for usb c headsets at the moment.

You might need to add the check for AudioDeviceInfo.type TYPE_USB_HEADSET to your list of TYPE_WIRED_HEADPHONES and TYPE_WIRED_HEADSET

Events are not detected on Android 10

On HeadphoneDetection.isAudioDeviceConnected().then(console.log); I get bluetooth true but no events are detected when I press on any button on the headphone.

The event listener HeadphoneDetection.addListener(console.log); is never called

I am testing on android 10.

Execution failed for task ':tasks'.

Execution failed for task ':tasks'.

Could not create task ':react-native-headphone-detection:compileDebugJavaWithJavac'

build gradle:

buildToolsVersion = "33.0.0"
//kotlinVersion= "1.7.0"
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 33

Ios Listener is repeating non stop

Hello,
My is does use webRTC as well as react-native-incall-manager to control media call. So, I wanted to have a listener whenever a bluetooth or wired is connected to a device and I came across react-native-headphone-detection. But I'm having issues with the listener on ios devices where it rapidly executing the listener function over and over again which freezes the app completely. You can find the following sample code of the issue where it logs the listener rapidly without stopping.

P.S. the wired thing is that when I remove InCallManager.setSpeakerphoneOn() from the listener.. the listener only run once which is fine, but when I add this function from InCallManager it runs non stopping.
I have used react-native-switch-audio-output to control speaker instead of using react-native-incall-manager and still the issue occurs.
This issue only occurs on iOS devices (I have tested on real ios device SE). Although Android works fine but it seems the listener runs twice whenever I connect/disconnect with a wired/bluetooth headset

import React, { PureComponent } from 'react';
import {
  View,
  Text,
  StyleSheet,
} from 'react-native';
import InCallManager from 'react-native-incall-manager';
import HeadphoneDetection from 'react-native-headphone-detection';

export default class Playground extends PureComponent {
  constructor() {
    super();

    this.state = {
      bluetoothAudio: false,
      wireAudio: false,
    };

    this.onHeadphoneChanged = this.onHeadphoneChanged.bind(this);
  }

  componentDidMount = () => {
    HeadphoneDetection.isAudioDeviceConnected().then((data) => {
      const { audioJack, bluetooth } = data;
      this.setState({
        bluetoothAudio: bluetooth,
        wireAudio: audioJack,
      });
    });
    HeadphoneDetection.addListener(this.onHeadphoneChanged);
  }

  onHeadphoneChanged = (data) => {
    const { audioJack, bluetooth } = data;
    this.setState({
      bluetoothAudio: bluetooth,
      wireAudio: audioJack,
    });
    if (bluetooth || audioJack) {
      InCallManager.setSpeakerphoneOn(false);
    } else {
      InCallManager.setSpeakerphoneOn(true);
    }
    console.log({
      bluetoothAudio: bluetooth,
      wireAudio: audioJack,
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>{`Bluetooth Status: ${this.state.bluetoothAudio}`}</Text>
        <Text>{`Wired Status: ${this.state.wireAudio}`}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

react-native: v0.63.3
react-native-headphone-detection: v1.3.0
react-native-incall-manager: v3.3.0
react-native-switch-audio-output: v1.1.2

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.