蓝牙设备可以在安卓应用程序中找到,但不能在Flutter中的特定平台代码中找到



我正在尝试扫描、连接并接收来自蓝牙模块的数据。如果我只是使用安卓应用程序,一切都很好。我可以扫描并找到附近的所有设备,连接到任何人(我只对我的蓝牙模块感兴趣(,并且我能够读取蓝牙模块发送的测试数据。

问题是该应用程序是使用Flutter开发的。我使用了安卓应用程序中的相同代码,并通过EventsChannel将其与Dart链接,但现在我在Flutter应用程序中只能看到更少的蓝牙设备,而且这些设备都不是我感兴趣的蓝牙模块。我对Flutter和特定平台的编码是新手,我不明白为什么相同的代码在相同硬件上的不同应用程序中表现不同。

我在三星S4和S8手机上测试了我的代码,结果是一样的。

这是发现部分的EventChannel代码:

new EventChannel(flutterEngine.getDartExecutor(), DISCOVER_CHANNEL).setStreamHandler(
new EventChannel.StreamHandler() {
@Override
public void onListen(Object args, EventChannel.EventSink events) {
Log.w(TAG, "Registering receiver");
discoverReceiver = DiscoverReceiver(events);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(discoverReceiver, filter);
}
@Override
public void onCancel(Object args) {
Log.w(TAG, "Unregistering receiver");
unregisterReceiver(discoverReceiver);
discoverReceiver = null;
}
}
);

目前,我的discoverReceiver是一个全局BroadcastReceiver。以下是Broadcastreceiver的代码:

private BroadcastReceiver DiscoverReceiver(final EventSink events) {
return new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
if (deviceName == null){
deviceName = "No Device Name";
}
events.success(deviceName);
Log.w(TAG, "Sending " + deviceName);
} 
}
};
}

**我使用了(Log.w(TAG,"Sending"+deviceName(;(语句,查看事件是否丢失/丢弃。下面是我在飞镖中收到它的方式:

@override
void initState() {
super.initState();
devices.add(selectDevice);
discoverChannel.receiveBroadcastStream().listen(onEvent);
}
void onEvent(Object event) {
setState(() {
devices.add(event);
});
}

以下是我的Android应用程序中的代码,它可以扫描并找到所有设备,以防你想与上面的设备进行比较:

private BroadcastReceiver DiscoverReceiver() {
return new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
if (deviceName == null){
deviceName = "No Device Name";
}
devicelist.add(device);
devNames.add(deviceName);
arrayAdapter.add(deviceName);
arrayAdapter.notifyDataSetChanged();
Log.w(TAG, "Sending " + deviceName);
}
}
};
}

我不关心最后一个片段,但我只是想展示完整的流程。Snippet 2是我在一个独立的Android应用程序中的副本,它扫描并找到所有设备,但一旦我在Flutter应用程序中使用它作为Android的本地代码,它就会停止找到相同数量的设备,但仍然会找到一些,而且非常不可靠。

我尝试过大多数Flutter蓝牙软件包,但没有一个是我想要的,所以我最终使用了特定于平台的代码,在插入Flutter之前,这些代码一直很好。我已经阅读了Android开发的文档,上面的代码大多是从Android示例中修改的代码。我只是不明白为什么如果最终在同一硬件上进行测试,那么同一代码作为独立应用程序可以找到更多的设备,而不是将其作为flutter应用程序的本地代码。

如有任何意见,我们将不胜感激!

好的,我终于找到了解决方案。这与蓝牙的startDiscovery((的运行和正常工作有关,但事件会在稍后捕获,这是调试器无法显示的。

因此,在我的情况下,所有设备都被"发现"了,但flutter应用程序稍后开始捕捉事件,因此它只显示在发现过程中发现的最后1或2个设备。

我搬家了:

discoverChannel.receiveBroadcastStream().listen(onEvent);

从initState((到按下按钮后调用的函数,该函数确保在本地端注册广播接收器和在Dart上注册广播流接收器之前,所有内容都已加载。

我仍然不确定如何表达这一点,但这是关于在本机端注册BroadcastReceiver和在Dart端注册receiveBroadcastStream的时间。

现在,它开始发现并正确捕获事件,这显示了在Android独立应用程序中发现的相同数量的设备。

希望这对将来可能面临这个奇怪问题的人有所帮助。

相关内容

最新更新