掃描
掃描設備需要通過BletoothManager獲取到BluetoothAdapter對象進行掃描,在BluetoothAdapter.LeScanCallback回調中返回結果:
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.startLeScan(mLeScanCallback);
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
//掃描結果
}
};
mBluetoothAdapter.stopLeScan(mLeScanCallback); //停止掃描
調用掃描到的設備BluetoothDevice的connectGatt接口進行連接,通過回調BluetoothGattCallback返回連接結果:
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
連接後調用BluetoothGatt的discoverServices發現服務後進行傳輸數據。
傳輸數據
服務是設備的屬性,特徵是服務的屬性,描述是特徵的屬性,暫時這麼理解吧。發數據和收數據都是通過讀特徵值,寫特徵值進行的。服務,特徵,描述都有各自的UUID,可以通過UUID區分他們。
讀數據之前要設置特徵的notify屬性,也就是設置讀取的數據可以通過回調返回,不然收不到數據。設置特徵notify需要調用BluetoothGatt的setCharacteristicNotify(mCharacter ,true)和修改對應特徵的描述纔行。
所有這些操作都是在BluetoothGattCallback回調中收到結果。
mBluetoothGatt.discoverServices(); //對應onServicesDiscovered
mBluetoothGatt.setCharacteristicNotification(mCharacter, true); //對應onCharacteristicChanged
mBluetoothGatt.readCharacteristic(mCharacter); //對應onCharacteristicRead
mBluetoothGatt.writeCharacteristic(mCharacter); //對應onCharacteristicWrite
mBluetoothGatt.readDescriptor(mDescriptor); //對應onDescriptorRead
mBluetoothGatt.writeDescriptor(mDescriptor); //對應onDescriptorWrite
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(LOG_TAG, "onConnectionSateChange staus = " + status + ",newState = " + newState);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(LOG_TAG, "onServicesDiscovered status = " + status);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.d(LOG_TAG, "onCharacteristicChanged");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(LOG_TAG, "onCharacteristicRead");
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(LOG_TAG, "onCharacteristicWrite");
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d(LOG_TAG, "onDescriptorRead");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d(LOG_TAG, "onDescriptorWrite");
}
};
獲取服務,特徵,描述
BluetoothGattService mService;
BluetoothGattCharacteristic mCharacter;
BluetoothGattDescriptor mDescriptor;
mBluetoothGatt.getServices();
mService = mBluetoothGatt.getService(mServiceUUID);
mService.getUuid();
mService.getCharacteristics();
mCharacter = mService.getCharacteristic(mCharacterUUID);
mCharacter.getUuid();
mCharacter.getDescriptors();
mDescriptor = mCharacter.getDescriptor(mDescriptorUUID);
mDescriptor.getUuid();