没有输入onCharacteristicRead或onCharacteristicChanged

时间:2018-07-17 18:57:54

标签: java android bluetooth-lowenergy

正如标题所述,我的代码似乎在onServicesDiscovered之后卡住了,我知道的唯一原因是我的Log消息表明了这一点。谁能透露我的代码有什么问题?我可以连接到我的设备,但是没有收到数据。我在应用程序方面知道它的原因,因为我使用过adafruit bluefruit应用程序和nRF应用程序,它们都接收数据。

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);

            Log.i("onConnectionStateChange", "State Changed from: " + status + " to " + newState);
            gatt.discoverServices();

            // seems to connect but if statements dont work?

//            if (newState == BluetoothProfile.STATE_CONNECTED){ // 2
//                Toast.makeText(BluetoothDiscovery.this, "Attempting service discovery", Toast.LENGTH_SHORT).show();
//                Log.i("onConnectionStateChange", "Attempting service discovery: " + mGatt.discoverServices());
////                mGatt.discoverServices();
//            } else if (newState == BluetoothProfile.STATE_DISCONNECTED){ // 0
//                Toast.makeText(BluetoothDiscovery.this, "Connection has been terminated", Toast.LENGTH_SHORT).show();
//            } else if (newState == BluetoothProfile.STATE_CONNECTING) { // 1
//                Log.i("Welp", "IM GIVIN ER ALL SHES GOT CAPN");
//            } else if (newState == BluetoothProfile.STATE_DISCONNECTING) { // 3
//                Log.i("Welp", "HOW DID WE GET HERE");
//            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status){
            super.onServicesDiscovered(gatt, status);

            Log.i("onServicesDiscovered", "Hey, we found a service");

            List<BluetoothGattService> services = gatt.getServices();
            Log.i("SERVICE", "Services: " + services.toString());


            //BluetoothGattCharacteristic characteristic = services.get(4).getCharacteristics().get(0);
            BluetoothGattCharacteristic characteristic = gatt.getService(baseUUID).getCharacteristic(rxUUID);


            if (characteristic != null){
                gatt.setCharacteristicNotification(characteristic, true);
            }


            List<BluetoothGattDescriptor> describeMe = characteristic.getDescriptors();
            Log.i("DESCRIPTORS", "Descriptors: " + describeMe.toString());
            Log.i("DESCRIPTORS", "Descriptors: " + describeMe.get(1).getUuid().toString());


            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(describeMe.get(1).getUuid());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptor);

            Log.i("ByeSERVICESDISCOVERED", "that");
        }

0 个答案:

没有答案