Android蓝牙BLE开发,扫描、连接、发送和读取信息;
1、BLE开发权限
Android蓝牙BLE开发须打开蓝牙权限和6.0位置权限;
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
if (Build.VERSION.SDK_INT < 23){return;}
//判断是否有权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
若需要BLE支持feature 还需打开这个权限;
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
2、手机是否支持BLE,打开蓝牙;
先判断手机是否支持BLE,在获取蓝牙的适配器打开蓝牙;
获取蓝牙适配器和打开蓝牙各有两种方法,都可以;
public void initialize(){
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
return;
}
BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = btManager.getAdapter();
//mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //也可以这样获取
if ( null != mBluetoothAdapter ) {
if ( !mBluetoothAdapter.isEnabled() ) {
mBluetoothAdapter.enable();
// Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enabler,1); //提示用户打开蓝牙
Toast.makeText(this, "正在打开蓝牙...", Toast.LENGTH_SHORT).show();
}
}
}
3、扫描其它蓝牙设备;
开始扫描蓝牙,扫描时间为5秒;(扫描的设备可能重复) Log输出的就是扫描到的蓝牙设备;
leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
Log.i("ccb", "onLeScan: " + bluetoothDevice.getName() + "--" + bluetoothDevice.getAddress());
}
};
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothAdapter.stopLeScan(leScanCallback);
}
}, 5000);
mBluetoothAdapter.startLeScan(leScanCallback);
4、连接蓝牙
拿到扫描到的BluetoothDevice对象,使用它去调用连接的方法;其中BluetoothGatt 就是你所需要的通信管道了;
onConnectionStateChange:连接状态发生改变时;
onServicesDiscovered:发现服务时;(服务会包含UUID,还会包含其特征,特征中的内容)
device.connectGatt(AndroidBleActivity.this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.e("ccb", "设备连接上 开始扫描服务");
// 开始扫描服务,安卓蓝牙开发重要步骤之一
gatt.discoverServices();
}
if (newState == BluetoothGatt.STATE_DISCONNECTED) {
Log.e("ccb", "连接断开");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> servicesList;
//获取服务列表 服务列表中有当前设备的uuid
servicesList = gatt.getServices();
}
//若写入指令成功则回调BluetoothGattCallback中的onCharacteristicWrite()方法,说明将数据已经发送给下位机。
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
//若发送的数据符合通信协议,则下位机会向上位机回复相应的数据。发送的数据通过回调onCharacteristicChanged()方法获取
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
// value为设备发送的数据,根据数据协议进行解析
byte[] value = characteristic.getValue();
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
});
}
});
5、发送信息到BLE设备;
characteristic就是特征,它可以包含你要发送的信息;
BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("服务的uuid"));
BluetoothGattCharacteristic gattCharacteristic = gattService.getCharacteristic(UUID.fromString("特征的uuid"));
gattCharacteristic.setValue(new byte[]{0x1a,0x0a});
mBluetoothGatt.writeCharacteristic(gattCharacteristic); //发送数据
先写这么多吧,有时间再补;
Android蓝牙BLE开发,扫描、连接、发送和读取信息;的更多相关文章
- Android 蓝牙 BLE 开发笔记
最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE.Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了 ...
- iOS蓝牙BLE开发
蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...
- Android蓝牙——HID开发
代码地址如下:http://www.demodashi.com/demo/13891.html 原文地址: https://blog.csdn.net/VNanyesheshou/article/de ...
- Android 低功耗蓝牙BLE 开发注意事项
基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...
- Android低功耗蓝牙(BLE)开发的一点感受
最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...
- Android蓝牙BLE低功耗相关简单总结
在看Android4.42的源代码时看到有加入对BLE设备的处理.看的一头雾水,多方百度,最终有种柳暗花明的感觉. 本文总结来源于百度多篇文章,欢迎转载.分享交流 BLE蓝牙概念 BLE:Blueto ...
- Qt on Android 蓝牙通信开发
版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...
- android蓝牙通讯开发(详细)
新建一个工程之后,我们可以先看到界面左边的项目栏,我们可以看到,除了app目录以外,大多数的文件和目录都是自动生成的,我们也不需要对他们进行修改,而app目录之下的文件才是我们工作的重点.下面,我先对 ...
- Android问题-DelphiXE5开发Andriod连接Webservice乱码问题
问题现象:在使用DelphiXE5开发Andriod连接Webservice乱码. 问题原因:数据类型不同. 问题处理:为了不让广大朋友再烦恼,我就把解决办法写出来吧,把数据库中我们要查询的字段类型改 ...
随机推荐
- spring中ApplicationContextAware接口描述
项目中使用了大量的工厂类,采用了简单工厂模式: 通过该工厂类可以获取指定的处理器bean,这些处理器bean我们是从spring容器中获取的,如何获取,是通过实现ApplicationContextA ...
- pandas读取MySql/SqlServer数据 (转)
在 Anacondas环境中,conda install pymssql ,一直报包冲突,所以采用先在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#nump ...
- logback使用介绍
参考 https://www.cnblogs.com/warking/p/5710303.html
- socket 断线重连
send发送数据时,发送失败,进行如下重连处理: ) < )//serbuf中有数据可以发送才会执行这条语句 { printf("serial to tcp send msg erro ...
- golang 字符串统计
golang内建只认utf8 如果传递的字符串里含有汉字什么的,最好使用 utf8.RuneCountInString() 统计 字符串统计几种方法: - 使用 bytes.Count() 统计- 使 ...
- MobaXterm的一些介绍(Top 5 SSH Clients for Windows (Alternatives of PuTTY))
Top 5 SSH Clients for Windows (Alternatives of PuTTY) http://tecadmin.net/top-5-ssh-clients-for-wind ...
- console call的fallback console 兼容
(function() { var noop = function noop() {}; var methods = [ 'assert', 'clear', 'count', 'debug', 'd ...
- VC禁止或允许拖拽改变窗口尺寸
创建窗口时用WS_THICKFRAME控制 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CMDIFrameWndEx:: ...
- 测试教程网.unittest教程.6. 命令行接口
From: http://www.testclass.net/pyunit/commind_line_interface/ 背景 unittest支持命令行接口,我们可以在命令行里指定运行具体的测试用 ...
- 关于“最小的K个数”问题
从一堆无序的数中(共n个数)找到最小的K个数,这也算是一道比较经典的题目了,关于这道题目的解法,一般有几种: 方法1:先对所有的数据进行排序,然后直接找出前K个数来,即最小的K个数.时间复杂度为O(N ...