Skip to content

Commit 50bab51

Browse files
chore: optimize android ble pair (#4)
1 parent 77363df commit 50bab51

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

android/src/main/java/so/onekey/lib/ble/utils/BleUtilsModule.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,49 @@ class BleUtilsModule(private val reactContext: ReactApplicationContext) :
124124
callback.invoke(state)
125125
}
126126

127+
@SuppressLint("MissingPermission")
128+
@ReactMethod
129+
fun pairDevice(macAddress: String, callback: Callback) {
130+
val adapter = getBluetoothAdapter()
131+
if (adapter == null) {
132+
callback.invoke("Bluetooth not supported", null)
133+
return
134+
}
135+
136+
try {
137+
val device = adapter.getRemoteDevice(macAddress)
138+
139+
var bonded = false
140+
var bonding = false
141+
142+
when (device.bondState) {
143+
BluetoothDevice.BOND_BONDED -> {
144+
bonded = true
145+
bonding = false
146+
}
147+
148+
BluetoothDevice.BOND_BONDING -> {
149+
bonded = false
150+
bonding = true
151+
}
152+
153+
BluetoothDevice.BOND_NONE -> {
154+
val started = device.createBond()
155+
bonded = false
156+
bonding = started
157+
}
158+
}
159+
160+
val map: WritableMap = Arguments.createMap()
161+
map.putBoolean("bonded", bonded)
162+
map.putBoolean("bonding", bonding)
163+
callback.invoke(null, map)
164+
} catch (e: Exception) {
165+
Log.e(LOG_TAG, "pairDevice error: ${e.message}")
166+
callback.invoke(e.message, null)
167+
}
168+
}
169+
127170
@SuppressLint("MissingPermission")
128171
@ReactMethod
129172
fun getBondedPeripherals(callback: Callback) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@onekeyfe/react-native-ble-utils",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "ble uilts",
55
"source": "./src/index.tsx",
66
"main": "./dist/commonjs/index.js",

src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
22
import type { BleState, Peripheral, BondState, AdvertisingData } from './type';
33

44
const { BleUtilsModule } = NativeModules;
5+
type PairDeviceResult = {
6+
bonded: boolean;
7+
bonding: boolean;
8+
};
59

610
class BleUtils {
711
UiEventEmitter: NativeEventEmitter | null = null;
@@ -19,6 +23,38 @@ class BleUtils {
1923
});
2024
}
2125

26+
/**
27+
* [Android only]
28+
* @param macAddress
29+
* @returns
30+
*/
31+
pairDevice(macAddress: string): Promise<PairDeviceResult> {
32+
if (Platform.OS !== 'android')
33+
return Promise.resolve({
34+
bonded: true,
35+
bonding: false,
36+
});
37+
return new Promise<PairDeviceResult>((fulfill, reject) => {
38+
BleUtilsModule.pairDevice(
39+
macAddress,
40+
(error: string | null, result: PairDeviceResult | null) => {
41+
if (error) {
42+
reject(error);
43+
} else {
44+
if (result) {
45+
fulfill(result);
46+
} else {
47+
fulfill({
48+
bonded: false,
49+
bonding: false,
50+
});
51+
}
52+
}
53+
}
54+
);
55+
});
56+
}
57+
2258
/**
2359
*
2460
* @param serviceUUIDs [optional] not used on android, optional on ios.

0 commit comments

Comments
 (0)