GVKun编程网logo

使用x64dbg分析微信聊天函数并实现发信息

13

对于想了解使用x64dbg分析微信聊天函数并实现发信息的读者,本文将是一篇不可错过的文章,并且为您提供关于Android蓝牙通信聊天实现发送和接受功能、Gitee推荐|Windows开源调试器x64d

对于想了解使用x64dbg分析微信聊天函数并实现发信息的读者,本文将是一篇不可错过的文章,并且为您提供关于Android蓝牙通信聊天实现发送和接受功能、Gitee 推荐 | Windows 开源调试器 x64dbg、Gitee 推荐 | x64dbg 自动化控制插件 LyScript、HTML5高仿微信聊天、微信聊天表情、对话框、编辑器功能的有价值信息。

本文目录一览:

使用x64dbg分析微信聊天函数并实现发信息

使用x64dbg分析微信聊天函数并实现发信息

1.引言

我们知道微信现在不光在手机上很常用,在电脑也是非常常用的,尤其是使用微信联系客户和维护群的人,那这个时候每天都会定期发送一些信息,如果人工操作会很累,所以自动化工具是王道,本节就使用x64dbg让你看看怎么完成发消息。本节完整源码在github:https://github.com/15pb/wechat_tools

2. 网络协议分析模型

首先我们讲一些理论知识,在开发中,网络通信的协议最常见的有tcp,udp,http等,而在通信时使用的模型(这里的模型说的是代码执行方式)也不尽相同,我们在分析微信协议的时候第一要确定微信是什么通信协议,第二要确定使用的是哪种模型。第三再分析微信的私有通信协议完成调用。

2.1 PC微信通信协议的识别

PC端微信使用的协议我们可以通过对每种协议相关的API设置断点来确定是哪个协议。一般PC端客户端程序的通信协议常用的是tcp和udp。 

关于tcp,udp的API:

协议 函数
tcp send
udp sendto

Clipboard Image.png

2.2 通信模型的识别

知道了通信协议之后,再说说通信协议的模型。一般我们在开发网络程序时,会采用两种方式来与网络进行通信,第一种是就是直接从UI获取数据然后发送数据,第二种是有单独的工作线程负责发送数据,两种通信模型我把它们分别称为同步模型,异步模型。 这两种模型中异步模型是比较复杂的,为了方便大家理解,我们将两种模型最简单的雏形给大家梳理一下:

① 同步模型 

Clipboard Image.png

这种模型的优点在于UI操作完就可以立即发送,非常适合我们做逆向分析,通过栈回溯一般都可以找到字符串的踪迹,从而找到发送消息的函数,剩下的就是分析其参数,完成调用即可。一些网络负载不是太大的程序,比如普通软件、2D网络游戏,大多会采用这种方式,实时的获取 

② 异步模型 

Clipboard Image.png

这种模型通常在UI线程中发送数据时会添加到一个队列中,然后在工作线程中不停的从队列中读取数据,然后发送数据。在逆向分析时,我们要做的就是通过在send设置断点,然后可以通过观察send的参数缓冲区,再对缓冲区或是与缓冲区关联的地址设置访问或写入断点,断到写缓冲区,添加队列的地方。之后在去找调用函数。由于这个过程比较复杂,有时需要多次设置断点才可以找到我们想要的信息。 

3. 使用x64dbg分析微信模型与定位关键函数

基于上面的理论,我们实际去分析一下微信的通信模型。

3.1 使用x64dbg分析微信网络发包线程

首先我们还是在send设置断点,然后等断下之后观察堆栈窗口中有没有敏感字符串,我们使用PC版微信中的文件传输助手来测试  

Clipboard Image.png

send断下之后,我们查看堆栈窗口中的信息,观察有无我们输入的字符串

Clipboard Image.png

实际观察会发现堆栈中没有我们想要的字符串,一般这个时候可以大致确定发送数据的这个线程与UI线程不是一个线程,通信的模型属于异步模型。

3.2 使用x64dbg定位UI线程的队列添加

确定了异步模型,我们下一步做的就是在send的参数buf上设置硬件写入断点,看看哪里给这个buf写入了信息,找到那个队列信息。 

send函数断下返回上层的代码中,发现buf的传递经过了好几层,而这几层当中有一个地址中的内容是随着消息的改变而变化的,所以我们可以对这个地址设置硬件写入断点,而不是buf。 

Clipboard Image.png   

