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. day6大纲

    01 昨日内容回顾 字典: 增: setdefault() 有责不变,无责添加 dic['key'] = value 删: pop 按照key pop('key') pop('key',None) p ...

  2. springfox+swagger2生成API文档

    1.建立一个spring mvc工程: 2.添加POM依赖: <properties> <springfoxversion>2.6.1</springfoxversion ...

  3. javascript, jquery, nodejs学习2

    debug tools http://stackoverflow.com/questions/1911015/how-to-debug-node-js-applications node inspec ...

  4. 洛谷 4383 [八省联考2018]林克卡特树lct——树形DP+带权二分

    题目:https://www.luogu.org/problemnew/show/P4383 关于带权二分:https://www.cnblogs.com/flashhu/p/9480669.html ...

  5. 反转链表 II

    反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2, ...

  6. JMeterPlugins插件监听器学习-监听器

    JMeterPlugins插件监听器学习-监听器 1.jp@gc - Actiive Threads Over Time:不同时间活动用户数量展示(图表)2.jp@gc - AutoStop List ...

  7. python kafka client--confluent-kafka-python

    项目中需要使用python 向Kafka生产和消费数据,最初使用pykafka .后来发现pykafka不支持client.id. 最后,终于找到confluent-kafka. python kaf ...

  8. [转] openwrt关闭调试串口

    转自: http://wiki.wrtnode.com/index.php?title=Release_UART/zh-cn 由于mt7620n只有一个UART lite接口,在原生的OpenWrt中 ...

  9. spring-jar包及架构介绍

    查看博客: http://www.cnblogs.com/ywlaker/p/6136625.html

  10. .net(C#)常见面试题

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...