カテゴリー
RISC-V

Arm時代が

暫く続くと思ったらRISC-Vにが来ていた。

カテゴリー
Instagram

Every Circuitで振幅偏移変調多重化通信回路をシミュレーションしてみた

カテゴリー
Instagram

パソコンから出力した音声信号から10KHz付近のみバンドパスして全波整流してみた。#電子工作

カテゴリー
Flutter

FlutterAppとWebViewで交信してみた

        function executeJavascript() {
            return "Execute JavaScript."
        }
        function sendFlutterApp() {
            flutterJavaScript.postMessage("Post message from JavaScript.");
        }
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Javascript Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Javascript Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  late WebViewController _controller;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: WebView(
        onWebViewCreated: (WebViewController webViewController) async {
          _controller = webViewController; // 生成されたWebViewController情報を取得する
        },
        initialUrl: 'https://mymyser.com/flutterjs.html',
        javascriptMode: JavascriptMode.unrestricted,
        javascriptChannels: {
          JavascriptChannel(
            name: 'flutterJavaScript',
            onMessageReceived: (result) async {
              print(result.message);
            },
          )
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _executeJavascript,
      ),

    );
  }

  Future<void> _executeJavascript() async {
    String result = await _controller.runJavascriptReturningResult('executeJavascript()');
    print(result);
  }
}
カテゴリー
Flutter

FlutterWebとNativeAppで交信してみた

    function js2Dart(message) {
      console.log(message);
      window.postMessage(message, "*");
    }

    function js2Swift(message) {
      console.log(message);
      webkit.messageHandlers.js2Swift.postMessage(message);
    }

    function js2Java(message) {
      console.log(message);
      Android.js2Java(message);
    }

    function js2Native(message) {
      console.log(message);
      const ua = navigator.userAgent
      if (/android/i.test(ua)) {
        js2Java(message)
      }
      else if (/iPad|iPhone|iPod/.test(ua)) {
        js2Swift(message)
      }
    }
window.addEventListener("message", (event) {
    _incrementCounter();
});
 void _dart2Js() {
    _incrementCounter();
    setState(() {
      context.callMethod('js2Native', ['dart2Js']);
    });
 }

 floatingActionButton: FloatingActionButton(
     onPressed: _dart2Js,
     tooltip: 'Increment',
     child: const Icon(Icons.add),
 ),
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        webView = binding.webView;
        webView.addJavascriptInterface(new WebAppInterface(getContext()), "Android");
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://demo.mymyser.com/index.html");
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.loadUrl("javascript:postMessage('Send Native Message.');");
            }
        });
    }
    @JavascriptInterface
    public void js2Java(String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
        let config = WKWebViewConfiguration()
        let userContentController: WKUserContentController = WKUserContentController()
        userContentController.add(self, name: "js2Swift")
        config.userContentController = userContentController

        webView = WKWebView(
            frame: CGRect(x: 0, y: 64,
                          width: view.frame.size.width,
                          height: view.frame.size.height-64),
            configuration: config
        )
    @IBAction func onClickButton (_ sender: UIButton) {
        webView?.evaluateJavaScript("js2Dart('Send Native Message');", completionHandler: { (object, error) -> Void in
            print("⭐️:Send Native Message")
        })
    }
extension ViewController: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if(message.name == "js2Swift") {
             print("⭐️: \(message.body)")
        }
    }
}