获取 gattService 失败。 BluetoothGatt 返回但没有任何服务

发布于 2025-01-11 11:37:52 字数 7471 浏览 0 评论 0原文

我无法获取 BT 服务列表

mBluetoothGatt.getService 方法返回 null 因为 mDevice.connectGatt 方法返回没有服务的 BluetoothGatt(大小 0)

我确信我正在使用的 BT 设备工作正常

这是我的第一个任务在一份新工作中,尽快解决它对我来说非常重要,

我错过了一些东西,但不知道是什么......

非常感谢

public class TagService extends Service {
String TAG = "TagService";

public static boolean running;
public e_status status;

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;

enum e_status {
    none, init, searchDevice, foundDevice, discoverServices, sendHeartBeat, finish
}

@Override
public void onCreate() {
    super.onCreate();
    runThread();
}

@Override
public void onDestroy() {
    super.onDestroy();
    running = false;
}

@SuppressLint("MissingPermission")
private void runThread() {
    status = e_status.none;
    running = true;

    new Thread(() -> {
        while (running && status != e_status.finish) {
            int interval=4000;

            try {
                if (status == e_status.none) {
                    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                    mBtAdapter = mBluetoothManager.getAdapter();
                    setStatus(e_status.init);
                }

                if (status == e_status.init) {
                    mScanner = mBtAdapter.getBluetoothLeScanner();
                    ArrayList<ScanFilter> filters = new ArrayList<>();
                    ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
                    filters.add(tagFilter);
                    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
                    setStatus(e_status.searchDevice);
                    mScanner.startScan(filters, settings, getScanCallback());
                }

                if (status == e_status.foundDevice) {
                    mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
                    if (mBluetoothGatt != null) {
                        setStatus(e_status.discoverServices);
                        mBluetoothGatt.discoverServices();
                    }
                }

                if (status == e_status.discoverServices) {
                    mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
                    if (mService != null) {
                        setStatus(e_status.sendHeartBeat);
                        //
                        // this is no happen!!!
                        //
                        // TODO
                        //BluetoothGattCharacteristic charac = mService.getCharacteristic(ConstParams.serviceUUID);
                        //mBluetoothGatt.writeCharacteristic(characteristic);
                        //mService.addCharacteristic(charac, value));
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG, "error" ,ex);
                interval=30000;
            }

            sleep(interval);
        }
    }).start();
}

private ScanCallback getScanCallback() {
    return new ScanCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
            setStatus(e_status.foundDevice);
            mScanner.stopScan(this);
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            setStatus(e_status.init);
        }
    };
}
private void setStatus(e_status newStatus) {
    Log.i(TAG, "status: " + status + " -> " + newStatus);
    status = newStatus;
}

private BluetoothGattCallback getGattCallback() {
    return new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyUpdate");
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyRead");
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
            super.onConnectionStateChange(gatt, status1, newState);
            Log.i(TAG, "onConnectionStateChange" + status + "," + newState);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered");
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicRead");
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite");
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.i(TAG, "onCharacteristicChanged");
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorRead");
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorWrite");
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            Log.i(TAG, "onReliableWriteCompleted");
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            Log.i(TAG, "onReadRemoteRssi");
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
            Log.i(TAG, "onMtuChanged");
        }

        @Override
        public void onServiceChanged(@NonNull BluetoothGatt gatt) {
            super.onServiceChanged(gatt);
            Log.i(TAG, "onServiceChanged");
        }
    };
}

