1

Bluetooth モジュールからデータをスキャン、接続、受信しようとしています。Android アプリケーションを使用するだけであれば、すべて正常に動作します。近くのすべてのデバイスをスキャンして見つけ、誰にでも接続し (Bluetooth モジュールにのみ関心があります)、Bluetooth モジュールから送信されているテスト データを読み取ることができます。

問題は、アプリケーションが Flutter を使用して開発されていることです。Android アプリケーションから同じコードを使用し、EventsChannel を介して Dart にリンクしましたが、Flutter アプリで表示される Bluetooth デバイスの数が少なくなり、関心のある Bluetooth モジュールはどれもありません。 Flutter とプラットフォーム固有のコーディング。同じハードウェア上の異なるアプリで同じコードが異なる動作をする理由がわかりません。

コードを Samsung 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);
            }
        }
    };
}

最後のスニペットは気にしませんが、完全なフローを示したいと思いました。スニペット 2 は、スタンドアロンの Android アプリにあるもののコピーであり、すべてのデバイスをスキャンして検出しますが、Flutter アプリで Android のネイティブ コードとして使用すると、同じ数のデバイスが検出されなくなり、まだいくつかのデバイスが検出されます。ただし、非常に信頼性が低いです。

ほとんどの Flutter bluetooth パッケージを試しましたが、探していたものはなかったので、プラットフォーム固有のコードを使用することになりました。これは、Flutter に接続されるまで問題なく動作しました。Android 開発のドキュメントを読みましたが、上記のコードはほとんどが Android サンプルから変更されたコードです。最後に同じハードウェアでテストされている場合、同じコードをフラッターアプリケーションのネイティブコードとして使用するのではなく、スタンドアロンアプリとしてより多くのデバイスを見つけることができる理由がわかりません。

どんな入力でも大歓迎です!

4

1 に答える 1