Skip to content
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

Add get bluetooth class #1

Merged
merged 10 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothClass;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -269,20 +270,21 @@ public void onReceive(Context context, Intent intent) {
switch (action) {
case BluetoothDevice.ACTION_FOUND:
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//final BluetoothClass deviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); // @TODO . !BluetoothClass!
// final BluetoothClass deviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); // @TODO . !BluetoothClass!
//final String extraName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); // @TODO ? !EXTRA_NAME!
final int deviceRSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);

Map<String, Object> discoveryResult = new HashMap<>();
discoveryResult.put("address", device.getAddress());
discoveryResult.put("name", device.getName());
discoveryResult.put("type", device.getType());
//discoveryResult.put("class", deviceClass); // @TODO . it isn't my priority for now !BluetoothClass!
// discoveryResult.put("class", deviceClass); // @TODO . it isn't my priority for now !BluetoothClass!
discoveryResult.put("isConnected", checkIsDeviceConnected(device));
discoveryResult.put("bondState", device.getBondState());
discoveryResult.put("rssi", deviceRSSI);
discoveryResult.put("deviceClass", device.getBluetoothClass().getDeviceClass());

Log.d(TAG, "Discovered " + device.getAddress());
Log.d(TAG, "Discovered " + device.getAddress() + " (deviceClass: " + device.getBluetoothClass().getDeviceClass() + ")");
if (discoverySink != null) {
discoverySink.success(discoveryResult);
}
Expand Down Expand Up @@ -499,7 +501,6 @@ static private boolean checkIsDeviceConnected(BluetoothDevice device) {
}
}


/// Helper wrapper class for `BluetoothConnection`
private class BluetoothConnectionWrapper extends BluetoothConnection {
private final int id;
Expand Down Expand Up @@ -944,6 +945,8 @@ public void onReceive(Context context, Intent intent) {
entry.put("type", device.getType());
entry.put("isConnected", checkIsDeviceConnected(device));
entry.put("bondState", BluetoothDevice.BOND_BONDED);
entry.put("deviceClass", device.getBluetoothClass().getDeviceClass());
Log.d(TAG, "Discovered " + device.getAddress() + " (deviceClass: " + device.getBluetoothClass().getDeviceClass() + ")");
list.add(entry);
}

Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ android {
signingConfig signingConfigs.debug
}
}
buildToolsVersion '33.0.3'
buildToolsVersion '30.0.3'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
10 changes: 8 additions & 2 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
android.enableJetifier=true
org.gradle.jvmargs=-Xmx1536M \
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED \
--add-opens=java.base/java.lang=ALL-UNNAMED \
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED \
--add-opens=java.base/java.io=ALL-UNNAMED \
--add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.enableR8=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
3 changes: 1 addition & 2 deletions example/lib/SelectBondedDevicePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class SelectBondedDevicePage extends StatefulWidget {
}

enum _DeviceAvailability {
no,
maybe,
yes,
}
Expand All @@ -27,7 +26,7 @@ class _DeviceWithAvailability {
_DeviceAvailability availability;
int? rssi;

_DeviceWithAvailability(this.device, this.availability, [this.rssi]);
_DeviceWithAvailability(this.device, this.availability);
}

