uni-app 调用原生socket实现与WiFi设备的直连通讯。

var inetSocketAddress = plus.android.importClass("java.net.InetSocketAddress");  
var ByteBuffer = plus.android.importClass("java.nio.ByteBuffer");  
var SelectionKey = plus.android.importClass("java.nio.channels.SelectionKey");  
var Selector = plus.android.importClass("java.nio.channels.Selector");  
var SocketChannel = plus.android.importClass("java.nio.channels.SocketChannel");  
var Iterator = plus.android.importClass("java.util.Iterator");  
var An_String = plus.android.importClass("java.lang.String");  
 
var StrictMode = plus.android.importClass("android.os.StrictMode");  
var Build = plus.android.importClass("android.os.Build");  
if (Build.VERSION.SDK_INT > 9) {  
    var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  
    StrictMode.setThreadPolicy(policy);  
}  
 
var An_Selector = new Selector();  
var An_SocketChannel = new SocketChannel();  
var An_SelectionKey = new SelectionKey();  
 
var selector = null;  
var channel = null;  
var stop = false;   //进程控制  
 
try {  
 
    channel = An_SocketChannel.open();  
    //设置通道为非阻塞  
    channel.configureBlocking(false);  
    selector = An_Selector.open();  
    channel.connect(new inetSocketAddress("192.168.2.245", 6000));  
    channel.register(selector, An_SelectionKey.OP_CONNECT);  
 
    while (!stop) {  
        selector.select(1000);  
 
        var keys = selector.selectedKeys();  
        plus.android.importClass(keys);  
        var ite = keys.iterator();  
        plus.android.importClass(ite);  
 
        while (ite.hasNext()) {  
            An_SelectionKey = ite.next();  
            ite.remove();  
 
            if (An_SelectionKey.isConnectable()) {  
                if (channel.isConnectionPending()) {  
                    if (channel.finishConnect()) {  
                        if (channel.isConnected()) {  
                            // 设置成非阻塞  
                            channel.configureBlocking(false);  
                            //在和服务端连接成功之后,为了可以接收到服务端的信息,需要给通道设置读的权限。  
                            channel.register(selector, An_SelectionKey.OP_READ);  
                        }  
                    }  
                }  
            } else if (An_SelectionKey.isReadable()) {  
                // 检测到服务端的数据  
                var buffer = ByteBuffer.allocate(100);  
                var ret = channel.read(buffer);  
                if (ret == -1) {  
                    stop = true;  
                    console.log("server is unconnected");  
                } else {  
                    var data = buffer.array();  
                    var msg = new An_String(data).trim();  
                    console.log("client recive msg : " + msg);  
                    // 发送数据给客户端  
                    var data = new An_String("Im app " + msg);  
                    var outBuffer = new ByteBuffer.wrap(data.getBytes());  
                    channel.write(outBuffer);  
                    if ("by" == msg) {  
                        stop = true  
                    }  
                }  
            }  
        }  
    }  
} catch (e) {  
    console.log("run error : " + e)  
} finally {  
    try {  
        channel.close();  
    } catch (e) {  
        console.log("close error " + e);  
    }  
}

玩咖指针 2020-09-12 14:40:55