在 react-native-nfc-manager v2 中禁用声音
Disable sound in react-native-nfc-manager v2
react-native-nfc-manager 的版本 2 应该支持禁用扫描声音 (https://github.com/whitedogg13/react-native-nfc-manager/issues/60#issuecomment-464352677),但它似乎不起作用。
这是调用:
NfcManager.registerTagEvent({
alertMessage: 'my message',
readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
});
但是没用...
我做错了什么吗?
我正在使用 react-native 0.62.2, react-native-nfc-manager 2.1.7
这是全部代码:
import React from 'react'
import {
View, Text, TouchableOpacity
} from 'react-native'
import NfcManager, {NfcEvents, NfcAdapter} from 'react-native-nfc-manager';
class App extends React.Component {
componentDidMount() {
console.log('NfcManager.start()')
NfcManager.start();
console.log('NfcManager.setEventListener()')
NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
console.log('NfcEvents.DiscoverTag')
console.warn('tag', tag);
NfcManager.setAlertMessageIOS('I got your tag!');
//NfcManager.unregisterTagEvent().catch(() => 0);
});
this._listen();
}
componentWillUnmount() {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
NfcManager.unregisterTagEvent().catch(() => 0);
}
render() {
return (
<View style={{padding: 20}}>
<Text>NFC Demo</Text>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._listen}
>
<Text>Listen</Text>
</TouchableOpacity>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._cancel}
>
<Text>Cancel listening</Text>
</TouchableOpacity>
</View>
)
}
_cancel = () => {
NfcManager.unregisterTagEvent().catch(() => 0);
}
_listen = async () => {
try {
//await NfcManager.registerTagEvent({alertMessage: 'my message', readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS});
await NfcManager.registerTagEvent({
alertMessage: 'my message',
// when I added isReaderModeEnabled, tag scanning stopped working
//isReaderModeEnabled: true,
readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
});
} catch (ex) {
console.warn('ex', ex);
NfcManager.unregisterTagEvent().catch(() => 0);
}
}
}
export default App
更新:
对 ios 不可能,应该对 android 有效(它本身就有效)
那个undocumented Flag是非常特定于这个库中的undocumented Advanced Android Native direct enableReaderMode
API usage.
iOS NFC API 和较旧的 Android API 无法控制平台在标签检测时发出的声音。
额外Android具体
并查看 Android https://github.com/whitedogg13/react-native-nfc-manager/blob/master/android/src/main/java/community/revteltech/nfc/NfcManager.java
的代码
以下内容似乎部分有效(我注意到它设置了其他高级选项,这意味着它可能每 10 秒只检测一次标签)
NfcManager.registerTagEvent({
alertMessage: 'my message',
isReaderModeEnabled: true,
readerModeFlags: NfcAdapter.FLAG_READER_NFC_A |
NfcAdapter.FLAG_READER_NFC_B |
NfcAdapter.FLAG_READER_NFC_F |
NfcAdapter.FLAG_READER_NFC_V |
NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
});
react-native-nfc-manager 的版本 2 应该支持禁用扫描声音 (https://github.com/whitedogg13/react-native-nfc-manager/issues/60#issuecomment-464352677),但它似乎不起作用。
这是调用:
NfcManager.registerTagEvent({
alertMessage: 'my message',
readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
});
但是没用...
我做错了什么吗?
我正在使用 react-native 0.62.2, react-native-nfc-manager 2.1.7
这是全部代码:
import React from 'react'
import {
View, Text, TouchableOpacity
} from 'react-native'
import NfcManager, {NfcEvents, NfcAdapter} from 'react-native-nfc-manager';
class App extends React.Component {
componentDidMount() {
console.log('NfcManager.start()')
NfcManager.start();
console.log('NfcManager.setEventListener()')
NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
console.log('NfcEvents.DiscoverTag')
console.warn('tag', tag);
NfcManager.setAlertMessageIOS('I got your tag!');
//NfcManager.unregisterTagEvent().catch(() => 0);
});
this._listen();
}
componentWillUnmount() {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
NfcManager.unregisterTagEvent().catch(() => 0);
}
render() {
return (
<View style={{padding: 20}}>
<Text>NFC Demo</Text>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._listen}
>
<Text>Listen</Text>
</TouchableOpacity>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._cancel}
>
<Text>Cancel listening</Text>
</TouchableOpacity>
</View>
)
}
_cancel = () => {
NfcManager.unregisterTagEvent().catch(() => 0);
}
_listen = async () => {
try {
//await NfcManager.registerTagEvent({alertMessage: 'my message', readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS});
await NfcManager.registerTagEvent({
alertMessage: 'my message',
// when I added isReaderModeEnabled, tag scanning stopped working
//isReaderModeEnabled: true,
readerModeFlags: NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
});
} catch (ex) {
console.warn('ex', ex);
NfcManager.unregisterTagEvent().catch(() => 0);
}
}
}
export default App
更新:
对 ios 不可能,应该对 android 有效(它本身就有效)
那个undocumented Flag是非常特定于这个库中的undocumented Advanced Android Native direct enableReaderMode
API usage.
iOS NFC API 和较旧的 Android API 无法控制平台在标签检测时发出的声音。
额外Android具体
并查看 Android https://github.com/whitedogg13/react-native-nfc-manager/blob/master/android/src/main/java/community/revteltech/nfc/NfcManager.java
以下内容似乎部分有效(我注意到它设置了其他高级选项,这意味着它可能每 10 秒只检测一次标签)
NfcManager.registerTagEvent({
alertMessage: 'my message',
isReaderModeEnabled: true,
readerModeFlags: NfcAdapter.FLAG_READER_NFC_A |
NfcAdapter.FLAG_READER_NFC_B |
NfcAdapter.FLAG_READER_NFC_F |
NfcAdapter.FLAG_READER_NFC_V |
NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
});