第一个代码,可以编译,但不能按预期工作:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:location_permissions/location_permissions.dart';
class BluetoothConnectionScreen extends StatefulWidget {
const BluetoothConnectionScreen({super.key});
@override
State<BluetoothConnectionScreen> createState() => _BluetoothConnectionScreenState();
}
class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
final flutterReactiveBle = FlutterReactiveBle();
late DiscoveredDevice _myDevice;
late StreamSubscription<DiscoveredDevice> _scanStream;
late QualifiedCharacteristic _rxCharacteristic;
final Uuid serviceUuid = Uuid.parse("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
final Uuid characteristicUuid = Uuid.parse("beb5483e-36e1-4688-b7f5-ea07361b26a8");
late List<int> _infoCharacteristics;
bool _foundDeviceWaitingToConnect = false;
bool _scanStarted = false;
bool _connected = false;
void _startScan() async {
bool permGranted = false;
setState(() {
_scanStarted = true;
});
PermissionStatus permission;
permission = await LocationPermissions().requestPermissions();
if (permission == PermissionStatus.granted) permGranted = true;
if (permGranted) {
_scanStream = flutterReactiveBle
.scanForDevices(withServices: [serviceUuid]).listen((device) {
if (device.name.startsWith("L")) {
setState(() {
_myDevice = device;
_foundDeviceWaitingToConnect = true;
});
}
});
}
}
void _connectToDevice() {
_scanStream.cancel();
Stream<ConnectionStateUpdate> _currentConnectionStream = flutterReactiveBle
.connectToAdvertisingDevice(
id: _myDevice.id,
prescanDuration: const Duration(seconds: 5),
withServices: [serviceUuid, characteristicUuid]);
_currentConnectionStream.listen((event) {
switch (event.connectionState) {
case DeviceConnectionState.connected:
{
_rxCharacteristic = QualifiedCharacteristic(
serviceId: serviceUuid,
characteristicId: characteristicUuid,
deviceId: event.deviceId);
setState(() {
_foundDeviceWaitingToConnect = false;
_connected = true;
});
break;
}
case DeviceConnectionState.disconnected:
{
setState(() {
_foundDeviceWaitingToConnect = true;
_connected = false;
});
break;
}
default:
}
});
}
void _readFromDevice() async
{
if (_connected) {
setState(() async {
_infoCharacteristics.addAll(await flutterReactiveBle.readCharacteristic(_rxCharacteristic));
});
}
}
@override
Widget build(BuildContext context) {
_infoCharacteristics.addAll([1,2,3]);
return Scaffold(
appBar: AppBar(
title: const Text("BLE"),
),
body: SafeArea(child:
Center(child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Started scan: $_scanStarted"),
ElevatedButton.icon(
icon: const Icon(
Icons.bluetooth,
color: Colors.yellow,
size: 24.0,
),
onPressed: () => _startScan(),
label: const Text("Find and pair BLE device"),
style: ElevatedButton.styleFrom(
alignment: Alignment.centerLeft,
fixedSize: const Size(
290,
32,
),
),
),
Text("Device found? $_foundDeviceWaitingToConnect"),
Text("Scan started? $_scanStarted"),
Text("Connected? $_connected"),
_connected ? Text("OK! Device name: ${_myDevice.id.toString()}") : Text("Device not connected!"),
ElevatedButton.icon(
icon: const Icon(
Icons.bluetooth,
color: Colors.yellow,
size: 24.0,
),
onPressed: () => _connectToDevice,
label: const Text("Connect to BLE device"),
style: ElevatedButton.styleFrom(
alignment: Alignment.centerLeft,
fixedSize: const Size(
290,
32,
),
),
),
ElevatedButton.icon(
icon: const Icon(
Icons.bluetooth,
color: Colors.yellow,
size: 24.0,
),
onPressed: () => _readFromDevice,
label: const Text("Read information"),
style: ElevatedButton.styleFrom(
alignment: Alignment.centerLeft,
fixedSize: const Size(
290,
32,
),
),
),
_connected ? Text("OK ! $_infoCharacteristics") : Text("Device not connected!"),
],
))));
}
}
我的Ffltter应用程序可以编译,但不能正常工作.它通过UUID找到设备,但无法连接到它.不知道为什么.最后,我想从我的设备中读取特征(用字符串表示的文本).欢迎任何建议!谢谢.
我添加了所有需要的应用程序权限(在Android Manifest文件中),这个应用程序已经在Android OS 11中授予了所有需要的权限.当然,我已经在真实的设备上测试过它,手机.
我该怎么办?
日志(log)记录:
2023-04-28 22:15:17.784 10350-16111 flutter com.example.my_app I[38;5;12米之举─
10350-16111.我的应用程序I[38;5;12M│#0_BluToothConnectionScreenState._StartScan ScreenState._Start
10350-16111.我的应用程序I[38;5;12M│#1_BluToothConnectionScreenState.Build.(package:my_app/Screens/BluetoothConnection/BluetoothPairingScreen.dart:128:26)[0m
10350-16111Flutter 通信例如:my_app I[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄[0m
10350-28 22:15:17.785 10350-16111Flutter 通信.例如,my_app I[38;5;12M│?设备扫描已启动[0M
2023-04-28 22:15:17.785 10350-16111 flutter com.example.my_app I[38;5;12m回过头了─
2023-04-28 22:15:17.797 10350-16111 flutter com.example.my_app I[38;5;12米之举─
10350-16111.我的应用程序I[38;5;12M│#0_BluToothConnectionScreenState._StartScan ScreenState._Start
10350-16111Flutter 通信例如My_APP I[38;5;12M│#1[0M
10350-16111Flutter 通信例如:my_app I[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄[0m
10350-16111Flutter 通信例如:My_APP I[38;5;12 M│?许可已批准[0 M 10350-16111Flutter 通信例如:my_app I[38;5;12m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[0m
2023-04-28 22:15:17.797 10350-16111 flutter com.example.my_app I[38;5;12米之举─
10350-16111.我的应用程序I[38;5;12M│#0_BluToothConnectionScreenState._StartScan ScreenState._Start
10350-16111Flutter 通信例如My_APP I[38;5;12M│#1[0M
10350-16111Flutter 通信例如:my_app I[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄[0m
10350-16111Flutter 通信例如:my_app I[38;5;12M│?Device Found:FALSE[0M
2023-04-28 22:15:17.797 10350-16111 flutter com.example.my_app I[38;5;12m回过头了─
10350-16177蓝牙适配器com.example.my_app D is LeEnabled():ON
10350-16177蓝牙适配器com.example.my_app D is LeEnabled():ON
10350-16191蓝牙 scanner .my_app D onScanerRegisted()-Status=0 scannerID=10 mScanerID=0
2023-04-28 22:15:17.815 2029-7327 AppOps PID-2029年UID 1002发出错误呼叫.包"com.example.my_app"不属于uid 1002.