Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.kkipn.bluetoothchatfinal;
- /**
- * Created by kkipn on 17/03/2018.
- */
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class MyConnectedThread
- extends Thread
- {
- String TAG = "ConnectedThread";
- Context mContext;
- private final InputStream mmInStream;
- private final OutputStream mmOutStream;
- private final BluetoothSocket mmSocket;
- BluetoothDevice remoteDevice;
- public MyConnectedThread(BluetoothSocket paramBluetoothSocket, BluetoothDevice paramBluetoothDevice,
- Context paramContext)
- {
- Log.d(this.TAG, "create ConnectedThread");
- this.mmSocket = paramBluetoothSocket;
- this.remoteDevice = paramBluetoothDevice;
- this.mContext = paramContext;
- InputStream localInputStream = null;
- //added declaration
- OutputStream localOutputStream1 =null;
- try
- {
- localInputStream = paramBluetoothSocket.getInputStream();
- OutputStream localOutputStream2 = paramBluetoothSocket.getOutputStream();
- localOutputStream1 = localOutputStream2;
- }
- catch (IOException localIOException)
- {
- for (;;)
- {
- Log.e(this.TAG, "temp sockets not created", localIOException);
- //OutputStream localOutputStream1 = null;
- }
- }
- this.mmInStream = localInputStream;
- this.mmOutStream = localOutputStream1;
- }
- public void cancel()
- {
- try
- {
- this.mmSocket.close();
- return;
- }
- catch (IOException localIOException)
- {
- Log.e(this.TAG, "close() of connect socket failed", localIOException);
- }
- }
- public void run()
- {
- Log.i(this.TAG, "BEGIN mConnectedThread");
- byte[] arrayOfByte = new byte[2048];
- try
- {
- for (;;)
- {
- String str = new String(arrayOfByte, 0, this.mmInStream.read(arrayOfByte));
- MyMessageBean localMyMessageBean = new MyMessageBean();
- localMyMessageBean.setContent(str);
- localMyMessageBean.setInMesage(true);
- localMyMessageBean.setReceiverName("");
- localMyMessageBean.setSenderAddr(this.remoteDevice.getAddress());
- Intent localIntent2 = new Intent("com.example.kkipn.bluetoothchatfinal.MESSAGE_READ");
- localIntent2.putExtra("value", localMyMessageBean);
- this.mContext.sendBroadcast(localIntent2);
- }
- return;
- }
- catch (IOException localIOException)
- {
- Log.e(this.TAG, "disconnected", localIOException);
- Intent localIntent1 = new Intent("liu.niu.soft.bluetoothchatsingle.ACTION_CONNECT_LOST");
- localIntent1.putExtra("value", this.remoteDevice.getAddress());
- this.mContext.sendBroadcast(localIntent1);
- }
- }
- public void write(byte[] paramArrayOfByte)
- {
- try
- {
- this.mmOutStream.write(paramArrayOfByte);
- String str = new String(paramArrayOfByte, 0, paramArrayOfByte.length);
- Log.d("ConnectedThread write:", str);
- MyMessageBean localMyMessageBean = new MyMessageBean();
- localMyMessageBean.setContent(str);
- localMyMessageBean.setInMesage(false);
- localMyMessageBean.setReceiverName(this.remoteDevice.getAddress());
- localMyMessageBean.setSenderAddr("");
- Intent localIntent = new Intent("liu.niu.soft.bluetoothchatsingle.MESSAGE_WRITE");
- localIntent.putExtra("value", localMyMessageBean);
- this.mContext.sendBroadcast(localIntent);
- return;
- }
- catch (IOException localIOException)
- {
- Log.e(this.TAG, "Exception during write", localIOException);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement