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. 没有了CommonsChunkPlugin,咱拿什么来分包(译)

    webpack 4 移除 CommonsChunkPlugin,取而代之的是两个新的配置项(optimization.splitChunks 和 optimization.runtimeChunk). ...

  2. Python的rand vs randn以及linspace

    Numpy里面的randn是满足了整体分布的,normal distribution(正态分布):rand则是满足了Uniform Distribution(均匀分布): Linspace(start ...

  3. FireDAC内部初探

    procedure TForm1.Button1Click(Sender: TObject); var FCommIntf: IFDPhysCommand; //FireDAC.Phys.Intf F ...

  4. padding margin

    padding:内边距 margin:外边距 内外边距是针对本标签相对于它相邻的标签而言. margin: 10px;是本标签与它周围上下左右有10像素的空白. padding: 10px;本标签其实 ...

  5. Windows Azure Virtual Machine (36) 扩展Azure ARM VM的磁盘大小

    <Windows Azure Platform 系列文章目录> 在默认情况下,Azure ARM VM的操作系统磁盘(OS Disk),容量为: (1)Windows VM OS Disk ...

  6. sql 语句收集

    SELECT type as 'plan cache store', buckets_count  FROM sys.dm_os_memory_cache_hash_tables WHERE type ...

  7. sysroot和prefix

    --with-sysroot用来指定系统的root.该选项主要用于新系统(比如LFS)构建或交叉编译.比如你的LFS的root在/mnt/lfs,那么configure时指定--with-sysroo ...

  8. 胖子哥的大数据之路(11)-我看Intel&&Cloudera的合作

    一.引言 5月8日,作为受邀嘉宾,参加了Intel与Cloudera在北京中国大饭店新闻发布会,两家公司宣布战略合作,该消息成为继Intel宣布放弃大数据平台之后的另外一个热点新闻.对于Intel的放 ...

  9. ALGO-151_蓝桥杯_算法训练_6-2递归求二进制表示位数

    记: 进制转换 AC代码: #include <stdio.h> #define K 2 int main(void) { ; scanf("%d",&n); ...

  10. How HipChat Stores And Indexes Billions Of Messages Using ElasticSearch And Redis[转]

    This article is from an interview with Zuhaib Siddique, a production engineer at HipChat, makers of  ...