赞
踩
权限适配参考
https://juejin.cn/post/7320169878644031488
https://www.cnblogs.com/fly263/p/16715525.html
https://blog.csdn.net/qq_17189617/article/details/135291784
流写数据
https://cloud.tencent.com.cn/developer/article/2310635
https://www.jb51.net/article/263206.htm
* 客户端设置 * 单击事件 * */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String s = arrayAdapter.getItem(position); String address = s.substring(s.indexOf(":") + 1).trim();//获取蓝牙IP try { if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery();//若当前蓝牙被使用,则关闭重新启用 } try { if (device == null) {//若未连接,则获得远程设备 device = bluetoothAdapter.getRemoteDevice(address); } if (clientSocket == null) { clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID); clientSocket.connect();//连接蓝牙 os = clientSocket.getOutputStream();//客户端向服务端输出文本 } } catch (IOException e) { e.printStackTrace(); } if (os != null) { os.write("发送信息到其他设备".getBytes("utf-8")); } } catch (Exception e) { e.printStackTrace(); } }
https://www.cnblogs.com/Free-Thinker/p/11507349.html
判断服务是否有读或者写权限 用或运算
// 获取特征的 UUID
val characteristicUUID = characteristic.uuid
// 检查特征的属性
val properties = characteristic.properties
// 判断特征是否可读写
if (properties and (BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
// 特征可读写,打印其 UUID
LogUtils.e("Characteristic UUID", characteristicUUID.toString())
}
判断服务是否有 读写 通知权限 这边用与运算
@SuppressLint("MissingPermission") fun findGattService(gatt: BluetoothGatt?): BluetoothGattService? { var service: BluetoothGattService? = null if (gatt != null) { LogUtils.e("findGattService 当前设备: ${gatt.device.name}") val bgsList = gatt.services label@ for (gattService in bgsList) { val uuid = gattService!!.uuid.toString() if (gattService.type == BluetoothGattService.SERVICE_TYPE_PRIMARY) { } LogUtils.e(" gattService服务UUID $uuid ${gattService.type} ") for (characteristic in gattService.characteristics) { // 获取特征的 UUID val characteristicUUID = characteristic.uuid // 检查特征的属性 val properties = characteristic.properties // 判断特征是否可读写 if (properties and (BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) { // 特征可读写,打印其 UUID LogUtils.e("Characteristic UUID", characteristicUUID.toString()) } var isRead = (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_READ) > 0 var isWrite = (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 var isNotify = (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0 if (isRead) { UUID_RECEIVE_CHARACTERISTIC = characteristic.uuid.toString() LogUtils.e("写特征 $UUID_RECEIVE_CHARACTERISTIC") } if (isWrite) { UUID_SEND_CHARACTERISTIC = characteristic.uuid.toString() LogUtils.e("读特征 $UUID_SEND_CHARACTERISTIC") } if (isNotify) { CLIENT_CHARACTERISTIC_CONFIG = characteristic.uuid.toString() LogUtils.e("通知特征 $CLIENT_CHARACTERISTIC_CONFIG") } if (isWrite and isWrite and isNotify) { UUID_GATT_SERVICE = gattService.uuid.toString() service = gattService break@label } } } } return service }
特征判断参考
https://blog.51cto.com/u_16213401/9599749
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。