コード例 #1
0
 /// <summary>
 /// 将打印命令发送给打印机
 /// </summary>
 /// <param name="bytes"></param>
 public void Write(byte[] bytes)
 {
     try
     {
         if (null == bytes || bytes.Length <= 0)
         {
             return;
         }
         if (null == mAdapter)
         {
             mAdapter = BluetoothAdapter.DefaultAdapter;
         }
         if (null == mBtService)
         {
             mBtService = new BtService(mContext);
         }
         if (mBtService.GetState() != BtService.STATE_CONNECTED)
         {
             if (!string.IsNullOrEmpty(App.BtAddress))
             {
                 BluetoothDevice device = mAdapter.GetRemoteDevice(App.BtAddress);
                 mBtService.Connect(device);
                 return;
             }
         }
         mBtService.Write(bytes);
     }
     catch (Exception e)
     {
         //e.printStackTrace();
     }
 }
コード例 #2
0
        /// <summary>
        /// 断开连接
        /// </summary>
        public void Disconnect()
        {
            try
            {
                if (mQueue != null && mQueue.Any())
                {
                    mQueue.Clear();
                }

                if (null != mBtService)
                {
                    mBtService.Stop();
                    mBtService = null;
                }

                if (null != mAdapter)
                {
                    mAdapter = null;
                }
            }
            catch (Exception e)
            {
                //e.printStackTrace();
            }
        }
コード例 #3
0
        /// <summary>
        /// 打印队列
        /// </summary>
        public void Print()
        {
            lock (Lock)
            {
                try
                {
                    if (null == mQueue || mQueue.Count() <= 0)
                    {
                        return;
                    }
                    if (null == mAdapter)
                    {
                        mAdapter = BluetoothAdapter.DefaultAdapter;
                    }
                    if (null == mBtService)
                    {
                        mBtService = new BtService(mContext);
                    }

                    //没有连接时,重新连接
                    if (mBtService.GetState() != BtService.STATE_CONNECTED)
                    {
                        if (!string.IsNullOrEmpty(App.BtAddress))
                        {
                            BluetoothDevice device = mAdapter.GetRemoteDevice(App.BtAddress);
                            //连接
                            mBtService.Connect(device);

                            return;
                        }
                    }

                    while (mQueue.Count() > 0)
                    {
                        mBtService.Write(mQueue[0]);
                        mQueue.RemoveAt(0);
                    }
                }
                catch (Exception)
                {
                    //e.printStackTrace();
                }
            }
        }
コード例 #4
0
            public override void Run()
            {
                //Log.i(TAG, "BEGIN mConnectedThread");

                //缓冲
                byte[] buffer = new byte[1024];
                int    bytes;

                // 连接时继续收听InputStream
                while (true)
                {
                    try
                    {
                        // 从输入流读取
                        bytes = mmInStream.Read(buffer);
                        // 发送数据
                        //EventBus.getDefault().post(new BtMsgReadEvent(bytes, buffer));
                    }
                    catch (IOException e)
                    {
                        //Log.e(TAG, "disconnected", e);

                        ConnectionLost();

                        // 重新启动服务以重新启动侦听模式
                        BtService.Start();

                        break;
                    }
                    catch (Exception)
                    {
                        ConnectionLost();
                        BtService.Start();
                        break;
                    }
                }
            }