какой UUID следует использовать для чтения характеристик с определенного устройства Bluetooth? Насколько я знаю, для этого не нужно сопряжение, я должен подключиться к устройству и получить данные
Мой код правильный?
Также mBluetoothGattService
равно нулю!
Как я могу получить данные с конкретного устройства?
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getName() != null && device.getName().equals(BluetoothName)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
if (device.getUuids() != null) {
for (int i = 0; i <= device.getUuids().length; i++) {
Log.d(TAG, "onReceive: " + device.getUuids()[i].getUuid().toString());
}
}
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
return;
}
device.connectGatt(context, true, new BluetoothGattCallback() {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService mBluetoothGattService = mBluetoothGatt.getService(BTMODULEUUID);
if (mBluetoothGattService != null) {
Log.i(TAG, "Service characteristic UUID found : " + mBluetoothGattService.getUuid().toString());
} else {
Log.i(TAG, "Service characteristic not found for UUID : " + BTMODULEUUID);
}
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gatt.requestMtu(512);
}
}
mBluetoothGatt = gatt;
Log.d(TAG, "discoverServices Size : " + mBluetoothGatt.getServices().size());
for (BluetoothGattService s : mBluetoothGatt.getServices()) {
Log.d(TAG, "discoverServices : found " + s.getUuid());
for (BluetoothGattCharacteristic c : s.getCharacteristics()) {
Log.d(TAG, "--> characteristic : " + c.getUuid() + ":" + String.format("%x", c.getInstanceId()));
}
}
super.onConnectionStateChange(gatt, status, newState);
Log.d(TAG, "onReceive connectGatt.");
readCustomCharacteristic(mBluetoothAdapter);
}
});
}
}
}
public void readCustomCharacteristic(BluetoothAdapter mBluetoothAdapter) {
if (mBluetoothAdapter == null) {
return;
}
if (mBluetoothGatt == null) {
return;
}
// Is the service available on the device
BluetoothGattService mCustomService = mBluetoothGatt.getService(convertUuidFromInteger(0x181D));// BTMODULEUUID not worked too
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
// Read the characteristic from the service
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(BTMODULEUUID);
if (!mBluetoothGatt.readCharacteristic(mReadCharacteristic)) {
Log.w(TAG, "Failed to read characteristic");
}
}
LogCat показывает:
D: DiscoverServices Размер: 0
D: onReceive connectGatt.
W: Пользовательская служба BLE не найдена
which UUID
это то, что должна содержать документацияspecific Bluetooth device
. Никто этого не знает. - person Vladyslav Matviienko   schedule 14.01.2019Unknown Service UUID
не работает - person Hossein Kurd   schedule 14.01.2019BluetoothGatt
вonConnectionStateChange
внутри BroadcastReceiver? - person Hossein Kurd   schedule 14.01.2019