private void sleep(long millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

I could not get the BT services list

The method mBluetoothGatt.getService return null Because the method mDevice.connectGatt return BluetoothGatt with no services (size 0)

I know for sure that the BT Device I am working with is working properly

This is my first task in a new job and it is very important for me to solve it as quickly as I can

I'm missing something but do not know what ...

Thanks a lot

public class TagService extends Service {
String TAG = "TagService";

public static boolean running;
public e_status status;

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;

enum e_status {
    none, init, searchDevice, foundDevice, discoverServices, sendHeartBeat, finish
}

@Override
public void onCreate() {
    super.onCreate();
    runThread();
}

@Override
public void onDestroy() {
    super.onDestroy();
    running = false;
}

@SuppressLint("MissingPermission")
private void runThread() {
    status = e_status.none;
    running = true;

    new Thread(() -> {
        while (running && status != e_status.finish) {
            int interval=4000;

            try {
                if (status == e_status.none) {
                    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                    mBtAdapter = mBluetoothManager.getAdapter();
                    setStatus(e_status.init);
                }

                if (status == e_status.init) {
                    mScanner = mBtAdapter.getBluetoothLeScanner();
                    ArrayList<ScanFilter> filters = new ArrayList<>();
                    ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
                    filters.add(tagFilter);
                    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
                    setStatus(e_status.searchDevice);
                    mScanner.startScan(filters, settings, getScanCallback());
                }

                if (status == e_status.foundDevice) {
                    mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
                    if (mBluetoothGatt != null) {
                        setStatus(e_status.discoverServices);
                        mBluetoothGatt.discoverServices();
                    }
                }

                if (status == e_status.discoverServices) {
                    mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
                    if (mService != null) {
                        setStatus(e_status.sendHeartBeat);
                        //
                        // this is no happen!!!
                        //
                        // TODO
                        //BluetoothGattCharacteristic charac = mService.getCharacteristic(ConstParams.serviceUUID);
                        //mBluetoothGatt.writeCharacteristic(characteristic);
                        //mService.addCharacteristic(charac, value));
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG, "error" ,ex);
                interval=30000;
            }

            sleep(interval);
        }
    }).start();
}

private ScanCallback getScanCallback() {
    return new ScanCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
            setStatus(e_status.foundDevice);
            mScanner.stopScan(this);
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            setStatus(e_status.init);
        }
    };
}
private void setStatus(e_status newStatus) {
    Log.i(TAG, "status: " + status + " -> " + newStatus);
    status = newStatus;
}

private BluetoothGattCallback getGattCallback() {
    return new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyUpdate");
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyRead");
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
            super.onConnectionStateChange(gatt, status1, newState);
            Log.i(TAG, "onConnectionStateChange" + status + "," + newState);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered");
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicRead");
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite");
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.i(TAG, "onCharacteristicChanged");
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorRead");
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorWrite");
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            Log.i(TAG, "onReliableWriteCompleted");
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            Log.i(TAG, "onReadRemoteRssi");
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
            Log.i(TAG, "onMtuChanged");
        }

        @Override
        public void onServiceChanged(@NonNull BluetoothGatt gatt) {
            super.onServiceChanged(gatt);
            Log.i(TAG, "onServiceChanged");
        }
    };
}

private void sleep(long millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2025-01-18 11:37:52

所以...我解决了!

问题是我在服务处于连接模式之前调用了discoverServices()。

现在已经可以使用了,

附上修改后的代码,希望有人会喜欢

public class TagService extends Service {
String TAG = "TagService";
byte OPCODE_ACTIVATE_HEART_BEAT = 0x40;

public static boolean running;
public e_status status;

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;

enum e_status {
    none, init, searchDevice, foundDevice, connectingToDevice, discoverServices, sendHeartBeat, finish
}

@Override
public void onCreate() {
    super.onCreate();
    runThread();
}

@Override
public void onDestroy() {
    super.onDestroy();
    running = false;
}

@SuppressLint("MissingPermission")
private void runThread() {
    status = e_status.none;
    running = true;

    new Thread(() -> {
        int interval = 4000;

        while (running && status != e_status.finish) {

            try {
                if (status == e_status.none) {
                    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                    mBtAdapter = mBluetoothManager.getAdapter();
                    setStatus(e_status.init);
                }

                if (status == e_status.init) {
                    mScanner = mBtAdapter.getBluetoothLeScanner();
                    ArrayList<ScanFilter> filters = new ArrayList<>();
                    ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
                    filters.add(tagFilter);
                    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
                    setStatus(e_status.searchDevice);
                    mScanner.startScan(filters, settings, getScanCallback());
                }

                if (status == e_status.foundDevice) {
                    mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
                    setStatus(e_status.connectingToDevice);
                    interval = 500;
                }

                if (status == e_status.discoverServices) {
                    mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
                    if (mService != null) {
                        interval=5000;
                        setStatus(e_status.sendHeartBeat);
                        if(sendHeartBeat()) {
                            setStatus(e_status.finish);
                        }
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG, "error", ex);
                interval = 30000;
            }

            sleep(interval);
        }
    }).start();
}

private ScanCallback getScanCallback() {
    return new ScanCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
            setStatus(e_status.foundDevice);
            mScanner.stopScan(this);
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            setStatus(e_status.init);
        }
    };
}
private void setStatus(e_status newStatus) {
    Log.i(TAG, "status: " + status + " -> " + newStatus);
    status = newStatus;
}
private BluetoothGattCallback getGattCallback() {
    return new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyUpdate");
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyRead");
        }

        @SuppressLint("MissingPermission")
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
            super.onConnectionStateChange(gatt, status1, newState);
            Log.i(TAG, "onConnectionStateChange" + status1 + "," + newState);

            if (newState == BluetoothProfile.STATE_CONNECTED) {
                mBluetoothGatt.discoverServices();
                setStatus(e_status.discoverServices);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered");
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicRead");
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite");
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.i(TAG, "onCharacteristicChanged");
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorRead");
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorWrite");
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            Log.i(TAG, "onReliableWriteCompleted");
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            Log.i(TAG, "onReadRemoteRssi");
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
            Log.i(TAG, "onMtuChanged");
        }

        @Override
        public void onServiceChanged(@NonNull BluetoothGatt gatt) {
            super.onServiceChanged(gatt);
            Log.i(TAG, "onServiceChanged");
        }
    };
}

