package com.crazy.bluetooth.ble;
import android.app.Activity;
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.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.crazy.bluetooth.BaseApplication;
import com.crazy.bluetooth.R;
import com.crazy.bluetooth.util.CRC8;
import com.crazy.bluetooth.util.IntByteStringHexUtil;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;
/**
* BLE客户端
*/
public class BleClientActivity extends Activity {
private static final String TAG = BleClientActivity.class.getSimpleName();
private EditText mWriteET;
private TextView mTips;
private BleDevAdapter mBleDevAdapter;
private BluetoothGatt mBluetoothGatt;
private boolean isConnected = false;
private static final int bleSize = 20;
private static Queue<String> queue = new LinkedList<String>();
// 与服务端连接的Callback
public BluetoothGattCallback mBluetoothGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
BluetoothDevice dev = gatt.getDevice();
Log.i(TAG, String.format("onConnectionStateChange:%s,%s,%s,%s", dev.getName(), dev.getAddress(), status, newState));
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
isConnected = true;
gatt.discoverServices(); //启动服务发现
} else {
isConnected = false;
closeConn();
}
logTv(String.format(status == 0 ? (newState == 2 ? "与[%s]连接成功" : "与[%s]连接断开") : ("与[%s]连接出错,错误码:" + status), dev));
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.i(TAG, String.format("onServicesDiscovered:%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), status));
if (status == BluetoothGatt.GATT_SUCCESS) { //BLE服务发现成功
// 遍历获取BLE服务Services/Characteristics/Descriptors的全部UUID
for (BluetoothGattService service : gatt.getServices()) {
StringBuilder allUUIDs = new StringBuilder("UUIDs={\nS=" + service.getUuid().toString());
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
allUUIDs.append(",\nC=").append(characteristic.getUuid());
for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors())
allUUIDs.append(",\nD=").append(descriptor.getUuid());
}
allUUIDs.append("}");
Log.i(TAG, "onServicesDiscovered:" + allUUIDs.toString());
logTv("发现服务" + allUUIDs);
}
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
UUID uuid = characteristic.getUuid();
String valueStr = new String(characteristic.getValue());
Log.e(TAG, String.format("onCharacteristicRead:%s,%s,%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), uuid, valueStr, status));
logTv("读取Characteristic[" + uuid + "]:\n" + valueStr);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
UUID uuid = characteristic.getUuid();
String valueStr = new String(characteristic.getValue());
Log.e(TAG, String.format("onCharacteristicWrite:%s,%s,%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), uuid, valueStr, status));
logTv("写入Characteristic[" + uuid + "]:\n" + valueStr);
if(queue.size()>0){
bleNotifyDevice(Base64.decode(queue.poll(), Base64.NO_WRAP));
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
UUID uuid = characteristic.getUuid();
String valueStr = new String(characteristic.getValue());
Log.e(TAG, String.format("onCharacteristicChanged:%s,%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), uuid, valueStr));
logTv("通知Characteristic[" + uuid + "]:\n" + valueStr);
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
UUID uuid = descriptor.getUuid();
String valueStr = Arrays.toString(descriptor.getValue());
Log.i(TAG, String.format("onDescriptorRead:%s,%s,%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), uuid, valueStr, status));
logTv("读取Descriptor[" + uuid + "]:\n" + valueStr);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
UUID uuid = descriptor.getUuid();
String valueStr = Arrays.toString(descriptor.getValue());
Log.e(TAG, String.format("onDescriptorWrite:%s,%s,%s,%s,%s", gatt.getDevice().getName(), gatt.getDevice().getAddress(), uuid, valueStr, status));
logTv("写入Descriptor[" + uuid + "]:\n" + valueStr);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bleclient);
RecyclerView rv = findViewById(R.id.rv_ble);
mWriteET = findViewById(R.id.et_write);
mTips = findViewById(R.id.tv_tips);
rv.setLayoutManager(new LinearLayoutManager(this));
mBleDevAdapter = new BleDevAdapter(new BleDevAdapter.Listener() {
@Override
public void onItemClick(BluetoothDevice dev) {
closeConn();
mBluetoothGatt = dev.connectGatt(BleClientActivity.this, false, mBluetoothGattCallback,BluetoothDevice.TRANSPORT_LE);
//autoConnect 这意味着 是否直接连接到远程设备(false)或在远程设备可用时立即自动连接(true)
//以下方案还是无效
//mBluetoothGatt = dev.connectGatt(BleClientActivity.this, true, mBluetoothGattCallback,TRANSPORT_LE); // 连接蓝牙设备
//bluetoothDevice.connectGatt(MainActivity.this, true, gattCallback);总是连接失败,提示status返回133,用了各种方法都不行,
// 后台一查才发现6.0及以上系统的手机要使用bluetoothDevice.connectGatt(MainActivity.this,true, gattCallback, TRANSPORT_LE),
// 其中TRANSPORT_LE参数是设置传输层模式。传输层模式有三种TRANSPORT_AUTO 、TRANSPORT_BREDR 和TRANSPORT_LE。
// 如果不传默认TRANSPORT_AUTO,6.0系统及以上需要使用TRANSPORT_LE这种传输模式,
// 具体为啥,我也不知道,我猜是因为Android6.0及以上系统重新定义了蓝牙BLE的传输模式必须使用TRANSPORT_LE这种方式吧。
//发起蓝牙Gatt连接 BluetoothDevice.connectGatt(Context context, boolean autoConnect,
//BluetoothGattCallback callbac
没有合适的资源?快使用搜索试试~ 我知道了~
Android蓝牙Ble通讯Demo示例源码–扫描,连接,发送和接收数据,分包解包
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉

万物互联的物联网时代的已经来临,ble蓝牙开发在其中扮演着举重若轻的角色。最近刚好闲一点,抽时间梳理下这块的知识点。 涉及ble蓝牙通讯的客户端(开启、扫描、连接、发送和接收数据、分包解包)和服务端(初始化广播数据、开始广播、配置Services、Server回调操作)整个环节以及一些常见的问题即踩过的一些坑。 比如 1、在Android不同版本或不同手机的适配问题,扫描不到蓝牙设备 2、如何避免ble蓝牙连接出现133错误? 3、单次写的数据大小有20字节限制,如何发送长数据 具体可以参考 https://blog.csdn.net/daokedream/article/details/114240815,欢迎交流。
资源推荐
资源详情
资源评论

















收起资源包目录










































































































































共 95 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
Crazy程序猿2020
- 粉丝: 0
- 资源: 26

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
- 3
- 4
- 5
- 6
前往页