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开发,扫描、连接、发送和读取信息;的更多相关文章

  1. Android 蓝牙 BLE 开发笔记

    最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE.Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了 ...

  2. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

  3. Android蓝牙——HID开发

    代码地址如下:http://www.demodashi.com/demo/13891.html 原文地址: https://blog.csdn.net/VNanyesheshou/article/de ...

  4. Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...

  5. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...

  6. Android蓝牙BLE低功耗相关简单总结

    在看Android4.42的源代码时看到有加入对BLE设备的处理.看的一头雾水,多方百度,最终有种柳暗花明的感觉. 本文总结来源于百度多篇文章,欢迎转载.分享交流 BLE蓝牙概念 BLE:Blueto ...

  7. Qt on Android 蓝牙通信开发

    版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...

  8. android蓝牙通讯开发(详细)

    新建一个工程之后,我们可以先看到界面左边的项目栏,我们可以看到,除了app目录以外,大多数的文件和目录都是自动生成的,我们也不需要对他们进行修改,而app目录之下的文件才是我们工作的重点.下面,我先对 ...

  9. Android问题-DelphiXE5开发Andriod连接Webservice乱码问题

    问题现象:在使用DelphiXE5开发Andriod连接Webservice乱码. 问题原因:数据类型不同. 问题处理:为了不让广大朋友再烦恼,我就把解决办法写出来吧,把数据库中我们要查询的字段类型改 ...

随机推荐

  1. flutter 控制台快捷键

  2. [转]HTML 简介

    HTML 实例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  3. React V16.x 生命周期调整

    旧声明周期: table th:nth-of-type(5) { width: 400px; } 生命周期 属于阶段 调用次数 是否可以setState 作用 getDefaultProps 创建阶段 ...

  4. elastic 部分更新 retry_on_conflict 和 数据库写锁 详细比对

    1 数据库的  update 在修改这条数据的的过程中(这个过程指的是 数据库执行update 到 事务提交的过程中 )为这条数据加上 写锁,阻止 别的事务 对锁定数据的修改,请求后一个修改事务的线程 ...

  5. Reshape以及向量机分类学习和等高线绘制代码

    首先科普一下python里面对于数组的处理,就是如果获取数组大小,以及数组元素数量,这个概念是不一样的,就是一个size和len处理不用.老规矩,上代码: arr2 = np.array([-19.5 ...

  6. 解决The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    Spring Boot JPA 使用Mysql是出现如下错误: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represe ...

  7. split与re.split/捕获分组和非捕获分组/startswith和endswith和fnmatch/finditer 笔记

    split()对字符串进行划分: >>> a = 'a b c d' >>> a.split(' ') ['a', 'b', 'c', 'd'] 复杂一些可以使用r ...

  8. mysql update 将一个表某字段设为另一个表某字段的值

    表新添加了一个字段,毫无疑问是空值.所以想将另一个表的某个字段的值写入到修改的表中. sql语句不复杂,但还是记录一下,因为也查了一会,以后说不定还会用到. mysql> update cent ...

  9. RichEdit选中文字右键菜单的实现

    procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: ...

  10. debian的python蓝牙库

    sudo apt--dev pip install bluepy