Android BLE蓝牙蓝牙4.2数据透传操作数据透传操作
BLE蓝牙蓝牙4.2数据透传操作数据透传操作蓝牙模块服务查看测试代码新建工程添加权限初始化几个工具控件代码流程
Android版本有网友提到需要7.0以上(未求证)
本文所测试的蓝牙模块是CC2640,不适用蓝牙2.0版本。
代码流程代码流程
蓝牙的操作流程网友介绍得比较详细,这里不再做重复介绍,上面的演示效果除了布局,写的代码其余都在这一个MainActivity类里,也就没有做太多优化(抄起来方便),其中比较
关键的就是一定要检查那两个UUID:
package com.example.bluetooth4;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 布局控件
private Button scanBtn;
private Button sendBtn;
private TextView reTv;
// 蓝牙相关的变量
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothManager;
评论0