尝试通过BLE + BT通过Android设备连接到iOS

时间:2020-10-08 11:31:03

标签: android ios bluetooth bluetooth-lowenergy bluetooth-gatt

我正在尝试通过Android蓝牙+ BLE连接到i-phone。 我的目标是通过Android蓝牙+ BLE读取iOS通知。

我能够在android应用程序中显示i-phone蓝牙,并且能够连接到i-phone,但是我无法找到Notification特性。

我从此link获得了通知特征UUID,我正在使用通知源:UUID

这是我的BluetoothGattCallback:

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

            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    final BluetoothGatt mGatt = gatt;
                    Handler handler;
                    handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mGatt.discoverServices();
                        }
                    });
                    //gatt.discoverServices();

                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    try
                    {
                        Log.i("no_conn", "Connection unsuccessful with status"+status);
                        //mGatt.disconnect();
                        mGatt.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }

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

            if (status != BluetoothGatt.GATT_SUCCESS) {
                Log.i("Not success", "Device service discovery unsuccessful, status " + status);
                return;
            }
            List<BluetoothGattService> matchingServices = gatt.getServices();
            gatt.getService(UUID.fromString(SERVICE_STRING));
            List<BluetoothGattCharacteristic> matchingCharacteristics = BluetoothUtils.findCharacteristics(gatt);
            if (matchingCharacteristics.isEmpty()) {
                Log.i("No characteristics", "Unable to find characteristics.");
                showToast("No characteristic found");
                return;
            }else {
                showToast("characteristic found");
            }
        }
    }; 

这是我的findCharacteristics函数:

public static List<BluetoothGattCharacteristic> findCharacteristics1(BluetoothGatt bluetoothGatt) {
        List<BluetoothGattCharacteristic> matchingCharacteristics = new ArrayList<>();

        List<BluetoothGattService> serviceList = bluetoothGatt.getServices();
        BluetoothGattService service = null;
        
        for (BluetoothGattService bgservice : serviceList) {
            String serviceIdString = bgservice.getUuid()
                    .toString();
            if (matchesServiceUuidString(serviceIdString)) {
                service = bgservice;
            }
        }
        
        if (service == null) {
            Log.i("Null service", "Null service.");
            return matchingCharacteristics;
        }
        return matchingCharacteristics;
    }

在这里,我与带有通知源:UUID 的serviceIdString匹配。 我想念什么吗?

0 个答案:

没有答案