//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import com.nttdocomo.device.BTStateListener;
import com.nttdocomo.device.BluetoothException;
import com.nttdocomo.device.DeviceException;
import com.nttdocomo.device.RemoteDevice;
import com.nttdocomo.io.SPPConnection;
import java.io.InputStream;
import java.io.OutputStream;

public class btMgr implements BTStateListener {
    private Module parent;
    public RemoteDevice m_dev = null;
    private SPPConnection m_con = null;
    private InputStream m_in = null;
    private OutputStream m_out = null;
    boolean recv_stop = false;
    boolean regiLoop = false;
    boolean connectF = false;
    boolean scan_flg = false;
    boolean statClose = false;
    byte[] write_dat = new byte[16];
    boolean write_flg = false;
    byte[] read_dat = new byte[16];
    boolean read_flg = false;

    public btMgr(Module var1) {
        this.parent = var1;
    }

    public void doClose4() throws Exception {
        try {
            if (this.m_dev != null) {
                this.m_dev.dispose();
                this.m_dev = null;
            }
        } catch (BluetoothException var2) {
        } catch (DeviceException var3) {
        } catch (RuntimeException var4) {
        } catch (Throwable var5) {
        }

        this.regiLoop = false;
    }

    public void doClose3() throws Exception {
        try {
            if (this.m_con != null) {
                this.m_con.close();
                this.m_con = null;
            }
        } catch (BluetoothException var2) {
        } catch (DeviceException var3) {
        } catch (RuntimeException var4) {
        } catch (Throwable var5) {
        }

        this.regiLoop = false;
    }

    public void doClose2() throws Exception {
        try {
            this.recv_stop = true;
            if (this.m_in != null) {
                this.m_in.close();
            }

            if (this.m_out != null) {
                this.m_out.close();
            }

            if (this.m_con != null) {
                this.m_con.close();
            }
        } catch (BluetoothException var2) {
        } catch (DeviceException var3) {
        } catch (RuntimeException var4) {
        } catch (Throwable var5) {
        }

        this.regiLoop = false;
    }

    public boolean doClose5() throws Exception {
        try {
            if (!this.read_flg && this.m_in != null) {
                this.m_in = null;
            }

            if (this.m_out != null) {
                this.write_flg = false;
                this.m_out = null;
            }
        } catch (Exception var2) {
        }

        this.regiLoop = false;
        return this.m_in == null && this.m_out == null;
    }

    public boolean registerDevice2(RemoteDevice var1, boolean var2) {
        this.connectF = var2;
        this.m_dev = var1;
        this.m_con = null;
        this.regiLoop = true;
        (new Thread() {
            public void run() {
                boolean var1 = false;
                btMgr.this.scan_flg = true;

                do {
                    if (btMgr.this.regiLoop) {
                        try {
                            if (btMgr.this.m_dev != null) {
                                if (btMgr.this.connectF) {
                                    btMgr.this.m_con = (SPPConnection)btMgr.this.m_dev.connect(0);
                                } else {
                                    btMgr.this.m_con = (SPPConnection)btMgr.this.m_dev.accept(0);
                                }
                            }

                            if (btMgr.this.m_con != null) {
                                var1 = true;
                            }
                        } catch (Exception var3) {
                            if (btMgr.this.m_dev != null) {
                                btMgr.this.m_dev.dispose();
                                btMgr.this.m_dev = null;
                            }

                            var1 = true;
                        }
                    }
                } while(!var1 && btMgr.this.regiLoop);

                btMgr.this.scan_flg = false;
                btMgr.this.regiLoop = false;
            }
        }).start();
        return true;
    }

    public int registerDevice(RemoteDevice var1, boolean var2) {
        if (this.m_dev == null) {
            return -1;
        } else if (this.m_con == null) {
            return 0;
        } else {
            this.m_con.setBTStateListener(this);
            this.statClose = false;
            return !this.doOpenStream() ? -1 : 1;
        }
    }

    public boolean doOpenStream() {
        boolean var1 = false;

        try {
            this.m_in = this.m_con.openInputStream();
            this.m_out = this.m_con.openOutputStream();
            this.read_flg = false;
            this.recv_stop = false;
            this.doRecv2();
            this.write_flg = false;
            Module var10000 = this.parent;
            var10000.btStat |= 16;
            var1 = true;
        } catch (Throwable var3) {
            this.m_in = null;
            this.m_out = null;
        }

        return var1;
    }

    public void doSend(byte[] var1) {
        this.parent.btWriteErr = false;
        if (this.m_out != null && !this.write_flg) {
            for(int var2 = 0; var2 < 16; ++var2) {
                this.write_dat[var2] = var1[var2];
            }

            this.write_flg = true;

            try {
                (new Thread() {
                    public void run() {
                        if (btMgr.this.m_out != null && (btMgr.this.parent.btStat & 32) == 0 && !btMgr.this.recv_stop) {
                            try {
                                btMgr.this.m_out.write(btMgr.this.write_dat);
                                ++btMgr.this.parent.btSendCnt;
                            } catch (Exception var2) {
                                Module var10000 = btMgr.this.parent;
                                var10000.btStat |= 96;
                                btMgr.this.write_flg = false;
                            }
                        }

                        btMgr.this.write_flg = false;
                    }
                }).start();
            } catch (Exception var3) {
                Module var10000 = this.parent;
                var10000.btStat |= 96;
                this.write_flg = false;
            }
        } else {
            this.parent.btWriteErr = true;
        }

    }

    public void doRecv2() {
        try {
            (new Thread() {
                public void run() {
                    boolean var1 = false;

                    while(true) {
                        btMgr.this.read_flg = true;
                        if (btMgr.this.recv_stop || btMgr.this.m_in == null) {
                            break;
                        }

                        try {
                            if (btMgr.this.m_in != null) {
                                if ((btMgr.this.parent.btStat & 32) != 0) {
                                    break;
                                }

                                boolean var2 = false;
                                int var5 = btMgr.this.m_in.read(btMgr.this.read_dat);
                                if (btMgr.this.parent.btRecv2Size < var5) {
                                    btMgr.this.parent.btRecv2Size = var5;
                                }

                                if (var5 > 0) {
                                    btMgr.this.parent.btRecvBuf[btMgr.this.parent.btRecvCnt][0] = -1;
                                    btMgr.this.parent.btRecvBuf[btMgr.this.parent.btRecvCnt][15] = -1;
                                    if (var5 >= 16) {
                                        var5 = 16;
                                    }

                                    for(int var3 = 0; var3 < var5; ++var3) {
                                        btMgr.this.parent.btRecvBuf[btMgr.this.parent.btRecvCnt][var3] = btMgr.this.read_dat[var3];
                                    }

                                    btMgr.this.parent.btRecvTop2 = btMgr.this.parent.btRecvCnt;
                                    btMgr.this.parent.btRecvCnt = btMgr.this.parent.btRecvCnt + 1 & 15;
                                    ++btMgr.this.parent.btRecv2Cnt;
                                }
                            }
                        } catch (Exception var4) {
                            Module var10000 = btMgr.this.parent;
                            var10000.btStat |= 96;
                        }
                    }

                    btMgr.this.read_flg = false;
                }
            }).start();
        } catch (Exception var2) {
            this.read_flg = false;
            Module var10000 = this.parent;
            var10000.btStat |= 96;
        }

    }

    public void doRecv() {
    }

    public void stateChanged(int var1) {
        if (var1 == 0) {
            this.statClose = true;
            Module var10000 = this.parent;
            var10000.btStat |= 32;
        }

    }
}
