This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.BLUETOOTH"/> | |
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const subscription = this.bleManager.onStateChange((state) => { | |
if (state === State.PowerOn) { | |
// 已確認藍牙正常開啟 | |
subscription.remove() | |
} | |
}, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bleManager.startDeviceScan(null, { scanMode: ScanMode.LowLatency }, (error, device) => { | |
if (error) { | |
console.log(error) | |
return | |
} | |
if (device.name !== "DeviceName") return | |
bleManager.stopDeviceScan() | |
// 在這裡對 device 進行操作... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
await device.connect() | |
} catch (error) { | |
console.log(error.reason) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
await bleManager.connectToDevice(UUID, { timeout: 2000 }) | |
} catch (error) { | |
console.log(error.reason) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await device.writeCharacteristicWithResponse( | |
Service_UUID, | |
Characteristic_UUID, | |
Data | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const byteArray = ("PASS").split("").map(char => char.charCodeAt(0)) | |
await device.writeCharacteristicWithResponse( | |
Service_UUID, | |
Characteristic_UUID, | |
Buffer.from(byteArray).toString("base64") | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { value } = await bleManager.readCharacteristicForDevice( | |
Device_UUID, | |
Service_UUID, | |
Characteristic_UUID | |
); | |
console.log(atob(value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const subscription = device.monitorCharacteristicForService( | |
Service_UUID, | |
Characteristic_UUID, | |
(error, characteristic) => { | |
if (error) { | |
console.log(error) | |
return; | |
} | |
console.log(characteristic.value) | |
} |
OlderNewer