在带有React Native的设备之间使用蓝牙低功耗(BLE)进行通信涉及几个步骤。你需要使用一个React Native的BLE库,比如react-native-ble-plx
,来实现这一功能。以下是一个基本的指南:
首先,你需要安装react-native-ble-plx
库及其相关依赖。
npm install --save react-native-ble-plx
然后,链接原生模块(对于React Native 0.60及以上版本,这一步通常是自动的):
npx react-native link react-native-ble-plx
ios/YourProject.xcworkspace
文件。Build Phases
-> Link Binary With Libraries
中添加以下框架: CoreBluetooth.framework
CoreLocation.framework
android/app/build.gradle
文件中添加以下权限:android {
...
defaultConfig {
...
permissions {
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
}
}
}
android/app/src/main/AndroidManifest.xml
中添加以下权限:<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
以下是一个简单的示例,展示了如何在React Native中使用BLE进行通信。
import { BleManager } from 'react-native-ble-plx';
const manager = new BleManager();
manager.startDeviceScan(null, null, (error, device) => {
if (error) {
console.log(error);
return;
}
if (device.name === 'YourDeviceName' || device.id === 'YourDeviceId') {
manager.stopDeviceScan();
console.log('Device found:', device.id);
// 连接到设备
device.connect()
.then((device) => {
console.log('Device connected:', device.id);
// 发现服务和特征
return device.discoverAllServicesAndCharacteristics();
})
.then((device) => {
console.log('Services and characteristics discovered:', device.id);
// 进行读写操作
})
.catch((error) => {
console.log(error);
});
}
});
device.connect()
.then((device) => {
console.log('Device connected:', device.id);
return device.discoverAllServicesAndCharacteristics();
})
.then((device) => {
console.log('Services and characteristics discovered:', device.id);
// 进行读写操作
})
.catch((error) => {
console.log(error);
});
const serviceUUID = 'YourServiceUUID';
const characteristicUUID = 'YourCharacteristicUUID';
device.readCharacteristicForService(serviceUUID, characteristicUUID)
.then((characteristic) => {
console.log('Characteristic value:', characteristic.value);
})
.catch((error) => {
console.log(error);
});
device.writeCharacteristicWithResponseForService(serviceUUID, characteristicUUID, new Buffer([0x01, 0x02, 0x03]))
.then(() => {
console.log('Characteristic written successfully');
})
.catch((error) => {
console.log(error);
});
在Android上,你可能需要在运行时请求位置权限:
import { PermissionsAndroid } from 'react-native';
async function requestPermissions() {
try {
const granted = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
]);
if (
granted['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED &&
granted['android.permission.ACCESS_COARSE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED
) {
console.log('Location permissions granted');
} else {
console.log('Location permissions denied');
}
} catch (err) {
console.warn(err);
}
}
requestPermissions();
领取专属 10元无门槛券
手把手带您无忧上云