class _SelectBondedDevicePage extends State<SelectBondedDevicePage> {
Expand Down
6 changes: 3 additions & 3 deletions example/lib/helpers/LineChart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ class LineChart extends StatelessWidget {
values: values,
valuesLabels: valuesLabels,
horizontalLabelsTextStyle:
horizontalLabelsTextStyle ?? Theme.of(context).textTheme.caption,
horizontalLabelsTextStyle ?? Theme.of(context).textTheme.bodySmall,
verticalLabelsTextStyle:
verticalLabelsTextStyle ?? Theme.of(context).textTheme.caption,
verticalLabelsTextStyle ?? Theme.of(context).textTheme.bodySmall,
horizontalLinesPaint: horizontalLinesPaint,
verticalLinesPaint: verticalLinesPaint,
additionalMinimalHorizontalLabelsInterval:
Expand Down Expand Up @@ -510,7 +510,7 @@ class _LineChartPainter extends CustomPainter {
Iterator<double> argument = arguments.iterator;
while (value.moveNext()) {
argument.moveNext();
if (value.current == null || value.current == double.nan) continue;
if (value.current == null) continue;

if (argument.current < argumentsOffset) continue;
final double xOffset = padding.left +
Expand Down
2 changes: 0 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: flutter_bluetooth_serial_example
version: 0.4.0
description: Demonstrates how to use the `flutter_bluetooth_serial` plugin.
authors:
- Patryk Ludwikowski <patryk.ludwikowski.7@gmail.com>
homepage: https://github.com/edufolly/flutter_bluetooth_serial/tree/master/example/

environment:
Expand Down
1 change: 1 addition & 0 deletions lib/BluetoothConnection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BluetoothConnection {
//

/// This ID identifies real full `BluetoothConenction` object on platform side code.
// ignore: unused_field
final int? _id;

final EventChannel _readChannel;
Expand Down
29 changes: 18 additions & 11 deletions lib/BluetoothDevice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ class BluetoothDevice {
final BluetoothDeviceType type;

/// Class of the device.
//final BluetoothClass bluetoothClass // @TODO . !BluetoothClass!
//final category category // @TODO . !category!

/// Describes is device connected.
final bool isConnected;

/// Bonding state of the device.
final BluetoothBondState bondState;

/// Broadcasted friendly clas of the device.
final BluetoothDeviceClass deviceClass;

/// Tells whether the device is bonded (ready to secure connect).
@Deprecated('Use `isBonded` instead')
bool get bonded => bondState.isBonded;
Expand All @@ -34,23 +37,26 @@ class BluetoothDevice {
this.type = BluetoothDeviceType.unknown,
this.isConnected = false,
this.bondState = BluetoothBondState.unknown,
this.deviceClass = BluetoothDeviceClass.UNCATEGORIZED,
});

/// Creates `BluetoothDevice` from map.
///
/// Internally used to receive the object from platform code.
factory BluetoothDevice.fromMap(Map map) {
return BluetoothDevice(
name: map["name"],
address: map["address"]!,
type: map["type"] != null
? BluetoothDeviceType.fromUnderlyingValue(map["type"])
: BluetoothDeviceType.unknown,
isConnected: map["isConnected"] ?? false,
bondState: map["bondState"] != null
? BluetoothBondState.fromUnderlyingValue(map["bondState"])
: BluetoothBondState.unknown,
);
name: map["name"],
address: map["address"]!,
type: map["type"] != null
? BluetoothDeviceType.fromUnderlyingValue(map["type"])
: BluetoothDeviceType.unknown,
isConnected: map["isConnected"] ?? false,
bondState: map["bondState"] != null
? BluetoothBondState.fromUnderlyingValue(map["bondState"])
: BluetoothBondState.unknown,
deviceClass: map["deviceClass"] != null
? (map['deviceClass'] as int).getBluetoothDeviceClassFromValue
: BluetoothDeviceClass.UNCATEGORIZED);
}

/// Creates map from `BluetoothDevice`.
Expand All @@ -60,6 +66,7 @@ class BluetoothDevice {
"type": this.type.toUnderlyingValue(),
"isConnected": this.isConnected,
"bondState": this.bondState.toUnderlyingValue(),
'deviceClass': this.deviceClass.value,
};

/// Compares for equality of this and other `BluetoothDevice`.
Expand Down
99 changes: 99 additions & 0 deletions lib/BluetoothDeviceClass.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
part of flutter_bluetooth_serial;

enum BluetoothDeviceClass {
// Audio and Video Devices
AUDIO_VIDEO_CAMCORDER(1076),
AUDIO_VIDEO_CAR_AUDIO(1056),
AUDIO_VIDEO_HANDSFREE(1032),
AUDIO_VIDEO_HEADPHONES(1048),
AUDIO_VIDEO_HIFI_AUDIO(1064),
AUDIO_VIDEO_LOUDSPEAKER(1044),
AUDIO_VIDEO_MICROPHONE(1040),
AUDIO_VIDEO_PORTABLE_AUDIO(1052),
AUDIO_VIDEO_SET_TOP_BOX(1060),
AUDIO_VIDEO_UNCATEGORIZED(1024),
AUDIO_VIDEO_VCR(1068),
AUDIO_VIDEO_VIDEO_CAMERA(1072),
AUDIO_VIDEO_VIDEO_CONFERENCING(1088),
AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER(1084),
AUDIO_VIDEO_VIDEO_GAMING_TOY(1096),
AUDIO_VIDEO_VIDEO_MONITOR(1080),
AUDIO_VIDEO_WEARABLE_HEADSET(1028),

// Computer Devices
COMPUTER_DESKTOP(260),
COMPUTER_HANDHELD_PC_PDA(272),
COMPUTER_LAPTOP(268),
COMPUTER_PALM_SIZE_PC_PDA(276),
COMPUTER_SERVER(264),
COMPUTER_UNCATEGORIZED(256),
COMPUTER_WEARABLE(280),

// Health Devices
HEALTH_BLOOD_PRESSURE(2308),
HEALTH_DATA_DISPLAY(2332),
HEALTH_GLUCOSE(2320),
HEALTH_PULSE_OXIMETER(2324),
HEALTH_PULSE_RATE(2328),
HEALTH_THERMOMETER(2312),
HEALTH_UNCATEGORIZED(2304),
HEALTH_WEIGHING(2316),

// Peripheral Devices
PERIPHERAL_KEYBOARD(1344),
PERIPHERAL_KEYBOARD_POINTING(1472),
PERIPHERAL_NON_KEYBOARD_NON_POINTING(1280),
PERIPHERAL_POINTING(1408),

// Phone Devices
PHONE_CELLULAR(516),
PHONE_CORDLESS(520),
PHONE_ISDN(532),
PHONE_MODEM_OR_GATEWAY(528),
PHONE_SMART(524),
PHONE_UNCATEGORIZED(512),

// Toy Devices
TOY_CONTROLLER(2064),
TOY_DOLL_ACTION_FIGURE(2060),
TOY_GAME(2068),
TOY_ROBOT(2052),
TOY_UNCATEGORIZED(2048),
TOY_VEHICLE(2056),

// Wearable Devices
WEARABLE_GLASSES(1812),
WEARABLE_HELMET(1808),
WEARABLE_JACKET(1804),
WEARABLE_PAGER(1800),
WEARABLE_UNCATEGORIZED(1792),
WEARABLE_WRIST_WATCH(1796),

// Major Class Devices
IMAGING(1536),
MISC(0),
UNCATEGORIZED(7936),
NETWORKING(768);

const BluetoothDeviceClass(this.value);

final int value;
}

extension BluetoothDeviceClassEnum on int {
BluetoothDeviceClass get getBluetoothDeviceClassFromValue {
for (BluetoothDeviceClass enumValue in BluetoothDeviceClass.values) {
if (enumValue.value == this) {
return enumValue;
}
}
// Handle the case where the integer value doesn't match any enum value.
// Default value or handle as needed.
return BluetoothDeviceClass.UNCATEGORIZED;
}
}

extension BluetoothDeviceClassName on BluetoothDeviceClass {
// Get readable name of the bluetooth class
String get name => this.toString().replaceAll('BluetoothDeviceClass.', '');
}
2 changes: 1 addition & 1 deletion lib/flutter_bluetooth_serial.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
library flutter_bluetooth_serial;

import 'dart:async';
import 'dart:typed_data';
import 'dart:convert';

import 'package:flutter/services.dart';
Expand All @@ -14,3 +13,4 @@ part './BluetoothPairingRequest.dart';
part './BluetoothDiscoveryResult.dart';
part './BluetoothConnection.dart';
part './FlutterBluetoothSerial.dart';
part './BluetoothDeviceClass.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository: https://github.com/edufolly/flutter_bluetooth_serial
issue_tracker: https://github.com/edufolly/flutter_bluetooth_serial/issues

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.17.0 <3.0.0'
flutter: ">=1.17.0"

dependencies:
Expand Down