我在图中的04EEECA8地址设置硬件写入断点,然后重新发送消息,看断下之后填充值的代码,然后观察堆栈信息,如果没错的话,这个堆栈应该是UI线程,其中应该能找到我们刚刚发送的字符串。

Clipboard Image.png

当我们设置了硬件写入断点,等断点断下时观察堆栈,发现堆栈中并没有刚刚输入的字符串,观察许久发现只有一些和发送消息有关的字符串

Clipboard Image.png

这个时候说明我们发送的信息可能被包装起来了,因为程序会将我们的字符串放在结构体中或是某些数据结构中进行传递,只是查看堆栈可能看不到,需要查看堆栈中看起来像地址的值的内容,才可能找到。

3.3 使用x64dbg定位关键函数

我们刚刚在分析堆栈时发现的字符串:”/cgi-bin/micromsg-bin/newsendmsg” 看起来像是发生信息时信息的类型字符串。我们可以使用x64dbg查看模块wechatwin.dll中的所有和newsendmsg字符串,在其上所有相关字符串设置断点,然后再次发消息测试。    

Clipboard Image.png

 

 

以上的动图可以看到,我们通过搜索newsendmsg字符串,定位到一处代码,设置断点后,再次发送消息,可以在堆栈中找到我们发送的内容。只要找到内容,其实离成功就不远了。在堆栈中我们去寻找和内容有关的调用CALL,需要由下而上一一查看调用CALL的参数,分析之后,可以发现有一个CALL的调用参数非常适合我们的需求。其有5个参数,如下图: 

 

 

Clipboard Image.png

 

 

上图中的调用call 0x0F7F10C0应该就是发消息的代码,看起来非常幸运。当我们查看call内部的时候发现有大量的混淆代码,貌似这个函数被VM过了。查看wechatwin.dll模块的区段果然有有一个VMP的区段。不管怎么样,函数是定位到了,下一步就是调用函数了。

 

 

3.4 使用x64dbg分析关键函数的参数

 

 

定位到关键函数之后,观察函数的参数,发现有5个参数。 

 

 

对每一个参数进行清除测试,即调试过程中分别将每一个参数清除,然后运行程序,发现参数4和参数5,可以没有值,而参数1、参数2、参数3必须有值,根据内容可知参数2是微信用户名,参数3是要发送的内容。而参数1看起来并没有什么含义,经过测试发现,其实这是一个传出缓冲区,其中存储的是加密的数据信息,即我们发送信息的加密版本,由此也可知,0x0F7F10C0函数被VM的理由是其中有加密信息的代码,即微信的私有加密通信协议。

 

 

Clipboard Image.png

 

 

不管怎么样,参数现在基本定了,剩下的就是只要能调用这个函数,其实就可以最初的设想了。

 

 

4.使用C++编写测试代码

 

 

代码使用的是一个MFC的DLL来完成的,我们只需注入到微信中,执行我们的代码就可以了。我们可以根据参数编写如下代码:

  wchar_t* pUser1 = L"filehelper";
        wchar_t* pContent1 = L"hello 15pb";
​
        wchar_t* pUser = (wchar_t*)&pUser1;         wchar_t* pPass = (wchar_t*)&pContent1; ​         char buf[0x3d4] = { 0 }; // 传出buf         _asm {             push 1              // 参数5             push 0              // 参数4             push pPass          // 参数3:发送的内容             push pUser          // 参数2:用户名 ​             lea eax, buf             push eax            // 参数1:传出buf             mov eax, 0x0F7F10C0             call eax         }

 

 

使用上面代码会发现程序会出现异常,而出现异常的地方中,是在访问字符串指向的结构里,上面的代码微信用户名只是一个字符串指针,其他并没有,而观察原本调用时传递的参数可以发现,这是一个字符串结构体,其除了字符串指针之外还有字符串的长度,缓冲区最大长度等等。所以我们需要将整个结构都定义出来,然后测试,耐心排查直到测试完成。这个过程需要不停的修改我们定义的字符串结构体才行,或者分析代码中所有的引用点反推出字符串的结构。因此需要大量的时间才能最终完成。 
这里注意一下,在测试的过程中,总结下来有两个需要注意的问题。第一,参数2和参数3都是二级指针且参数2和参数3是一个字符串结构体而非就一个指针,第二,参数1是传出参数。这三个参数传对,就不会有问题了。 
经过测试以及对比内存中字符串周围的信息,最后定义的字符串数据结构如下:

