android BLE Peripheral 做外设模拟设备,供ios、android 连接通讯。
为了能让其它设备可以发现其设备,先启动特定广播。看自己需要什么广播格式。
对于广播可见的mac address:
在调用startAdvertising();时,mac address 就会改变。
并且跟mBluetoothAdapter.getAddress();获取到的蓝牙mac 地址不一样。
这是因为在android 5.0 之后,为了保护真正的mac address。
在广播出来的地址,是经过随机可解析隐秘转换的(Resolvable private address)。
所以要在广播数据中添加mac address 输出,就要用静态蓝牙地址。
BLE设备的地址类型:
一个BLE设备,可以使用两种类型的地址(一个BLE设备可同时具备两种地址):
1、 Public Device Address (公有地址)
该地址需要向IEEE申请(购买),IEEE保证地址的唯一性。
2、Random Device Address (随机地址)
设备地址不是固定分配的,而是在设备设备启动后随机生成的。
Random Device Address分为:
(1)Static Device Address (静态地址)
在一个上电周期内保持不变。地址随机生成。
(2)Private Device Address (私有地址)
通过定时更新和地址加密两种方法,提高蓝牙地址的可靠性和安全性。
Private Device Address分为
1) Non-resolvable Private Address (不可解析私有地址)
会定时更新。以T_GAP(private_addr_int)为周期,建议15分钟。
2) Resolvable Private Address (可解析私有地址)
以T_GAP(private_addr_int)为周期,定时更新。哪怕在广播、扫描、已连接等过程中,也可能改变。它通过一个随机数和一个称作identity resolving key (IRK) 的密码生成,因此只能被拥有相同IPK的设备扫描到,可以防止被未知设备扫描和追踪。
android 对外发出广播,都一样,只是更改其中的方法:
android BLE Peripheral 模拟 ibeacon 发出ble 广播
开始广播: 增加使用BluetoothGattServer
public void startAdvertising(MockServerCallBack callBack) {
//获取BluetoothLeAdvertiser,BLE发送BLE广播用的一个API
if (mBluetoothAdvertiser == null) {
mBluetoothAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
}
//创建BluetoothGattServerCallback,
// MockServerCallBack这个类继承自BluetoothGattServerCallback
// BluetoothGattServerCallback这个回调类主要是一些BLE读写的接口
// 关于BLE读写的操作都在这个Callback中完成
if (mBluetoothAdvertiser != null) {
mMockServerCallBack = callBack;
//打开BluetoothGattServer
mGattServer = mBluetoothManager.openGattServer(mActivity, mMockServerCallBack);
if (mGattServer == null) {
Log.e(TAG, "gatt is null");
}
try {
mMockServerCallBack.setupServices(mActivity, mGattServer);
mBluetoothAdvertiser.startAdvertising(createAdvSettings(true, 0)
, createAdvertiseData(BluetoothUUID.bleServerUUID), mAdvCallback);
} catch (Exception e) {
Log.v(TAG, "Fail to setup BleService");
}
}
isAdvertising = true;
}
创建广播,添加serviceUuid,广播内容,自行决定。
addServiceUuid(ParcelUuid serviceUuid) 的作用。
广播数据加入serviceUuid , 当其它设备扫描时,就可以根据此serviceUuid进行特定扫描.
扫描BLE设备的时候:启动扫描时,有可选参数,传入uuid数组。
BluetoothAdapter
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback);
使用该函数启动扫描的,会根据所传入的uuid,去扫描过滤,只返回符合条件的设备。
注意:部分手机可能设置uuid后,扫描不到设备,可能底层扫描问题。
public static AdvertiseData createAdvertiseData(UUID proximityUuid) {
AdvertiseData.Builder builder = new AdvertiseData.Builder();
builder.addManufacturerData(0x0301, new byte[]{0x01, 0x03});
builder.addServiceUuid(ParcelUuid.fromString(BluetoothUUID.bleServerUUID.toString())
AdvertiseData adv = builder.build();
return adv;
}
BluetoothGattServerCallback:服务事件的回调
打开notification 对应的value为 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
打开indication 对应的value为 BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
关闭notification 对应的value均为BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
服务事件响应过程:
(1) 当客户端开始写入数据时: 触发回调方法 onDescriptorWriteRequest
(2) 在 onDescriptorWriteRequest 方法中,执行下面的方法表示 写入成功 BluetoothGatt.GATT_SUCCESS
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
2 // 执行 sendResponse后,会触发回调方法 onCharacteristicWriteRequest
(3) 在 onCharacteristicWriteRequest方法中:
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId
, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] requestBytes) {
// 这个里可以获得 来自客户端发来的数据 requestBytes
}
(4) 处理响应内容
BluetoothGattServerCallback :
public class MockServerCallBack extends BluetoothGattServerCallback {
public void setupServices(Context context, BluetoothGattServer gattServer) throws InterruptedException {
if (gattServer == null) {
throw new IllegalArgumentException("gattServer is null");
}
mGattServer = gattServer;
// 设置一个GattService以及BluetoothGattCharacteristic
BluetoothGattService service = new BluetoothGattService(BluetoothUUID.bleServerUUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattService service2 = new BluetoothGattService(BluetoothUUID.bleServerUUID2,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
//add a read characteristic.
// 当是ios设备连接过来时,需添加BluetoothGattCharacteristic.PROPERTY_INDICATE或者notify进行兼容。
mCharacteristicRead = new BluetoothGattCharacteristic(BluetoothUUID.readDataUUID
, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_INDICATE
, BluetoothGattCharacteristic.PERMISSION_READ);
//add a descriptor
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(BluetoothUUID.CLIENT_CHARACTERISTIC_CONFIG
, BluetoothGattCharacteristic.PERMISSION_WRITE);
mCharacteristicRead.addDescriptor(descriptor);
service.addCharacteristic(mCharacteristicRead);
BluetoothGattCharacteristic write = new BluetoothGattCharacteristic(
BluetoothUUID.writeDataUUID,
BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_INDICATE,
BluetoothGattCharacteristic.PERMISSION_WRITE);
service.addCharacteristic(write);
if (mGattServer != null && service != null) {
mGattServer.addService(service);
mGattServer.addService(service2);
}
}
//当添加一个GattService成功后会回调改接口。
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onServiceAdded status=GATT_SUCCESS service=" + service.getUuid().toString());
} else {
Log.d(TAG, "onServiceAdded status!=GATT_SUCCESS");
}
}
//BLE设备连接状态发生改变后回调的接口
public void onConnectionStateChange(android.bluetooth.BluetoothDevice device, int status,
int newState) {
super.onConnectionStateChange(device, status, newState);
Log.e(TAG, String.format("1.onConnectionStateChange:device name = %s, address = %s"
, device.getName(), device.getAddress()));
Log.d(TAG, "onConnectionStateChange status=" + status + "->" + newState);
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
btClient = null; // 移除客户端连接设备
}
}
//当有客户端来读数据时回调的接口
/**
* 特征被读取。当回复响应成功后,客户端会读取然后触发本方法,
*/
public void onCharacteristicReadRequest(android.bluetooth.BluetoothDevice device,
int requestId, int offset, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
characteristic.setValue(new byte[]{0x03, 0x01});
Log.e(TAG, String.format("1.onCharacteristicReadRequest:device name = %s, address = %s"
, device.getName(), device.getAddress()));
Log.e(TAG, String.format("onCharacteristicReadRequest:requestId = %s, offset = %s", requestId, offset));
boolean result = mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, characteristic.getValue());
Log.e(TAG, "read request send response:" + result);
}
//当有客户端来写数据时回调的接口
/**
* 接受具体数据字节
*/
@Override
public void onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice device,
int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite,
boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
// 需调用 sendResponse 来响应,为了保持连接。
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
// 处理其它设备写进来的数据
value. // 处理数据 byte[] value,记住连接设备
}
// 当有客户端来写Descriptor 时回调的接口
/** 描述被写入时,在这里执行bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS...) 时
* 触发onCharacteristicWriteRequest **/
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
Log.d(TAG, "onDescriptorWriteRequest:" + Arrays.toString(value));
// now tell the connected device that this was all successfull
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
}
通过adb 获取 蓝牙 mac address:
adb shell settings get secure bluetooth_address
或者
// for Android 4.4.4
adb shell service call bluetooth_manager 10
// for Android 5.0+
adb shell service call bluetooth_manager 12
编程获取bluetooth mac address :
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
推荐文章:
android BLE Peripheral 做外设模拟设备,供ios、android 连接通讯。的更多相关文章
- android BLE Peripheral 手机模拟设备发出BLE广播 BluetoothLeAdvertiser
android 从4.3系统开始可以连接BLE设备,这个大家都知道了.iOS是从7.0版本开始支持BLE. android 进入5.0时代时,开放了一个新功能,手机可以模拟设备发出BLE广播, 这个新 ...
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...
- android BLE Peripheral 模拟 ibeacon 发出ble 广播
Android对外模模式(peripheral)的支持: 从Android 5.0+开始才支持. api level >= 21 所以5.0 之前设备,是不能向外发送广播的. Android中心 ...
- Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信
Android BLE与终端通信(三)--客户端与服务端通信过程以及实现数据通信 前面的终究只是小知识点,上不了台面,也只能算是起到一个科普的作用,而同步到实际的开发上去,今天就来延续前两篇实现蓝牙主 ...
- Android BLE与终端通信(三)——client与服务端通信过程以及实现数据通信
Android BLE与终端通信(三)--client与服务端通信过程以及实现数据通信 前面的终究仅仅是小知识点.上不了台面,也仅仅能算是起到一个科普的作用.而同步到实际的开发上去,今天就来延续前两篇 ...
- Android与IOS的优缺点比较 对 Android 与 IOS 比较是个个人的问题。 就好比我来说,我两个都用。我深知这两个平台的优缺点。所以,我决定分享我关于这两个移动平台的观点。另外,然后谈谈我对新的 Ubuntu 移动平台的印象和它的优势。 IOS 的优点 虽然这些天我是个十足的 Android 用户,但我必须承认 IOS 在某些方面做的是不错。首先,苹果公司在他们的设备更新方面有更
Android与IOS的优缺点比较 对 Android 与 IOS 比较是个个人的问题. 就好比我来说,我两个都用.我深知这两个平台的优缺点.所以,我决定分享我关于这两个移动平台的观点.另外,然后谈谈 ...
- Android:BLE智能硬件开发详解
目录 前言 BLE是个什么鬼 BLE中的角色分工 主要的关键词和概念 GATT(Generic Attribute Profile ) Characteristic Service Android如何 ...
- BLE简介和Android BLE编程
一.BLE和BT区别 其实我知道许多程序员不太喜欢阅读除了代码以外的文档,因为有时这些过于冗长的文档对编程并没有更多的好处,有了协议,接口,demo差不多很多人就能写出很好质量的代码了.但其实更深入的 ...
随机推荐
- golang _下划线占位符代替需要释放的资源的问题
golang中_有两种作用,一种用在import中,比如这样 import _ "github.com/go-sql-driver/mysql" 表示并不需要导入整个包,只是执行这 ...
- SQL SERVER获取信息的方法
获取数据库的表 SELECT obj.name tablename, schem.name schemname, CAST ( CASE ) ) END AS BIT) HasPrimaryKey f ...
- proxysql系列 ~ 运维相关
一 常用命令 //实时加载 load mysql servers to runtime; mysql_server load mysql users to runtime; mysql_u ...
- Jenkins的配置从节点中默认没有Launch agent via Java Web Start选项问题
Jenkins的配置从节点中默认没有Launch agent via Java Web Start,如下图所示,而这种启动方式在Windows上是最方便的. 如何设置才能让出来呢? 1:打开" ...
- vue中数据添加完成以后,数据回显
1.格式 <FormItem label="奖品领取类型:" prop="getType" > <RadioGroup v-model=&qu ...
- vue-router路由动态传参query和params的区别
1.query方式传参和接收参数 //路由 { path: '/detail', //这里不需要参入参数 name: "detail", component: detail//这个 ...
- java----tomcat
下载: 测试使用一般下载zip格式,在服务器上布置tomcat使用直接安装格式(install) https://tomcat.apache.org/download-90.cgi 使用 在windo ...
- 测开之路十:函数&参数
def 函数名(): 函数代码块 return 返回值 参数 必备参数:位置参数,调用函数时必须有值传入 默认参数,调用如果没有传值使用默认值 不定长参数 *args:想传多少传多少,必须放在位置参数 ...
- python学习-Pillow图像处理
Pillow中文文档:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html 安装:pip install pillo ...
- Jenkins Vue项目自动构建以及构建后续操作
Jenkins在linux上的安装教程:http://www.ityouknow.com/springboot/2017/11/11/springboot-jenkins.html 另外,关于在win ...