3***@qq.com
3***@qq.com
  • 发布:2018-12-04 20:57
  • 更新:2022-09-23 13:48
  • 阅读:3335

Android 调用原生socket

分类:uni-app

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

网上查阅了一下,关于相关方面的案例略少,借鉴了本论坛中一贴中的测试案例,在此表示感谢。CTRL+C +V 实现了demo app的socket的通讯。
也是使用java nio的轮询io非阻塞式来实现io读写。
本案例是测试demo,有什么不足,请各位指教,谢谢。

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);  
    }  
}
3 关注 分享
金易 j***@sina.com redstoone

要回复文章请先登录注册

2***@qq.com

2***@qq.com

回复 z***@163.com :
有解决吗
2022-09-23 13:48
z***@163.com

z***@163.com

回复 everyoo :
你好,我也碰到这个问题,解决了么?
2020-09-18 12:02
everyoo

everyoo

var keys = selector.selectedKeys();
plus.android.importClass(keys); 这里报错怎么解
2020-08-05 10:13
n***@qq.com

n***@qq.com

make
2019-06-07 00:32
8***@qq.com

8***@qq.com

make
2019-03-03 12:05
FYC

FYC

学习了
2019-01-03 16:40