Skip to content

Commit c3d5a02

Browse files
authored
Merge pull request #18 from zain-noman/add-write-bytes
added function to write raw bytes to a bluetooth device
2 parents 649a7b2 + aacee3d commit c3d5a02

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

android/src/main/kotlin/com/matteogassend/bluetooth_classic/BluetoothClassicPlugin.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ class BluetoothClassicPlugin: FlutterPlugin, MethodCallHandler, PluginRegistry.R
180180
)
181181
"disconnect" -> disconnect(result)
182182
"write" -> write(result, call.argument<String>("message")!!)
183+
"writeBytes" -> {
184+
val byteArray = call.arguments as? ByteArray
185+
if (byteArray != null) {
186+
writeBytes(result, byteArray)
187+
} else {
188+
result.error("invalid_args", "Expected ByteArray as argument", null)
189+
}
190+
}
183191
else -> result.notImplemented()
184192
}
185193
}
@@ -194,6 +202,16 @@ class BluetoothClassicPlugin: FlutterPlugin, MethodCallHandler, PluginRegistry.R
194202
}
195203
}
196204

205+
private fun writeBytes(result: Result, message: ByteArray) {
206+
Log.i("write_handle", "inside writeBytes handle")
207+
if (thread != null) {
208+
thread!!.write(message)
209+
result.success(true)
210+
} else {
211+
result.error("write_bytes_impossible", "could not send message to unconnected device", null)
212+
}
213+
}
214+
197215
private fun disconnect(result: Result) {
198216
device = null
199217
android.util.Log.i("Bluetooth Disconnect", "device removed from memory")

lib/bluetooth_classic.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ class BluetoothClassic {
4747
Future<bool> write(String message) {
4848
return BluetoothClassicPlatform.instance.write(message);
4949
}
50+
51+
Future<bool> writeBytes(Uint8List message) {
52+
return BluetoothClassicPlatform.instance.writeBytes(message);
53+
}
5054
}

lib/bluetooth_classic_method_channel.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ class MethodChannelBluetoothClassic extends BluetoothClassicPlatform {
132132
.invokeMethod<bool>("write", <String, String>{"message": message});
133133
return res!;
134134
}
135+
136+
@override
137+
Future<bool> writeBytes(Uint8List message) async {
138+
var res = await methodChannel
139+
.invokeMethod<bool>("writeBytes", message);
140+
return res!;
141+
}
135142
}

lib/bluetooth_classic_platform_interface.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ abstract class BluetoothClassicPlatform extends PlatformInterface {
7070
Future<bool> write(String message) {
7171
throw UnimplementedError('write() has not been implemented.');
7272
}
73+
74+
Future<bool> writeBytes(Uint8List message) {
75+
throw UnimplementedError('writeBytes() has not been implemented.');
76+
}
7377
}

0 commit comments

Comments
 (0)