Android 无法通过蓝牙低能量BLE发现服务和特征

Android 无法通过蓝牙低能量BLE发现服务和特征,android,service,bluetooth,bluetooth-lowenergy,android-bluetooth,Android,Service,Bluetooth,Bluetooth Lowenergy,Android Bluetooth,我正在研究蓝牙低能量蓝牙 我可以扫描并找到,通过蓝牙连接到固件设备使用BLE mbluetothgatt=mAl.get(pos.connectGatt)(mContext,false,mGattCallback) 虽然Play store中的应用程序可以在固件设备中发现服务和特性,但我的android应用程序无法发现它们,它总是返回空服务列表。实际上,固件设备已经设置了服务和特性 我不知道为什么,谁知道为什么,请帮助我如何发现服务和特征 谢谢, Java代码 // Various callba

我正在研究蓝牙低能量蓝牙

我可以扫描并找到,通过蓝牙连接到固件设备使用BLE

mbluetothgatt=mAl.get(pos.connectGatt)(mContext,false,mGattCallback)

虽然Play store中的应用程序可以在固件设备中发现
服务
特性
,但我的android应用程序无法发现它们,它总是返回空服务列表。实际上,固件设备已经设置了
服务
特性

我不知道为什么,谁知道为什么,请帮助我如何发现
服务
特征

谢谢,

Java代码

// Various callback methods defined by the BLE API.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                /**
                 * In case already connected to Pas device
                 */
                Log.i("", "Connected to GATT server. " + gatt.discoverServices());
                // CALL THIS METHOD TO BEGIN DISCOVER SERVICES
                gatt.discoverServices();

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                /**
                 * In case disconnected Pas device
                 */
                Log.i("", "Disconnected from GATT server.");
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i("", "onServicesDiscovered " + status + " " + gatt.discoverServices()
                    + " " + gatt.getServices());

            // RETURN : []
            Log.i("", "gatt.getServices() " + gatt.getServices());
            // RETURN : TRUE
            Log.i("", "gatt.getServices() " + gatt.getServices().isEmpty());

            // EXCEPTION HAPPEN BCS SIZE = 0
            gatt.readCharacteristic(gatt.getServices().get(0).getCharacteristics().get(0));

            if (status == BluetoothGatt.GATT_SUCCESS) {
            } else {
                Log.w("", "onServicesDiscovered received: " + status);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(
                BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            Log.i("", "onCharacteristicRead " + status + " " + characteristic);

            if (status == BluetoothGatt.GATT_SUCCESS) {
            }
        }
    };

// Device scan callback.
        private BluetoothAdapter.LeScanCallback mLeScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice device, int rssi,
                                         byte[] scanRecord) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                /**
                                 * Should show Scan Pas Devices :
                                 * Add to array list scanned pas devices if it is not exist in list
                                 * before
                                 */
                                if (!mAlPasDeviceNames.contains(device.getName())) {
                                    mAlPasDevices.add(device);
                                    mAlPasDeviceNames.add(device.getName());

                                    // set adapter and show on UI
                                    mLv.setAdapter(new PasConnectionAdapter(
                                            getActivity(),
                                            R.layout.simple_list_item_scan_pas_device,
                                            mAlPasDevices));
                                }
                            }
                        });
                    }
                };
Logcat

