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

Message other than String Type #12

Closed
kidzen opened this issue Dec 22, 2018 · 5 comments
Closed

Message other than String Type #12

kidzen opened this issue Dec 22, 2018 · 5 comments

Comments

@kidzen
Copy link

kidzen commented Dec 22, 2018

Base on this portion of code,

class FlutterBluetoothSerial {
    ...
    Future<dynamic> write(String message) => _channel.invokeMethod('write', {'message': message});
    ...
}

message can only be submitted through String type..

Just to give you a use case, currently i am trying to make use of this plugin to sent byte data to a bluetooth printer. In native android, write method are represent in byte[] type if im not mistaken..

@edufolly
Copy link
Owner

Hi @kidzen,

I believe that is not difficult but at this moment I don't have the environment to make tests.

import 'dart:typed_data';
class FlutterBluetoothSerial {
  ...
  Future<dynamic> writeBytes(Uint8List message)  =>
      _channel.invokeMethod('writeBytes', {'message': message});
  ...
}
public class FlutterBluetoothSerialPlugin ...
  ...
  public void onMethodCall ... {
    ...
            case "writeBytes":
                if (arguments.containsKey("message")) {
                    byte[] message = (byte[]) arguments.get("message");
                    write(result, message);
                } else {
                    result.error("invalid_argument", "argument 'message' not found", null);
                }
                break;
   ...
  }
  ...
    private void write(Result result, byte[] message) {
        if (THREAD == null) {
            result.error("write_error", "not connected", null);
            return;
        }
        try {
            THREAD.write(message);
            result.success(true);
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage(), ex);
            result.error("write_error", ex.getMessage(), exceptionToString(ex));
        }
    }

    private void write(Result result, String message) {
        write(result, message.getBytes());
    }
  ...
}

We can use this example as base code. ;-)

Maybe @rafaelterada could help us with tests and PR.

Best.

@kidzen
Copy link
Author

kidzen commented Dec 24, 2018

tq @edufolly for your fast response...i will try it out and post the result back to u...tq so much!

@rafaelterada
Copy link
Contributor

Hi, I made the changes, however I can not test passing byte []. Can you do the tests and not give a return?
Test with this:
https://github.com/rafaelterada/flutter_bluetooth_serial/tree/patch-1

@edufolly
Copy link
Owner

edufolly commented Feb 5, 2019

Hi @rafaelterada,

could you provide a PR with these features?

@edufolly
Copy link
Owner

Hi folks,

PR was merged.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants