Muh*_*sir 3 javascript android bluetooth reactjs react-native
我在我的应用程序中添加了 react-native-ble-plx。我还使用 react native link cmd 链接了它。我已遵循 lib 文档中提供的所有必需步骤。但它不工作。我从不要求用户许可,并且它给出了错误 Deivce 无权使用 BluetoothLE。这是我的代码
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smartdeviceiot">
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="23"/>
Run Code Online (Sandbox Code Playgroud)
设备搜索.js
从 '../constants/colors' 导入颜色;从“../constants/images”导入图像;import { Button, Icon, Text, Container, Header, Left, Body, Title, Right } from 'native-base'; 从 './styles/home' 导入 { HomeStyle }; import { BleManager } from'react-native-ble-plx' class DevicesSearch extends Component { static navigationOptions = { title: 'DevicesSearch' }; 构造函数(道具){ 超级(道具);const manager = new BleManager(); this.state = { }; } componentWillMount() { } render() { return ( >this.props.navigation.navigate("DrawerOpen")}> 设备搜索搜索设备);} scanAndConnect = () => { alert('asd') console.log('cal'); this.manager.startDeviceScan(null, null, (error, device) => { this.info("Scanning..."); console.log(device);
Run Code Online (Sandbox Code Playgroud)if (error) { this.error(error.message); return } if (device.name ==='MyDevice') { this.info("Connecting to Tappy"); this.manager.stopDeviceScan(); device.connect() .then((device) => { this.info("Discovering services and characteristics"); return device.discoverAllServicesAndCharacteristics() }) .then((device) => { this.info(device.id); device.writeCharacteristicWithResponseForService('12ab', '34cd','aGVsbG8gbWlzcyB0YXBweQ==') .then((characteristic) => { this.info(characteristic.value); return }) }) .catch((error) => { this.error(error.message) }) } } ); } } function mapStateToProps(state) { //传递提供者 return { } } /* Map Actions to Props */ function mapDispatchToProps(dispatch) >{ return { actions: bindActionCreators({ }, dispatch) }; } 导出默认连接( mapStateToProps, mapDispatchToProps )(DevicesSearch);
如果我的蓝牙关闭代码 console.log 我蓝牙已关闭,但是当它打开时我登录我该设备不是使用蓝牙的授权。我也厌倦了使用 AndroidPermission lib 但没有成功。它不需要用户的许可
您需要明确地向用户询问权限。我遇到了同样的问题,我通过在单独的文件中添加此代码来解决它:
export async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, {
title: 'Location permission for bluetooth scanning',
message: 'wahtever',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Location permission for bluetooth scanning granted');
return true;
} else {
console.log('Location permission for bluetooth scanning revoked');
return false;
}
} catch (err) {
console.warn(err);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我需要扫描时,在实际扫描代码之前,我执行以下操作:
scanAndConnect() {
const permission = requestLocationPermission();
if (permission) {
...here I scan because the user has given permission
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2177 次 |
| 最近记录: |