so... I solved it!!!

The problem was that I call discoverServices() before the service was in Connected mode.

Now it's working

Attach the revised code, hope someone will enjoys it

public class TagService extends Service {
String TAG = "TagService";
byte OPCODE_ACTIVATE_HEART_BEAT = 0x40;

public static boolean running;
public e_status status;

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;

enum e_status {
    none, init, searchDevice, foundDevice, connectingToDevice, discoverServices, sendHeartBeat, finish
}

@Override
public void onCreate() {
    super.onCreate();
    runThread();
}

@Override
public void onDestroy() {
    super.onDestroy();
    running = false;
}

@SuppressLint("MissingPermission")
private void runThread() {
    status = e_status.none;
    running = true;

    new Thread(() -> {
        int interval = 4000;

        while (running && status != e_status.finish) {

            try {
                if (status == e_status.none) {
                    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                    mBtAdapter = mBluetoothManager.getAdapter();
                    setStatus(e_status.init);
                }

                if (status == e_status.init) {
                    mScanner = mBtAdapter.getBluetoothLeScanner();
                    ArrayList<ScanFilter> filters = new ArrayList<>();
                    ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
                    filters.add(tagFilter);
                    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
                    setStatus(e_status.searchDevice);
                    mScanner.startScan(filters, settings, getScanCallback());
                }

                if (status == e_status.foundDevice) {
                    mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
                    setStatus(e_status.connectingToDevice);
                    interval = 500;
                }

                if (status == e_status.discoverServices) {
                    mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
                    if (mService != null) {
                        interval=5000;
                        setStatus(e_status.sendHeartBeat);
                        if(sendHeartBeat()) {
                            setStatus(e_status.finish);
                        }
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG, "error", ex);
                interval = 30000;
            }

            sleep(interval);
        }
    }).start();
}

private ScanCallback getScanCallback() {
    return new ScanCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
            setStatus(e_status.foundDevice);
            mScanner.stopScan(this);
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            setStatus(e_status.init);
        }
    };
}
private void setStatus(e_status newStatus) {
    Log.i(TAG, "status: " + status + " -> " + newStatus);
    status = newStatus;
}
private BluetoothGattCallback getGattCallback() {
    return new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyUpdate");
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
            Log.i(TAG, "onPhyRead");
        }

        @SuppressLint("MissingPermission")
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
            super.onConnectionStateChange(gatt, status1, newState);
            Log.i(TAG, "onConnectionStateChange" + status1 + "," + newState);

            if (newState == BluetoothProfile.STATE_CONNECTED) {
                mBluetoothGatt.discoverServices();
                setStatus(e_status.discoverServices);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered");
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicRead");
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite");
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.i(TAG, "onCharacteristicChanged");
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorRead");
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.i(TAG, "onDescriptorWrite");
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            Log.i(TAG, "onReliableWriteCompleted");
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            Log.i(TAG, "onReadRemoteRssi");
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
            Log.i(TAG, "onMtuChanged");
        }

        @Override
        public void onServiceChanged(@NonNull BluetoothGatt gatt) {
            super.onServiceChanged(gatt);
            Log.i(TAG, "onServiceChanged");
        }
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文