struct WXString {
    int num;
    wchar_t* pString;
    int nLen;
    int nMaxLen;     int n1;     int n2;     int n3;     int n4; ​     WXString(wchar_t* pStr) {         int len = wcslen(pStr);         pString = new wchar_t[len + 1];         memset(pString, 0, len * 2 + 2);         memcpy(pString, pStr, len * 2);         nLen = len;         nMaxLen = len + 2;         n1 = 0;         n2 = 0;         n3 = 0;         n4 = 0;     } ​     ~WXString() {         if (pString)             delete pString;     } };

 

 

而最后执行的代码:

  WXString szUser(L"filehelper");
    WXString szContent(L"hello 15pb");     char* pAddr = (char*)&szContent + 0x14;     char* pUser = (char*)&szUser.pString;     char* pPass = (char*)&szContent.pString; ​     char buf[0x3d4] = { 0 };     _asm {         push 1         push 0         push pPass         push pUser ​         lea eax, buf         push eax         mov eax, 0xF7F10c0         call eax     }

 

 

5. 测试结果

 

 

我们可以使用x64dbg的插件Scylla完成注入完成,为了测试方便,我们注入之后会显示一个对话框,点击对话框中的按钮测试即可,执行我们上面的关键代码:    

 

 

ceshi7.gif

 

 

6. 总结

 

 

在分析微信的发送消息的函数过程中,我们使用x64dbg的功能有软件断点、硬件写入断点、查找字符串、插件注入等等功能,而且在分析时用到了堆栈分析,找数据时会对数据窗口各种切换。总体来说想要从源头一点一点实现本节的功能还是比较复杂的,祝大家好运!

Android蓝牙通信聊天实现发送和接受功能

Android蓝牙通信聊天实现发送和接受功能

很不错的蓝牙通信demo实现发送和接受功能,就用了两个类就实现了,具体内容如下

