这两天在研究蓝牙,网上有关蓝牙的内容非常有限,Github上的蓝牙框架也很少很复杂,为此我特地写了一个最最简单的DEMO,实现BLE蓝牙接收数据的问题,

不需要什么特定的UUID,

不需要什么断开重连,

不需要什么多连接等等,

网上都把BLE蓝牙写的好复杂好复杂,那不是我想要的,我只想为新手提供一个最基本的例子

注意:

1.本DEMO运行前提是蓝牙已经配对成功,如果想实现自动配对可以期待我的下一篇文章

2.修改代码中的“你想要接收数据的已配对设备名称”为你真实的蓝牙设备

3.复制粘贴下面的代码,日志TAG是“BLE”

代码:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
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.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast; import java.util.List;
import java.util.Set;
import java.util.UUID; public class MainActivity extends AppCompatActivity {
private BluetoothAdapter adapter;
private BluetoothGatt bluetoothGatt; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openBlueToothLe();
} //打开蓝牙
private void openBlueToothLe() {
adapter = BluetoothAdapter.getDefaultAdapter();
if (null == adapter) {
Toast.makeText(this, "没有蓝牙功能", Toast.LENGTH_SHORT).show();
return;
}
if (!adapter.isEnabled()) {
adapter.enable();
}
startScan();
} //开始扫描
private void startScan() {
Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
for (BluetoothDevice bondedDevice : bondedDevices) {
if ("你想要接收数据的已配对设备名称".equals(bondedDevice.getName().trim())) {
connectDevice(bondedDevice);
}
}
} //连接设备
private void connectDevice(BluetoothDevice bondedDevice) {
bluetoothGatt = bondedDevice.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (BluetoothGatt.STATE_CONNECTED == newState) {
bluetoothGatt = gatt;
gatt.discoverServices();
} else if (BluetoothGatt.STATE_DISCONNECTED == newState) {
gatt.close();
}
} @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService service : services) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic character : characteristics) {
enableNotification(gatt, service.getUuid(), character.getUuid());
}
}
} @Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] value = characteristic.getValue();
Log.i("BLE", "receive value ----------------------------");
for (int i = 0; i < value.length; i++) {
Log.i("BLE", "character_value = " + value[i]);
}
}
});
} @Override
protected void onDestroy() {
super.onDestroy();
bluetoothGatt.disconnect();
} public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) {
boolean success = false;
BluetoothGattService service = gatt.getService(serviceUUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
if (characteristic != null) {
success = gatt.setCharacteristicNotification(characteristic, true);
if (success) {
// 来源:http://stackoverflow.com/questions/38045294/oncharacteristicchanged-not-called-with-ble
for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) {
if (dp != null) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
gatt.writeDescriptor(dp);
}
}
}
}
}
return success;
} private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
BluetoothGattCharacteristic characteristic = null;
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic c : characteristics) {
if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
&& characteristicUUID.equals(c.getUuid())) {
characteristic = c;
break;
}
}
if (characteristic != null)
return characteristic;
for (BluetoothGattCharacteristic c : characteristics) {
if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
&& characteristicUUID.equals(c.getUuid())) {
characteristic = c;
break;
}
}
return characteristic;
}
}

对,就是这么简单,一个类足以,接下来就可以在Android studio的Logcat看到打印的返回值了

Github地址:https://github.com/king1039/BlueToothLe

欢迎关注我的微信公众号:安卓圈

10分钟完成一个最最简单的BLE蓝牙接收数据的DEMO的更多相关文章

  1. CBrother脚本10分钟写一个拯救“小霸王服务器”的程序

    CBrother脚本语言10分钟写一个拯救“小霸王服务器”的程序 到了一家新公司,接手了一坨c++服务器代码,到处内存泄漏,这服务器没有数据库,挂了后重启一下就好了,公司就这么凑活着用了几年了,定时重 ...

  2. Django从Models 10分钟定制一个Admin后台

    目录 Django从Models 10分钟建立一套RestfulApi Django从Models 10分钟定制一个Admin后台 简介 Django自带一个Admin后台, 支持用户创建,权限配置和 ...

  3. 10 分钟实现一个自己的server监控器

    需求 近期须要给自己的server加入监控器.目的是监控server的内存.CPU.磁盘占用率,资源占用率过高的话能给自己发个提醒.当前主流的平台通常会提供邮件.短息.甚至会提供微信提醒,只是这类提醒 ...

  4. 10分钟搭建一个小型网页(python django)(hello world!)

    10分钟搭建一个小型网页(python django)(hello world!) 1.安装django pip install django 安装成功后,在Scripts目录下存在django-ad ...

  5. Android5.0(Lollipop) BLE蓝牙4.0+浅析demo连接(三)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23363591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Android5.0(L ...

  6. python scrapy 入门,10分钟完成一个爬虫

    在TensorFlow热起来之前,很多人学习python的原因是因为想写爬虫.的确,有着丰富第三方库的python很适合干这种工作. Scrapy是一个易学易用的爬虫框架,尽管因为互联网多变的复杂性仍 ...

  7. android5.0 BLE 蓝牙4.0+浅析demo搜索(一)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23341414来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Bgwan 莳萝花 ...

  8. Java工作流引擎结合可视化表单开发,10分钟完成一个业务流程发布

    回忆以前工作流引擎的应用,感觉历历在目啊!当初公司接了一个项目关于政府单位公文流转的管理系统,一开始客户跟我画了十多张业务流程图.话说这十多张业务流程图,涉及的业务范围还蛮多,像用审批授权,开通流程, ...

  9. 10分钟写一个markdown编辑器

    marked.js Marked是一个Markdown解析引擎. vue.js Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vu ...

随机推荐

  1. 利用avicap32.dll实现的实时视频传输

    直接上代码吧! 在窗体上调用的类: using System; using System.Collections.Generic; using System.ComponentModel; using ...

  2. 题解 洛谷P1236 【算24点】

    不得不说,个人认为许多大佬们把程序想复杂了,所以码量很长,但是实际上这题并不要这么复杂... 可以考虑用一个\(dfs\)维护一个状态\(f(n)[a_1,a_2--a_n]\) 接下来我们暴力枚举两 ...

  3. danci3

    permit 英 [pə'mɪt] 美 [pɚ'mɪt] vi. 许可:允许 vt. 许可:允许 n. 许可证,执照 encapsulate 英 [ɪn'kæpsjʊleɪt; en-] 美 [ɪn' ...

  4. docker-compose部署zabbix4.2.5

    桥接模式(mysql和zabbix-server镜像本人修改了一些配置进行重构过) cat zabbix.yml version: '3' services: mysql: image: debian ...

  5. CentOS7.6 yum方式安装redis最新版

    sudo yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm sudo yum --enablerep ...

  6. vs下载代码

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012605477/article/details/62222919Visual Studio 20 ...

  7. 猎豹全球智库执行院长:中国App出海的三大规律和最具代表的五大垂直品类

    https://36kr.com/p/5100078 中国出海还是处于一个黄金时代. “国内互联网公司的竞争越来越白热化,出海的时间点变得越来越紧迫,”在36氪日前举办的“WISEx新出海行业峰会”上 ...

  8. spring-cloud(一)

    1.SpringCloud概述和搭建Eureka服务注册中心 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注 ...

  9. Redis 高可用架构设计(转载)

    转载自:https://mp.weixin.qq.com/s?__biz=MzA3NDcyMTQyNQ==&mid=2649263292&idx=1&sn=b170390684 ...

  10. learning java 读写其他进程的数据

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public ...