-
Notifications
You must be signed in to change notification settings - Fork 3
fix: gitignore #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
216 changes: 216 additions & 0 deletions
216
android/src/main/java/so/onekey/lib/ble/utils/BleUtilsModule.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| package so.onekey.lib.ble.utils | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.bluetooth.BluetoothAdapter | ||
| import android.bluetooth.BluetoothDevice | ||
| import android.bluetooth.BluetoothManager | ||
| import android.bluetooth.BluetoothProfile.GATT | ||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.IntentFilter | ||
| import android.content.pm.PackageManager | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.util.Log | ||
| import com.facebook.react.bridge.* | ||
| import com.facebook.react.modules.core.DeviceEventManagerModule | ||
| import so.onekey.lib.ble.utils.data.Peripheral | ||
|
|
||
|
|
||
| class BleUtilsModule(private val reactContext: ReactApplicationContext) : | ||
| ReactContextBaseJavaModule(reactContext) { | ||
|
|
||
| private var bluetoothManager: BluetoothManager? = null | ||
| private val mBleBroadcastReceiver = lazy { | ||
| MyBroadcastReceiver(this) | ||
| } | ||
|
|
||
| override fun getName() = NAME | ||
|
|
||
| private fun getBluetoothManager(): BluetoothManager? { | ||
| if (bluetoothManager == null) { | ||
| bluetoothManager = | ||
| reactContext.getSystemService(Context.BLUETOOTH_SERVICE) | ||
| } | ||
| return bluetoothManager | ||
| } | ||
|
|
||
| private fun getBluetoothAdapter(): BluetoothAdapter? { | ||
| return getBluetoothManager()?.adapter | ||
| } | ||
|
|
||
| init { | ||
| registerBluetoothReceiver() | ||
| } | ||
|
|
||
| private fun registerBluetoothReceiver() { | ||
| if (getBluetoothAdapter() == null) { | ||
| Log.d(LOG_TAG, "No bluetooth support") | ||
| return | ||
| } | ||
|
|
||
| val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) | ||
| filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED) | ||
| val intentFilter = IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST) | ||
| intentFilter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY | ||
| if (Build.VERSION.SDK_INT >= 34) { | ||
| // Google in 2023 decides that flag RECEIVER_NOT_EXPORTED or RECEIVER_EXPORTED should be explicit set SDK 34(UPSIDE_DOWN_CAKE) on registering receivers. | ||
| // Also the export flags are available on Android 8 and higher, should be used with caution so that don't break compability with that devices. | ||
| reactContext.registerReceiver(mBleBroadcastReceiver.value, filter, Context.RECEIVER_EXPORTED) | ||
| reactContext.registerReceiver( | ||
| mBleBroadcastReceiver.value, | ||
| intentFilter, | ||
| Context.RECEIVER_EXPORTED | ||
| ) | ||
| } else { | ||
| reactContext.registerReceiver(mBleBroadcastReceiver.value, filter) | ||
| reactContext.registerReceiver(mBleBroadcastReceiver.value, intentFilter) | ||
| } | ||
| Log.d(LOG_TAG, "BleManager initialized") | ||
| } | ||
|
|
||
| // 向JS端发送设备绑定状态变化事件 | ||
| fun emitOnDeviceBondState(params: WritableMap) { | ||
| reactContext | ||
| .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) | ||
| .emit("onDeviceBondState", params) | ||
| } | ||
|
|
||
| // 事件监听管理 | ||
| @ReactMethod | ||
| fun addListener(eventName: String) { | ||
| // 实现为空,仅用于满足RN事件监听器注册需求 | ||
| Log.d(LOG_TAG, "addListener $eventName") | ||
| } | ||
|
|
||
| @ReactMethod | ||
| fun removeListeners(count: Int) { | ||
| // 实现为空,仅用于满足RN事件监听器注册需求 | ||
| Log.d(LOG_TAG, "removeListeners $count") | ||
| } | ||
|
|
||
| @ReactMethod | ||
| fun checkState(callback: Callback) { | ||
| Log.d(LOG_TAG, "checkState") | ||
|
|
||
| val adapter = getBluetoothAdapter() | ||
| var state = "off" | ||
| if (!reactContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { | ||
| state = "unsupported" | ||
| } else if (adapter != null) { | ||
| when (adapter.state) { | ||
| BluetoothAdapter.STATE_ON -> state = "on" | ||
| BluetoothAdapter.STATE_TURNING_ON -> state = "turning_on" | ||
| BluetoothAdapter.STATE_TURNING_OFF -> { | ||
| state = "turning_off" | ||
| } | ||
|
|
||
| BluetoothAdapter.STATE_OFF -> { | ||
| // should not happen as per https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getState() | ||
| state = "off" | ||
| } | ||
|
|
||
| else -> { | ||
| state = "off" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val map: WritableMap = Arguments.createMap() | ||
| map.putString("state", state) | ||
| Log.d(LOG_TAG, "state:$state") | ||
| callback.invoke(state) | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| @ReactMethod | ||
| fun getBondedPeripherals(callback: Callback) { | ||
| val map: WritableArray = Arguments.createArray() | ||
| val deviceSet: Set<BluetoothDevice> = getBluetoothAdapter()?.getBondedDevices() ?: emptySet() | ||
| for (device in deviceSet) { | ||
| val peripheral = Peripheral(device) | ||
| val jsonBundle: WritableMap = peripheral.asWritableMap() | ||
| map.pushMap(jsonBundle) | ||
| } | ||
| callback.invoke(null, map) | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| @ReactMethod | ||
| fun getConnectedPeripherals(serviceUUIDs: ReadableArray?, callback: Callback) { | ||
| Log.d(LOG_TAG, "Get connected peripherals") | ||
| val map: WritableArray = Arguments.createArray() | ||
|
|
||
| if (getBluetoothAdapter() == null) { | ||
| Log.d(LOG_TAG, "No bluetooth support") | ||
| callback.invoke("No bluetooth support") | ||
| return | ||
| } | ||
|
|
||
| val peripherals: List<BluetoothDevice> = | ||
| getBluetoothManager()?.getConnectedDevices(GATT) ?: emptyList() | ||
| for (entry in peripherals) { | ||
| val peripheral = Peripheral(entry) | ||
| val jsonBundle: WritableMap = peripheral.asWritableMap() | ||
| map.pushMap(jsonBundle) | ||
| } | ||
| callback.invoke(null, map) | ||
| } | ||
ByteZhang1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private class MyBroadcastReceiver(private val module: BleUtilsModule) : BroadcastReceiver() { | ||
| @SuppressLint("MissingPermission") | ||
| override fun onReceive(context: Context, intent: Intent) { | ||
| Log.d(LOG_TAG, "onReceive") | ||
| val action = intent.action | ||
|
|
||
| if (action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) { | ||
| val bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) | ||
| val prevState = intent.getIntExtra( | ||
| BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR | ||
| ) | ||
| val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| intent.getParcelableExtra( | ||
| BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java | ||
| ) | ||
| } else { | ||
| intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) | ||
| } | ||
|
|
||
| var bondStateStr = "UNKNOWN" | ||
| when (bondState) { | ||
| BluetoothDevice.BOND_BONDED -> bondStateStr = "BOND_BONDED" | ||
| BluetoothDevice.BOND_BONDING -> bondStateStr = "BOND_BONDING" | ||
| BluetoothDevice.BOND_NONE -> bondStateStr = "BOND_NONE" | ||
| BluetoothDevice.ERROR -> bondStateStr = "BOND_ERROR" | ||
| } | ||
|
|
||
| var prevBondStateStr = "UNKNOWN" | ||
| when (prevState) { | ||
| BluetoothDevice.BOND_BONDED -> prevBondStateStr = "BOND_BONDED" | ||
| BluetoothDevice.BOND_BONDING -> prevBondStateStr = "BOND_BONDING" | ||
| BluetoothDevice.BOND_NONE -> prevBondStateStr = "BOND_NONE" | ||
| BluetoothDevice.ERROR -> bondStateStr = "BOND_ERROR" | ||
| } | ||
| Log.d(LOG_TAG, "bond state: $bondStateStr") | ||
| Log.d(LOG_TAG, "bond state: $prevBondStateStr") | ||
|
|
||
| val bond = Arguments.createMap() | ||
| bond.putString("state", bondStateStr) | ||
| bond.putString("preState", prevBondStateStr) | ||
|
|
||
| val peripheral = Peripheral(device!!) | ||
| val map = peripheral.asWritableMap() | ||
| map.putMap("bondState", bond) | ||
ByteZhang1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Log.d(LOG_TAG, "onReceive BluetoothDevice BondState Change ${map}") | ||
| module.emitOnDeviceBondState(map) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "BleUtilsModule" | ||
| const val LOG_TAG: String = "RNBleUtils" | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
android/src/main/java/so/onekey/lib/ble/utils/BleUtilsPackage.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package so.onekey.lib.ble.utils | ||
|
|
||
| import com.facebook.react.ReactPackage | ||
| import com.facebook.react.bridge.NativeModule | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.uimanager.ViewManager | ||
|
|
||
|
|
||
| class BleUtilsPackage : ReactPackage { | ||
| override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | ||
| return listOf(BleUtilsModule(reactContext)) | ||
| } | ||
|
|
||
| override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | ||
| return emptyList() | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
android/src/main/java/so/onekey/lib/ble/utils/data/Peripheral.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package so.onekey.lib.ble.utils.data | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.bluetooth.BluetoothDevice | ||
| import android.util.Log | ||
| import com.facebook.react.bridge.Arguments | ||
| import com.facebook.react.bridge.WritableMap | ||
|
|
||
|
|
||
| class Peripheral( | ||
| private val device: BluetoothDevice | ||
| ) { | ||
| @SuppressLint("MissingPermission") | ||
| fun asWritableMap(): WritableMap { | ||
| val map: WritableMap = Arguments.createMap() | ||
| val advertising: WritableMap = Arguments.createMap() | ||
|
|
||
| try { | ||
| map.putString("name", device.getName()) | ||
| map.putString("id", device.getAddress()) // mac address | ||
|
|
||
| val name: String = device.getName() | ||
| if (name != null) advertising.putString("localName", name) | ||
ByteZhang1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // No scanResult to access so we can't check if peripheral is connectable | ||
| advertising.putBoolean("isConnectable", true) | ||
|
|
||
| map.putMap("advertising", advertising) | ||
| } catch (e: Exception) { // this shouldn't happen | ||
| Log.e("BleUtils", "Unexpected error on asWritableMap", e) | ||
| } | ||
|
|
||
| return map | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "extends": "./tsconfig", | ||
| "exclude": ["example", "lib"] | ||
| "exclude": ["example", "dist"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.