킥보드와 휴대폰간의 ble 통신을 위해서 사용한 모듈
https://dotintent.github.io/react-native-ble-plx/#device
react-native-ble-plx 2.0.2 | Documentation
nativeService (NativeService) NativeService properties to be copied.
dotintent.github.io
사용법
1. 스캔할수 있는 기기들을 찾아라
this.manager.startDeviceScan( UUID_SERVICE ,null, (error, device) => {
if (error) throw new Error( error );
const manufactureBy = BlePlxHelper.getScooterManufacturer( device.name )
const macAddress = BlePlxHelper.getMacAddress({ device , manufactureBy });
if ( !macAddress ) return;
this.scooters[device.id] = {
id: device.id,
name: device.name,
manufacturerData: device.manufacturerData,
manufactureBy,
macAddress,
device,
};
if ( callback ) callback( macAddress );
});
모듈에서 제공해주는 startDeviceScan 함수에 킥보드의 특정 service uuid를 통해서 모든 기기들을 스캔후 CRC16 및 base64 로 암호화 되어있는 command를 변경해 줘서 모든 킥보드들의 정보들을 모은후 연결을 시도합니다.
특정 기기에 연결할때는
2. 기기 연결
const connect = await device.connect();
await connect.discoverAllServicesAndCharacteristics();
this.errorCallback = undefined;
this.connectedScooter = scooter;
this.commandLog =''
device.monitorCharacteristicForService(
this.scooterBleInfo.UUID_SERVICE,
this.scooterBleInfo.UUID_NOTIFY,
this.onRecvForCommand
);
monitorCharacteristicForService 안에 onRecvForCommand를 통해서 해당 킥보드의 response을 받아주는 역할을 하면 됩니다.
전체적인 흐름을 정리하면
- 특정 uuid를 통해서 킥보드 스캔 ( startDeviceScan 통해서 )
- 많은 킥보드들 중에서 하나를 선택후 connect
- connect 된 device 와 response 받을 함수 연결해 주면 된다.
- 이후 writeCharacteristicWithResponseForService 를 통해서 해당 기기에 16 hex command를 쓰면 된다.
- resonse로 들어오는 command를 다시 해석하는것에 따라 command의 성공 여부를 확인 한다.
'FE > React & RN' 카테고리의 다른 글
GraphQL (0) | 2022.06.14 |
---|---|
Context (0) | 2022.06.07 |
[React-native] 자동 배포 (0) | 2021.11.17 |
Atomic Design (아토믹 패턴) (0) | 2021.11.10 |
[Mobx] (0) | 2021.08.02 |
댓글