我使用此代碼通過藍牙發送資料,我有兩個問題。
首先,我收到此錯誤訊息,我不知道如何修復,錯誤是這樣的:
“引數型別'List'不能分配給引數型別'Uint8List'”。
這是代碼:
void _sendMessage(String value1) async {
value1 = value1.trim();
if(value1.length >0){
try{
connection.output.add(utf8.encode(value1));
await connection.output.allSent;
}catch(e){
setState(() { });
}
}
}
編譯器將utf8.encode(value1)突出顯示為錯誤
我的第二個問題是如何更改此代碼以發送整數而不是字串
uj5u.com熱心網友回復:
像這樣的初始化
//send string
void _sendMessageString(String value1) async {
value1 = value1.trim();
if (value1.length > 0) {
try {
List<int> list = value1.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
connection.output.add(bytes);
await connection.output.allSent;
} catch (e) {
setState(() {});
}
}
}
//send int
void _sendMessageInt(int value1) async {
try {
Uint8List bytes = Uint8List(value1);
connection.output.add(bytes);
await connection.output.allSent;
} catch (e) {
setState(() {});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478655.html