说下思路把 主要有两个类 主界面类 蓝牙聊天服务类 。 首先创建线程 实际上就是创建BluetoothChatService() (蓝牙聊天服务类) 这个时候把handler 传过去 这样就可以操作UI 界面了,在线程中不断轮询读取蓝牙消息,当主界面点击发送按钮时 调用BluetoothChatService 的发送方法write 方法,这里的write 方法 使用了handler 发送消息,在主界面显示,另一个 客户端 不断读取蓝牙消息 类似的有个read 方法 同样显示到界面上去,这样就完成了通信了。

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothChat extends Activity {

  // Message types sent from the BluetoothChatService Handler
  public static final int MESSAGE_STATE_CHANGE = 1;
  public static final int MESSAGE_READ = 2;
  public static final int MESSAGE_WRITE = 3;
  public static final int MESSAGE_DEVICE_NAME = 4;
  public static final int MESSAGE_TOAST = 5;

  // Key names received from the BluetoothChatService Handler
  public static final String DEVICE_NAME = "device_name";
  public static final String TOAST = "toast";

  // Intent request codes
  private static final int REQUEST_CONNECT_DEVICE = 1;
  private static final int REQUEST_ENABLE_BT = 2;

  private TextView mTitle;
  private EditText text_chat;
  private EditText text_input;
  private Button but_On_Off;
  private Button but_search; // ------> 在菜单中可以搜索
  private Button but_create; // ------> 在菜单中设置"可被发现"
  private Button mSendButton;
  // 连接到的蓝牙设备的名称
  private String mConnectedDeviceName;
  // String buffer for outgoing messages
  private StringBuffer mOutStringBuffer;
  // Local Bluetooth adapter
  private BluetoothAdapter mBluetoothAdapter = null;
  // Member object for the chat services
  private BluetoothChatService mChatService = null;

  private ArrayList<String> mPairedDevicesList = new ArrayList<String>();
  private ArrayList<String> mNewDevicesList = new ArrayList<String>();
  private String[] strName;
  private String address;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestwindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);

    mTitle = (TextView) this.findViewById(R.id.text_title);
    text_chat = (EditText) this.findViewById(R.id.text_chat);
    text_input = (EditText) this.findViewById(R.id.text_input);
    but_On_Off = (Button) this.findViewById(R.id.but_off_on);
    but_search = (Button) this.findViewById(R.id.but_search_div);
    but_create = (Button) this.findViewById(R.id.but_cjlj);
    mSendButton = (Button) this.findViewById(R.id.but_fsxx);

    // 获得本地的蓝牙适配器
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    // 如果为null,说明没有蓝牙设备
    if (mBluetoothAdapter == null) {
      Toast.makeText(this,"没有蓝牙设备",Toast.LENGTH_LONG).show();
      finish();
      return;
    }

    if (mBluetoothAdapter.isEnabled()) {
      but_On_Off.setText("关闭蓝牙");
    } else {
      but_On_Off.setText("开启蓝牙");
    }

    but_On_Off.setonClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        if (!mBluetoothAdapter.isEnabled()) {
          mBluetoothAdapter.enable();
          Toast.makeText(BluetoothChat.this,"蓝牙已开启",Toast.LENGTH_SHORT).show();
          but_On_Off.setText("关闭蓝牙");
        } else {
          mBluetoothAdapter.disable();
          Toast.makeText(BluetoothChat.this,"蓝牙已关闭",Toast.LENGTH_SHORT).show();
          but_On_Off.setText("开启蓝牙");
        }
      }
    });

    but_search.setonClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        searchDevice();
      }
    });

    but_create.setonClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        final EditText et = new EditText(BluetoothChat.this);
        et.setSingleLine();
        et.setText(mBluetoothAdapter.getName());

        new AlertDialog.Builder(BluetoothChat.this)
            .setTitle("请输入房间名:")
            .setView(et)
            .setPositiveButton("确定",new DialogInterface.OnClickListener() {

                  @Override
                  public void onClick(DialogInterface dialog,int which) {
                    String name = et.getText().toString()
                        .trim();
                    if (name.equals("")) {
                      Toast.makeText(BluetoothChat.this,"请输入房间名",Toast.LENGTH_SHORT).show();
                      return;
                    }
                    // 设置房间名
                    mBluetoothAdapter.setName(name);
                  }

                })
            .setNegativeButton("取消",int which) {

                  }
                }).create().show();

        // 创建连接,也就是设备本地蓝牙设备可被其他用户的蓝牙搜到
        ensurediscoverable();
      }
    });

    // 获得一个已经配对的蓝牙设备的set集合
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
        .getBondedDevices();
    if (pairedDevices.size() > 0) {
      for (BluetoothDevice device : pairedDevices) {
        mPairedDevicesList.add("已配对:" + device.getName() + "\n"
            + device.getAddress());
      }
    } else {
      Toast.makeText(this,"没有已配对的设备",Toast.LENGTH_SHORT).show();
    }

    // 当发现一个新的蓝牙设备时注册广播
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver,filter);

    // 当搜索完毕后注册广播
    filter = new IntentFilter(BluetoothAdapter.ACTION_disCOVERY_FINISHED);
    this.registerReceiver(mReceiver,filter);

  }

  @Override
  public void onStart() {
    super.onStart();
    // If BT is not on,request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
      Intent enableIntent = new Intent(
          BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
      // Otherwise,setup the chat session
    } else {
      if (mChatService == null)
        setupChat();
    }
  }

  @Override
  public synchronized void onResume() {
    super.onResume();
    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(),so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity
    // returns.
    if (mChatService != null) {
      // Only if the state is STATE_NONE,do we kNow that we haven't
      // started already
      if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
        // Start the Bluetooth chat services
        mChatService.start();
      }
    }
  }

  private void setupChat() {

    mSendButton.setonClickListener(new OnClickListener() {
      public void onClick(View v) {
        // Send a message using content of the edit text widget
        String message = text_input.getText().toString();
        sendMessage(message);
      }
    });

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this,mHandler);

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    // Stop the Bluetooth chat services
    if (mChatService != null)
      mChatService.stop();

    // Make sure we're not doing discovery anymore
    if (mBluetoothAdapter != null) {
      mBluetoothAdapter.canceldiscovery();
    }
    // Unregister broadcast listeners
    this.unregisterReceiver(mReceiver);
  }

  /** 使本地的蓝牙设备可被发现 */
  private void ensurediscoverable() {
    if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_disCOVERABLE) {
      Intent discoverableIntent = new Intent(
          BluetoothAdapter.ACTION_REQUEST_disCOVERABLE);
      discoverableIntent.putExtra(
          BluetoothAdapter.EXTRA_disCOVERABLE_DURATION,300);
      startActivity(discoverableIntent);
    }
  }

  /**
   * Sends a message.
   * 
   * @param message
   *      A string of text to send.
   */
  private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
      Toast.makeText(this,"没有连接上",Toast.LENGTH_SHORT).show();
      return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
      // Get the message bytes and tell the BluetoothChatService to write
      byte[] send = message.getBytes();
      mChatService.write(send);

      // Reset out string buffer to zero and clear the edit text field
      mOutStringBuffer.setLength(0);
      text_input.setText(mOutStringBuffer);
    }
  }

  // The Handler that gets information back from the BluetoothChatService
  private final Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case MESSAGE_STATE_CHANGE:
        switch (msg.arg1) {
        case BluetoothChatService.STATE_CONNECTED:
          mTitle.setText("已经连接");
          mTitle.append(mConnectedDeviceName);
          // mConversationArrayAdapter.clear();
          break;
        case BluetoothChatService.STATE_CONNECTING:
          mTitle.setText("正在连接中...");
          break;
        case BluetoothChatService.STATE_LISTEN:
        case BluetoothChatService.STATE_NONE:
          mTitle.setText("未连接上");
          break;
        }
        break;
      case MESSAGE_WRITE:
        byte[] writeBuf = (byte[]) msg.obj;
        // construct a string from the buffer
        String writeMessage = new String(writeBuf);
        // mConversationArrayAdapter.add("Me: " + writeMessage);
        text_chat.append("我:" + writeMessage + "\n");
        break;
      case MESSAGE_READ:
        byte[] readBuf = (byte[]) msg.obj;
        // construct a string from the valid bytes in the buffer
        String readMessage = new String(readBuf,msg.arg1);
        // mConversationArrayAdapter.add(mConnectedDeviceName+": " +
        // readMessage);
        text_chat.append(mConnectedDeviceName + ":" + readMessage
            + "\n");
        break;
      case MESSAGE_DEVICE_NAME:
        // save the connected device's name
        mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
        Toast.makeText(getApplicationContext(),"连接到 " + mConnectedDeviceName,Toast.LENGTH_SHORT)
            .show();
        break;
      case MESSAGE_TOAST:
        Toast.makeText(getApplicationContext(),msg.getData().getString(TOAST),Toast.LENGTH_SHORT)
            .show();
        break;
      }
    }
  };

  // 连接蓝牙设备
  private void linkDevice() {
    if (mBluetoothAdapter.isdiscovering()) {
      mBluetoothAdapter.canceldiscovery();
    }
    int cou = mPairedDevicesList.size() + mNewDevicesList.size();
    if (cou == 0) {
      Toast.makeText(BluetoothChat.this,"没有搜索到可用的蓝牙设备",Toast.LENGTH_SHORT).show();
      return;
    }

    // 把已经配对的蓝牙设备和新发现的蓝牙设备的名称都放入数组中,以便在对话框列表中显示
    strName = new String[cou];
    for (int i = 0; i < mPairedDevicesList.size(); i++) {
      strName[i] = mPairedDevicesList.get(i);
    }
    for (int i = mPairedDevicesList.size(); i < strName.length; i++) {
      strName[i] = mNewDevicesList.get(i - mPairedDevicesList.size());
    }
    address = strName[0].substring(strName[0].length() - 17);
    new AlertDialog.Builder(BluetoothChat.this)
        .setTitle("搜索到的蓝牙设备:")
        .setSingleChoiceItems(strName,new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,int which) {
                // 当用户点击选中的蓝牙设备时,取出选中的蓝牙设备的MAC地址
                address = strName[which].split("\\n")[1].trim();
              }
            })
        .setPositiveButton("连接",new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog,int which) {
            if (address == null) {
              Toast.makeText(BluetoothChat.this,"请先连接外部蓝牙设备",Toast.LENGTH_SHORT).show();
              return;
            }

            Log.i("sxd","address:" + address);
            // Get the BLuetoothDevice object
            BluetoothDevice device = mBluetoothAdapter
                .getRemoteDevice(address);
            // Attempt to connect to the device
            mChatService.connect(device);
          }
        })
        .setNegativeButton("取消",int which) {

          }
        }).create().show();
  }

  // 搜索蓝牙设备蓝牙设备
  private void searchDevice() {
    mTitle.setText("正在努力搜索中...");
    setProgressBarIndeterminateVisibility(true);
    if (mBluetoothAdapter.isdiscovering()) {
      mBluetoothAdapter.canceldiscovery();
    }
    mNewDevicesList.clear();
    mBluetoothAdapter.startdiscovery();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,1,"搜索设备");
    menu.add(0,2,"可被发现");
    return true;
  }

  private final broadcastReceiver mReceiver = new broadcastReceiver() {
    @Override
    public void onReceive(Context context,Intent intent) {
      String action = intent.getAction();
      // 当发现一个新的蓝牙设备时
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent
            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // If it's already paired,skip it,because it's been listed
        // already
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
          String s = "未配对: " + device.getName() + "\n"
              + device.getAddress();
          if (!mNewDevicesList.contains(s))
            mNewDevicesList.add(s);
        }
        // When discovery is finished,change the Activity title
      } else if (BluetoothAdapter.ACTION_disCOVERY_FINISHED
          .equals(action)) {
        setProgressBarIndeterminateVisibility(false);
        if (mNewDevicesList.size() == 0) {
          Toast.makeText(BluetoothChat.this,"没有发现新设备",Toast.LENGTH_SHORT).show();
        }
        mTitle.setText("未连接");
        linkDevice();
      }
    }
  };

  @Override
  public boolean onoptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
      searchDevice();
      return true;
    case 2:
      // Ensure this device is discoverable by others
      ensurediscoverable();
      return true;
    }
    return false;
  }

}
package com.it2388.bluetooth;

