Skip to content

Commit

Permalink
Added rssi value, alias name and device type to BluetoothDevice; scan…
Browse files Browse the repository at this point in the history
… results and bonded devices now filtered to not show BLE devices; example app updates; dart & kotlin formatting
  • Loading branch information
LenhartStephan committed Apr 18, 2024
1 parent 1665e73 commit 2e72139
Show file tree
Hide file tree
Showing 14 changed files with 739 additions and 544 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.2
* Added RSSI value, alias name and device type added to BluetoothDevice
* Scan results und bonded devices are now filtered correctly to only return BL Classic devices
* Example app updated: Tap on a scan result to connect to it and send and receive messages.

## 0.0.1

* Initial release: Scan for and connect to BL Classic devices.
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,16 @@ In the **android/app/src/main/AndroidManifest.xml** add:

```xml
<!-- Permissions for Android 12 or above -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" /><uses-permission
android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />

<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
```

#### With location access
Expand All @@ -71,18 +68,16 @@ In the **android/app/src/main/AndroidManifest.xml** add:

```xml
<!-- Permissions for Android 12 or above -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /><uses-permission
android:name="android.permission.BLUETOOTH_CONNECT" /><uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
```

Then pass the `accessFineLocation` parameter when initializing the plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import android.content.Context
import android.content.Intent
import io.flutter.plugin.common.EventChannel

class AdapterStateReceiver : EventChannel.StreamHandler{
companion object{
class AdapterStateReceiver : EventChannel.StreamHandler {
companion object {
const val CHANNEL_NAME: String = "${BlueClassicHelper.NAMESPACE}/adapterState"
}

Expand All @@ -30,11 +30,13 @@ class AdapterStateReceiver : EventChannel.StreamHandler{
val adapterState =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)

adapterStateEventSink.let { it?.success(
BlueClassicHelper.adapterStateString(
adapterState
adapterStateEventSink.let {
it?.success(
BlueClassicHelper.adapterStateString(
adapterState
)
)
) }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package dev.lenhart.flutter_blue_classic

import android.Manifest
import android.annotation.TargetApi
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import androidx.annotation.RequiresPermission

class BlueClassicHelper {
companion object {
const val NAMESPACE: String = "blue_classic"
const val METHOD_CHANNEL_NAME: String = "$NAMESPACE/methods"
const val ERROR_ADDRESS_INVALID : String= "addressInvalid"
const val ERROR_ADDRESS_INVALID: String = "addressInvalid"

fun adapterStateString(state: Int): String {
return when (state) {
Expand All @@ -29,11 +32,30 @@ class BlueClassicHelper {
}
}

fun bluetoothDeviceToMap(device: BluetoothDevice): MutableMap<String, Any> {
val entry: MutableMap<String, Any> = HashMap()
fun deviceTypeString(type: Int): String {
return when (type) {
BluetoothDevice.DEVICE_TYPE_LE -> "le"
BluetoothDevice.DEVICE_TYPE_CLASSIC -> "classic"
BluetoothDevice.DEVICE_TYPE_DUAL -> "dual"
else -> "unknown"
}
}

@TargetApi(34)
@RequiresPermission(value = Manifest.permission.BLUETOOTH_CONNECT)
fun bluetoothDeviceToMap(
device: BluetoothDevice,
rssi: Short? = null
): MutableMap<String, Any?> {
val entry: MutableMap<String, Any?> = HashMap()
entry["address"] = device.address
entry["name"] = device.name ?: ""
entry["name"] = device.name
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
entry["alias"] = device.alias
}
entry["bondState"] = bondStateString(device.bondState)
entry["deviceType"] = deviceTypeString(device.type)
entry["rssi"] = rssi
return entry
}
}
Expand Down
Loading

0 comments on commit 2e72139

Please sign in to comment.