connect() - device: E2:5A:6B:5A:18:57, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=93455863-c385-4563-9197-6592024cc8cc
D/BtGatt.GattService: registerClient() - UUID=93455863-c385-4563-9197-6592024cc8cc
D/BtGatt.GattService: onClientRegistered() - UUID=93455863-c385-4563-9197-6592024cc8cc, clientIf=5
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5
D/BtGatt.GattService: clientConnect() - address=E2:5A:6B:5A:18:57, isDirect=true
E/BluetoothRemoteDevices: aclStateChangeCallback: Device is NULL
D/BtGatt.GattService: onConnected() - clientIf=5, connId=5, address=E2:5A:6B:5A:18:57
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=E2:5A:6B:5A:18:57
D/BluetoothGatt: discoverServices() - device: E2:5A:6B:5A:18:57
D/BtGatt.GattService: discoverServices() - address=E2:5A:6B:5A:18:57, connId=5
I/: Connected to GATT server. true
D/BluetoothGatt: discoverServices() - device: E2:5A:6B:5A:18:57
D/BtGatt.GattService: discoverServices() - address=E2:5A:6B:5A:18:57, connId=5
D/BtGatt.GattService: onSearchCompleted() - connId=5, status=0
D/BtGatt.GattService: onSearchCompleted() - connId=5, status=0
E/BtGatt.btif: bta_to_btif_uuid: Unknown UUID length 61618!
E/BtGatt.btif: bta_to_btif_uuid: Unknown UUID length 61619!
I/: gatt.getService(uuid) null
D/BluetoothGatt: onSearchComplete() = Device=E2:5A:6B:5A:18:57 Status=0
D/BluetoothGatt: discoverServices() - device: E2:5A:6B:5A:18:57
D/BtGatt.GattService: discoverServices() - address=E2:5A:6B:5A:18:57, connId=5
D/BtGatt.GattService: onSearchCompleted() - connId=5, status=0
I/: onServicesDiscovered 0 true []
I/: gatt.getServices() []
I/: gatt.getServices() true
E/BtGatt.btif: bta_to_btif_uuid: Unknown UUID length 61621!
W/BluetoothGatt: Unhandled exception in callback
W/BluetoothGatt: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
W/BluetoothGatt:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
W/BluetoothGatt:     at java.util.ArrayList.get(ArrayList.java:308)
W/BluetoothGatt:     at ui.fragment.PasConnectionFragment$1.onServicesDiscovered(PasConnectionFragment.java:379)
W/BluetoothGatt:     at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:304)
W/BluetoothGatt:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:217)
W/BluetoothGatt:     at android.os.Binder.execTransact(Binder.java:446)

一个问题可能是您多次调用gatt.discoverServices()。在onConnectionStateChange方法中执行两次:

Log.i("", "Connected to GATT server. " + gatt.discoverServices());
            // CALL THIS METHOD TO BEGIN DISCOVER SERVICES
            gatt.discoverServices();
然后再次使用OnServicesDiscovery方法

Log.i("", "onServicesDiscovered " + status + " " + gatt.discoverServices()
                + " " + gatt.getServices());

这意味着您要反复启动服务扫描。不要在日志中调用它,一次就足够了。

检查以下代码,它将显示服务数量及其UUID

 @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.e("BluetoothLeService", "onServicesDiscovered()");
            if (status == BluetoothGatt.GATT_SUCCESS) {


                List<BluetoothGattService> gattServices = mBluetoothGatt.getServices();
                Log.e("onServicesDiscovered", "Services count: "+gattServices.size());

                for (BluetoothGattService gattService : gattServices) {
                    String serviceUUID = gattService.getUuid().toString();
                    Log.e("onServicesDiscovered", "Service uuid "+serviceUUID);
                }


            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }
@覆盖
发现服务上的公共无效(Bluetooth gatt,int状态){
Log.e(“BluetoothLeService”、“OnServicesDiscovery()”);
如果(状态==蓝牙GATT.GATT\U成功){
List gattServices=mbluetothgatt.getServices();
Log.e(“OnServicesDiscoveryd”,“服务计数:+gattServices.size());
用于(蓝牙gattService gattService:gattServices){
字符串serviceUUID=gattService.getUuid().toString();
Log.e(“OnServicesDiscoveryd”、“服务uuid”+serviceUUID);
}
}否则{
Log.w(标记“OnServicesDiscovery已接收:+状态);
}
}

您可以按如下方式显示提供的服务和特征:

  for (BluetoothGattService service : gatt.getServices()) {
                Log.d(TAG, "Found Service " + service.getUuid().toString());
                for(BluetoothGattCharacteristic mcharacteristic :service.getCharacteristics())
                {
                    Log.d(TAG, "Found Characteristic " + mcharacteristic.getUuid().toString());
                }

            }

但这可能行不通,或者它只会向您显示通用属性概要文件o.e.那么制造商似乎在显示其余服务和特性之前交换了数据。在这种情况下,如果您使用android手机,您可以使用hci日志并在wireshark中检查它。在那里你可以嗅到交换的包裹。“破解”个人资料并非易事,也取决于复杂性,但如果没有文档,这是你唯一可能做到的事。

我也面临同样的问题。我调用了
discoverServices()
,但什么也没发生。然后我找到了解决办法。只有在成功连接到设备后,才能调用
discoverServices()
。这是我的密码:

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

        if(newState == BluetoothProfile.STATE_CONNECTED) {
            Log.d(TAG, "Connected to GATT server, discovering services...");
            gatt.discoverServices();
        } else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.d(TAG, "Disconnected from GATT server");
        }

    }
在这里,我尝试仅在连接到设备后才发现服务。这对我很有效,我能够在
onServicesDiscovered()
方法中检索到所需的服务ID。 希望我能帮忙