/*
 * copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,software
 * distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * This class does all the work for setting up and managing Bluetooth
 * connections with other devices. It has a thread that listens for incoming
 * connections,a thread for connecting with a device,and a thread for
 * performing data transmissions when connected.
 * 
 * *这个类做所有的工作,建立和管理蓝牙 与其他设备的连接。它有一个线程监听 传入的连接,一个用于连接一个设备的线程,和一个 当连接时执行数据传输的线程。
 */
public class BluetoothChatService {
  // Debugging
  private static final String TAG = "BluetoothChatService";
  private static final boolean D = true;

  // Name for the SDP record when creating server socket 对于SDP记录名称创建服务器套接字时
  private static final String NAME = "BluetoothChat";

  // Unique UUID for this application
  private static final UUID MY_UUID = UUID
      .fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

  // Member fields
  private final BluetoothAdapter mAdapter;
  private final Handler mHandler;
  private AcceptThread mAcceptThread;
  private ConnectThread mConnectThread;
  private ConnectedThread mConnectedThread;
  private int mState;

  // Constants that indicate the current connection state
  public static final int STATE_NONE = 0; // we're doing nothing 我们什么都不做
  public static final int STATE_LISTEN = 1; // Now listening for incoming 现在监听
                        // connections
  public static final int STATE_CONNECTING = 2; // Now initiating an outgoing
                          // 启动一个外向
                          // connection
  public static final int STATE_CONNECTED = 3; // Now connected to a remote
                          // 连接到一个远程
                          // device

