Android BLE订阅ios的ANCS另一个方式

背景

受到 https://www.jianshu.com/p/88858b8e5e67 的启发,我明确了要发现ANCS服务需要在配对并绑定蓝牙后。此文中的办法是在 ios侧使用 lightblue 模拟一个外设Heart Rate,让Android 搜索连接它,然后触发配对绑定,再接着搜索ANCS服务。操作起来还是比较繁琐。

我期待的是:在ios 的【设置-蓝牙】可以直接点击Android手机蓝牙设备名称,能配对成功并同时android能订阅ANCS。

简化操作方式

  1. 首先需要Android手机作为外设广播数据,其中的一个GATT服务uuid必须是ios可见的,经过测试,HID的服务是可见的,uuid为 1812 (00001812-0000-1000-8000-00805f9b34fb)

  2. 接着,在ios设置中,搜索蓝牙的界面选中第一步android广播出来的外设名称,此时ios会去连接此android外设,将会进入BluetoothGattServerCallback的回调事件

  3. 在BluetoothGattServerCallback的已连接事件中,先关闭GattServer。接着判断连接上的bt device(ios)是否已经绑定?

如果绑定:

mIphoneDevice.connectGatt(getApplicationContext(), false, mGattCallback);

如果未绑定:


try {

createBond(device.getClass(),device);

}catch (Exception e) {

e.printStackTrace();

}

//然后在绑定成功的广播接收者中,调用mIphoneDevice.connectGatt(getApplicationContext(), false, mGattCallback);

  1. 此时android作为外设的使命完成了,就是为了拿到bt device并配对绑定。

  2. android转换角色为中央设备,对上面的bt device展开搜索服务、订阅通知等

  3. 接下来就是在ANCS的数据源,控制源,通知源中依据ANCS的协议进行显示、控制、数据解析等等了

具体的demo见 github:https://github.com/billzbh/Androdi-testANCSDemo

demo我把ios通知的内容直接发到android的通知栏里了,感觉也蛮有趣!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Android BLE通信工具类,包含连接、发现服务、读写特征等基本操作。 ``` public class BLEManager { private BluetoothManager bluetoothManager; private BluetoothAdapter bluetoothAdapter; private BluetoothGatt bluetoothGatt; private BluetoothGattCharacteristic characteristic; private Context context; private String targetDeviceAddress; public BLEManager(Context context, String targetDeviceAddress) { this.context = context; this.targetDeviceAddress = targetDeviceAddress; bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); } public void connect() { BluetoothDevice device = bluetoothAdapter.getRemoteDevice(targetDeviceAddress); bluetoothGatt = device.connectGatt(context, false, gattCallback); } public void disconnect() { if (bluetoothGatt != null) { bluetoothGatt.disconnect(); bluetoothGatt.close(); bluetoothGatt = null; } } public void writeData(byte[] data) { if (characteristic != null) { characteristic.setValue(data); bluetoothGatt.writeCharacteristic(characteristic); } } public void readData() { if (characteristic != null) { bluetoothGatt.readCharacteristic(characteristic); } } public void discoverServices() { bluetoothGatt.discoverServices(); } public List<BluetoothGattService> getGattServices() { if (bluetoothGatt != null) { return bluetoothGatt.getServices(); } return null; } public void setCharacteristic(String serviceUUID, String characteristicUUID) { if (bluetoothGatt != null) { BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUUID)); characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID)); } } private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { gatt.discoverServices(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { // TODO: handle service discovery success } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { byte[] data = characteristic.getValue(); // TODO: handle read data } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { // TODO: handle write success } } }; } ``` 使用示例: ``` BLEManager bleManager = new BLEManager(context, targetDeviceAddress); bleManager.connect(); bleManager.discoverServices(); List<BluetoothGattService> services = bleManager.getGattServices(); bleManager.setCharacteristic(serviceUUID, characteristicUUID); bleManager.writeData(data); bleManager.readData(); bleManager.disconnect(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值