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

The getter 'address' was called on null. Bluetooth connection to a device #130

Closed
riteshghimire9090 opened this issue May 13, 2021 · 1 comment

Comments

@riteshghimire9090
Copy link

I am getting error of:
The getter 'address' was called on null. Bluetooth connection to a device

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

import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

class Obd {
BluetoothConnection connection;
String address;
String _messageBuffer = '';

void initObd(address) {
BluetoothConnection.toAddress(address).then((_connection) {
print('Connected to the device');
connection = _connection;

  connection.input.listen(_onDataReceived).onDone(() {
    print("Is Connected: " + _connection.isConnected.toString());
  });
}).catchError(
  (error) {
    print('Cannot connect, exception occured');
    print(error);
  },
);

}

void _onDataReceived(Uint8List data) {
// Allocate buffer for parsed data

int backspacesCounter = 0;
data.forEach((byte) {
  print(byte);
  if (byte == 8 || byte == 127) {
    backspacesCounter++;
  }
});
Uint8List buffer = Uint8List(data.length - backspacesCounter);
int bufferIndex = buffer.length;

// Apply backspace control character
backspacesCounter = 0;
for (int i = data.length - 1; i >= 0; i--) {
  if (data[i] == 8 || data[i] == 127) {
    backspacesCounter++;
  } else {
    if (backspacesCounter > 0) {
      backspacesCounter--;
    } else {
      buffer[--bufferIndex] = data[i];
    }
  }
}

// Create message if there is new line character
String dataString = String.fromCharCodes(buffer);
int index = buffer.indexOf(13);
if (~index != 0) {
  _messageBuffer = dataString.substring(index);
  print("////////////////////////1");
  print(index);
  print("////////////////////////");
} else {
  _messageBuffer = (backspacesCounter > 0
      ? _messageBuffer.substring(
          0, _messageBuffer.length - backspacesCounter)
      : _messageBuffer + dataString);
  print("////////////////////////2");
  print(_messageBuffer);
  print("////////////////////////");
}

}

void sendMessage(String text) async {
print(text);
text = text.trim();
print(text);

if (text.length > 0) {
  try {
    connection.output.add(utf8.encode("1"));
    await connection.output.allSent;

    print("ll");
  } catch (e) {
    print(e);

    // Ignore error, but notify state

  }
  print("Send Successfully");
}

}
}

@nightscape
Copy link
Collaborator

nightscape commented Jul 4, 2021

Duplicate of #131

@nightscape nightscape marked this as a duplicate of #130 Jul 4, 2021
@nightscape nightscape marked this as a duplicate of #131 Jul 4, 2021
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

2 participants