  /**
   * Constructor. Prepares a new BluetoothChat session.
   * 
   * @param context
   *      The UI Activity Context
   * @param handler
   *      A Handler to send messages back to the UI Activity
   */
  public BluetoothChatService(Context context,Handler handler) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;
    mHandler = handler;
  }

  /**
   * Set the current state of the chat connection
   * 
   * @param state
   *      An integer defining the current connection state
   */
  private synchronized void setState(int state) {
    if (D)
      Log.d(TAG,"setState() " + mState + " -> " + state);
    mState = state;

    // Give the new state to the Handler so the UI Activity can update
    mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE,state,-1)
        .sendToTarget();
  }

  /**
   * Return the current connection state.
   */
  public synchronized int getState() {
    return mState;
  }

  /**
   * Start the chat service. Specifically start AcceptThread to begin a
   * session in listening (server) mode. Called by the Activity onResume()
   */
  public synchronized void start() {
    if (D)
      Log.d(TAG,"start");

    // Cancel any thread attempting to make a connection
    if (mConnectThread != null) {
      mConnectThread.cancel();
      mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
      mConnectedThread.cancel();
      mConnectedThread = null;
    }

    // Start the thread to listen on a BluetoothServerSocket
    if (mAcceptThread == null) {
      mAcceptThread = new AcceptThread();
      mAcceptThread.start();
    }
    setState(STATE_LISTEN);
  }

  /**
   * Start the ConnectThread to initiate a connection to a remote device.
   * 
   * @param device
   *      The BluetoothDevice to connect
   */
  public synchronized void connect(BluetoothDevice device) {
    if (D)
      Log.d(TAG,"connect to: " + device);

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
      if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
      }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
      mConnectedThread.cancel();
      mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
  }

  /**
   * Start the ConnectedThread to begin managing a Bluetooth connection
   * 
   * @param socket
   *      The BluetoothSocket on which the connection was made
   * @param device
   *      The BluetoothDevice that has been connected
   */
  public synchronized void connected(BluetoothSocket socket,BluetoothDevice device) {
    if (D)
      Log.d(TAG,"connected");

    // Cancel the thread that completed the connection
    if (mConnectThread != null) {
      mConnectThread.cancel();
      mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
      mConnectedThread.cancel();
      mConnectedThread = null;
    }

    // Cancel the accept thread because we only want to connect to one
    // device
    if (mAcceptThread != null) {
      mAcceptThread.cancel();
      mAcceptThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();

    // Send the name of the connected device back to the UI Activity
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(BluetoothChat.DEVICE_NAME,device.getName());
    msg.setData(bundle);
    mHandler.sendMessage(msg);

    setState(STATE_CONNECTED);
  }

  /**
   * Stop all threads
   */
  public synchronized void stop() {
    if (D)
      Log.d(TAG,"stop");
    if (mConnectThread != null) {
      mConnectThread.cancel();
      mConnectThread = null;
    }
    if (mConnectedThread != null) {
      mConnectedThread.cancel();
      mConnectedThread = null;
    }
    if (mAcceptThread != null) {
      mAcceptThread.cancel();
      mAcceptThread = null;
    }
    setState(STATE_NONE);
  }

  /**
   * Write to the ConnectedThread in an unsynchronized manner
   * 
   * @param out
   *      The bytes to write
   * @see ConnectedThread#write(byte[])
   */
  public void write(byte[] out) {
    // Create temporary object
    ConnectedThread r;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
      if (mState != STATE_CONNECTED)
        return;
      r = mConnectedThread;
    }
    // Perform the write unsynchronized
    r.write(out);
  }

  /**
   * Indicate that the connection attempt Failed and notify the UI Activity.
   */
  private void connectionFailed() {
    setState(STATE_LISTEN);

    // Send a failure message back to the Activity
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
    Bundle bundle = new Bundle();
    bundle.putString(BluetoothChat.TOAST,"不能连接到设备");
    msg.setData(bundle);
    mHandler.sendMessage(msg);
  }

  /**
   * Indicate that the connection was lost and notify the UI Activity.
   */
  private void connectionLost() {
    setState(STATE_LISTEN);

    // Send a failure message back to the Activity
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
    Bundle bundle = new Bundle();
    bundle.putString(BluetoothChat.TOAST,"设备连接中断");
    msg.setData(bundle);
    mHandler.sendMessage(msg);
  }

  /**
   * This thread runs while listening for incoming connections. It behaves
   * like a server-side client. It runs until a connection is accepted (or
   * until cancelled).
   */
  private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
      BluetoothServerSocket tmp = null;
      // Create a new listening server socket
      try {
        tmp = mAdapter
            .listenUsingRfcommWithServiceRecord(NAME,MY_UUID);
      } catch (IOException e) {
        Log.e("sxd"," ======== ufcomm exception =======",e);
      }
      mmServerSocket = tmp;
    }

    public void run() {
      if (D)
        Log.d(TAG,"BEGIN mAcceptThread" + this);
      setName("AcceptThread");
      BluetoothSocket socket = null;

      // Listen to the server socket if we're not connected
      while (mState != STATE_CONNECTED) {
        try {
          // This is a blocking call and will only return on a
          // successful connection or an exception
          socket = mmServerSocket.accept();
        } catch (IOException e) {
          Log.e("sxd","---> accept socket Failed <---",e);
          break;
        }

        // If a connection was accepted
        if (socket != null) {
          synchronized (BluetoothChatService.this) {
            switch (mState) {
            case STATE_LISTEN:
            case STATE_CONNECTING:
              // Situation normal. Start the connected thread.
              connected(socket,socket.getRemoteDevice());
              break;
            case STATE_NONE:
            case STATE_CONNECTED:
              // Either not ready or already connected. Terminate
              // new socket.
              try {
                socket.close();
              } catch (IOException e) {
                Log.e(TAG,"Could not close unwanted socket",e);
              }
              break;
            }
          }
        }
      }
      if (D)
        Log.i(TAG,"END mAcceptThread");
    }

    public void cancel() {
      if (D)
        Log.d(TAG,"cancel " + this);
      try {
        mmServerSocket.close();
      } catch (IOException e) {
        Log.e(TAG,"close() of server Failed",e);
      }
    }
  }

  /**
   * This thread runs while attempting to make an outgoing connection with a
   * device. It runs straight through; the connection either succeeds or
   * fails.
   */
  private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
      mmDevice = device;
      BluetoothSocket tmp = null;

      // Get a BluetoothSocket for a connection with the
      // given BluetoothDevice
      try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
      } catch (IOException e) {
        Log.e(TAG,"create() Failed",e);
      }
      mmSocket = tmp;
    }

    public void run() {
      Log.i(TAG,"BEGIN mConnectThread");
      setName("ConnectThread");

      // Always cancel discovery because it will slow down a connection
      mAdapter.canceldiscovery();

      // Make a connection to the BluetoothSocket
      try {
        // This is a blocking call and will only return on a
        // successful connection or an exception
        mmSocket.connect();
      } catch (IOException e) {
        Log.e("sxd","链接发生了异常",e);
        connectionFailed();
        // Close the socket
        try {
          mmSocket.close();
        } catch (IOException e2) {
          Log.e(TAG,"unable to close() socket during connection failure",e2);
        }
        // Start the service over to restart listening mode
        BluetoothChatService.this.start();
        return;
      }

      // Reset the ConnectThread because we're done
      synchronized (BluetoothChatService.this) {
        mConnectThread = null;
      }

      // Start the connected thread
      connected(mmSocket,mmDevice);
    }

    public void cancel() {
      try {
        mmSocket.close();
      } catch (IOException e) {
        Log.e(TAG,"close() of connect socket Failed",e);
      }
    }
  }

  /**
   * This thread runs during a connection with a remote device. It handles all
   * incoming and outgoing transmissions.
   * 
   * 和已经建立连接的设置进行数据的传输
   */
  private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
      Log.d(TAG,"create ConnectedThread");
      mmSocket = socket;
      InputStream tmpIn = null;
      OutputStream tmpOut = null;

      // Get the BluetoothSocket input and output streams
      try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getoutputStream();
      } catch (IOException e) {
        Log.e(TAG,"temp sockets not created",e);
      }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }

    public void run() {
      Log.i(TAG,"BEGIN mConnectedThread");
      byte[] buffer = new byte[1024];
      int bytes;

      // Keep listening to the InputStream while connected
      while (true) {
        try {
          // Read from the InputStream
          bytes = mmInStream.read(buffer);

          // Send the obtained bytes to the UI Activity
          mHandler.obtainMessage(BluetoothChat.MESSAGE_READ,bytes,-1,buffer).sendToTarget();
        } catch (IOException e) {
          Log.e(TAG,"disconnected",e);
          connectionLost();
          break;
        }
      }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *      The bytes to write
     */
    public void write(byte[] buffer) {
      try {
        mmOutStream.write(buffer);

        // Share the sent message back to the UI Activity
        mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE,buffer).sendToTarget();
      } catch (IOException e) {
        Log.e(TAG,"Exception during write",e);
      }
    }

    public void cancel() {
      try {
        mmSocket.close();
      } catch (IOException e) {
        Log.e(TAG,e);
      }
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

Gitee 推荐 | Windows 开源调试器 x64dbg

Gitee 推荐 | Windows 开源调试器 x64dbg

适用于 Windows 的开源二进制调试器,旨在对没有源代码的可执行文件进行恶意软件分析和逆向工程。

 

Gitee 推荐 | x64dbg 自动化控制插件 LyScript

Gitee 推荐 | x64dbg 自动化控制插件 LyScript

一款强大的针对 x64dbg 开发的 Python 自动化控制模块,提高逆向分析效率。

HTML5高仿微信聊天、微信聊天表情、对话框、编辑器功能

HTML5高仿微信聊天、微信聊天表情、对话框、编辑器功能

之前做过一版h5微信聊天移动端,这段时间闲来无事就整理了下之前项目,又重新在原先的那版基础上升级了下,如是就有了现在的h5仿微信聊天高仿版,新增了微聊、通讯录、探索、我四个模块 左右触摸滑屏切换,聊天页面优化了多图预览、视频播放,长按菜单UI,聊天底部编辑器重新优化整理(新增多表情),弹窗则用到了自己开发的wcPop.js,具体看项目效果图吧!

HTML代码片段:

rush:html;toolbar:false">

关于使用x64dbg分析微信聊天函数并实现发信息的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android蓝牙通信聊天实现发送和接受功能、Gitee 推荐 | Windows 开源调试器 x64dbg、Gitee 推荐 | x64dbg 自动化控制插件 LyScript、HTML5高仿微信聊天、微信聊天表情、对话框、编辑器功能等相关内容,可以在本站寻找。

本文标签: