03.BLE DataTranfor
Client
- 扫描设备:
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.startLeScan(mScanCallBack)
mScanCallBack
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// 可以拿到远程设备和rssi设备信号强度
}
}
- Connecting to a GATT Server
BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback)
回调函数
**特征**
* onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
* onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
* onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
**状态改变**
* onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
**描述**
* onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
* onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
* onMtuChanged(BluetoothGatt gatt, int mtu, int status)
* onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)
* onReliableWriteCompleted(BluetoothGatt gatt, int status)
**发现设备**
* onServicesDiscovered(BluetoothGatt gatt, int status)
*
bluetoothGatt 读写数据
- readCharacteristic(BluetoothGattCharacteristic characteristic)
- readDescriptor(BluetoothGattDescriptor descriptor)
- readRemoteRssi()
- requestConnectionPriority(int connectionPriority)
- requestMtu(int mtu)
- setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable)
- writeCharacteristic(BluetoothGattCharacteristic characteristic)
- writeDescriptor(BluetoothGattDescriptor descriptor)
Server
设置广播 Ad
- 广播设置 AdvertiseSettings.Builder
- 广播数据设置 AdvertiseData.Builder
connect to gattServer
mBluetoothManager = (BluetoothManager) mContex.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
sGattServer = mBluetoothManager.openGattServer(mContex, callback);
//添加service, Characteristic , Descriptor
// callBack
- onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic)
- onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
- onConnectionStateChange(BluetoothDevice device, int status, int newState)
- onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor)
- onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
- onExecuteWrite(BluetoothDevice device, int requestId, boolean execute)
- onNotificationSent(BluetoothDevice device, int status)
- onServiceAdded(int status, BluetoothGattService service)
client Server 之间的数据流程传输控制。
//白板画图